From 5072f95356c17607722dddb918805ea1c48b8332 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Thu, 7 Jan 2021 11:49:24 +0000 Subject: [PATCH] Done day 23 --- advent23/package.yaml | 61 ++++++++++++ advent23/src/advent23.hs | 119 ++++++++++++++++++++++++ problems/day23.html | 194 +++++++++++++++++++++++++++++++++++++++ stack.yaml | 1 + 4 files changed, 375 insertions(+) create mode 100644 advent23/package.yaml create mode 100644 advent23/src/advent23.hs create mode 100644 problems/day23.html diff --git a/advent23/package.yaml b/advent23/package.yaml new file mode 100644 index 0000000..955330e --- /dev/null +++ b/advent23/package.yaml @@ -0,0 +1,61 @@ +# This YAML file describes your package. Stack will automatically generate a +# Cabal file when you run `stack build`. See the hpack website for help with +# this file: . + +name: advent23 +synopsis: Advent of Code +version: '0.0.1' + +default-extensions: +- AllowAmbiguousTypes +- ApplicativeDo +- BangPatterns +- BlockArguments +- DataKinds +- DeriveFoldable +- DeriveFunctor +- DeriveGeneric +- DeriveTraversable +- EmptyCase +- FlexibleContexts +- FlexibleInstances +- FunctionalDependencies +- GADTs +- GeneralizedNewtypeDeriving +- ImplicitParams +- KindSignatures +- LambdaCase +- MonadComprehensions +- MonoLocalBinds +- MultiParamTypeClasses +- MultiWayIf +- NamedFieldPuns +- NegativeLiterals +- NumDecimals +# - OverloadedLists +- OverloadedStrings +- PartialTypeSignatures +- PatternGuards +- PatternSynonyms +- PolyKinds +- RankNTypes +- RecordWildCards +- ScopedTypeVariables +- TemplateHaskell +- TransformListComp +- TupleSections +- TypeApplications +- TypeFamilies +- TypeInType +- TypeOperators +- ViewPatterns + +executables: + advent23: + main: advent23.hs + source-dirs: src + dependencies: + - base >= 2 && < 6 + - pointedlist + - lens + - vector diff --git a/advent23/src/advent23.hs b/advent23/src/advent23.hs new file mode 100644 index 0000000..47e0e33 --- /dev/null +++ b/advent23/src/advent23.hs @@ -0,0 +1,119 @@ +-- import Debug.Trace + +import qualified Data.List.PointedList.Circular as P +import Control.Lens +import Data.Maybe +import Data.Char (digitToInt) + +import Control.Monad +import Control.Monad.ST +import Data.STRef +import qualified Data.Vector.Unboxed.Mutable as V + + +puzzleInput :: [Int] +puzzleInput = map digitToInt "538914762" +-- puzzleInput = map digitToInt "389125467" -- example + +main :: IO () +main = + do putStrLn $ part1 puzzleInput + print $ part2 puzzleInput + + +part1 nums = label $ playN cups 100 + where cups = fromJust $ P.fromList nums + +part2 nums = a * b + where finalCups = runGame nums 1000000 10000000 + (a, b) = clockwiseOf1 finalCups + + +label cups = concatMap show $ tail $ takeRight (P.length cups) atOne + where atOne = fromJust $ P.find 1 cups + +playN cups n = (iterate playOne cups) !! n + +playOne cups = P.next replacedAtCurrent + where current = cups ^. P.focus + held = takeRight 3 $ P.next cups + shorter = fromJust $ dropRight 3 $ P.next cups + destination = validDestination (current - 1) 9 held + shorterAtDestination = fromJust $ P.find destination shorter + replaced = foldr P.insertRight shorterAtDestination $ reverse held + replacedAtCurrent = fromJust $ P.find current replaced + + +validDestination 0 max missing = validDestination max max missing +validDestination n max missing + | n `elem` missing = validDestination (n - 1) max missing + | otherwise = n + + +takeRight :: Int -> P.PointedList a -> [a] +takeRight 0 _ = [] +takeRight n xs = (xs ^. P.focus):(takeRight (n - 1) $ P.next xs) + +dropRight :: Int -> P.PointedList a -> Maybe (P.PointedList a) +dropRight 0 xs = Just xs +dropRight n xxs = case (P.deleteRight xxs) of + Just xs -> dropRight (n - 1) xs + Nothing -> Nothing + + +clockwiseOf1 cups = (a, b) + where a = cups!!1 + b = cups!!a + +runGame :: [Int] -> Int -> Int -> [Int] +runGame seed cupsNeeded roundsNeeded = + runST $ + do cups <- seedGame seed cupsNeeded + gameLoop roundsNeeded cupsNeeded cups + mapM (V.read cups) [0..cupsNeeded] + +seedGame :: [Int] -> Int -> ST s (V.MVector s Int) +seedGame seed cupsNeeded = + do cups <- V.new (cupsNeeded + 1) + let extended = seed ++ [10, 11..] + forM_ [0..cupsNeeded] $ \i -> V.write cups i (i + 1) + forM_ (zip seed $ tail extended) $ \(i, j) -> V.write cups i j + V.write cups 0 (head seed) + let end = if cupsNeeded > (length seed) + then cupsNeeded + else last seed + V.write cups end (head seed) + return cups + +gameLoop targetRound maxCups cups = + do forM_ [1..targetRound] + (\_ -> gameStep maxCups cups) + return () + +gameStep :: Int -> V.MVector s Int -> ST s () +gameStep maxCups cups = + do current <- V.read cups 0 + held1 <- V.read cups current + held2 <- V.read cups held1 + held3 <- V.read cups held2 + afterHeld <- V.read cups held3 + + -- close the loop, removing the held cups + V.write cups current afterHeld + + let destination = + validDestination (current - 1) maxCups [held1, held2, held3] + afterDestination <- V.read cups destination + + -- make the held come after the destination + V.write cups destination held1 + + -- make the end of the held point into the rest of the loop + V.write cups held3 afterDestination + + -- advance current + nextCup <- V.read cups current + -- and store it + V.write cups 0 nextCup + return () + diff --git a/problems/day23.html b/problems/day23.html new file mode 100644 index 0000000..18a99c3 --- /dev/null +++ b/problems/day23.html @@ -0,0 +1,194 @@ + + + + +Day 23 - Advent of Code 2020 + + + + + + + +

Advent of Code

Neil Smith (AoC++) 46*

          2020

+ + + +
+ +

--- Day 23: Crab Cups ---

The small crab challenges you to a game! The crab is going to mix up some cups, and you have to predict where they'll end up.

+

The cups will be arranged in a circle and labeled clockwise (your puzzle input). For example, if your labeling were 32415, there would be five cups in the circle; going clockwise around the circle from the first cup, the cups would be labeled 3, 2, 4, 1, 5, and then back to 3 again.

+

Before the crab starts, it will designate the first cup in your list as the current cup. The crab is then going to do 100 moves.

+

Each move, the crab does the following actions:

+
    +
  • The crab picks up the three cups that are immediately clockwise of the current cup. They are removed from the circle; cup spacing is adjusted as necessary to maintain the circle.
  • +
  • The crab selects a destination cup: the cup with a label equal to the current cup's label minus one. If this would select one of the cups that was just picked up, the crab will keep subtracting one until it finds a cup that wasn't just picked up. If at any point in this process the value goes below the lowest value on any cup's label, it wraps around to the highest value on any cup's label instead.
  • +
  • The crab places the cups it just picked up so that they are immediately clockwise of the destination cup. They keep the same order as when they were picked up.
  • +
  • The crab selects a new current cup: the cup which is immediately clockwise of the current cup.
  • +
+

For example, suppose your cup labeling were 389125467. If the crab were to do merely 10 moves, the following changes would occur:

+
-- move 1 --
+cups: (3) 8  9  1  2  5  4  6  7 
+pick up: 8, 9, 1
+destination: 2
+
+-- move 2 --
+cups:  3 (2) 8  9  1  5  4  6  7 
+pick up: 8, 9, 1
+destination: 7
+
+-- move 3 --
+cups:  3  2 (5) 4  6  7  8  9  1 
+pick up: 4, 6, 7
+destination: 3
+
+-- move 4 --
+cups:  7  2  5 (8) 9  1  3  4  6 
+pick up: 9, 1, 3
+destination: 7
+
+-- move 5 --
+cups:  3  2  5  8 (4) 6  7  9  1 
+pick up: 6, 7, 9
+destination: 3
+
+-- move 6 --
+cups:  9  2  5  8  4 (1) 3  6  7 
+pick up: 3, 6, 7
+destination: 9
+
+-- move 7 --
+cups:  7  2  5  8  4  1 (9) 3  6 
+pick up: 3, 6, 7
+destination: 8
+
+-- move 8 --
+cups:  8  3  6  7  4  1  9 (2) 5 
+pick up: 5, 8, 3
+destination: 1
+
+-- move 9 --
+cups:  7  4  1  5  8  3  9  2 (6)
+pick up: 7, 4, 1
+destination: 5
+
+-- move 10 --
+cups: (5) 7  4  1  8  3  9  2  6 
+pick up: 7, 4, 1
+destination: 3
+
+-- final --
+cups:  5 (8) 3  7  4  1  9  2  6 
+
+

In the above example, the cups' values are the labels as they appear moving clockwise around the circle; the current cup is marked with ( ).

+

After the crab is done, what order will the cups be in? Starting after the cup labeled 1, collect the other cups' labels clockwise into a single string with no extra characters; each number except 1 should appear exactly once. In the above example, after 10 moves, the cups clockwise from 1 are labeled 9, 2, 6, 5, and so on, producing 92658374. If the crab were to complete all 100 moves, the order after cup 1 would be 67384529.

+

Using your labeling, simulate 100 moves. What are the labels on the cups after cup 1?

+
+

Your puzzle answer was 54327968.

--- Part Two ---

Due to what you can only assume is a mistranslation (you're not exactly fluent in Crab), you are quite surprised when the crab starts arranging many cups in a circle on your raft - one million (1000000) in total.

+

Your labeling is still correct for the first few cups; after that, the remaining cups are just numbered in an increasing fashion starting from the number after the highest number in your list and proceeding one by one until one million is reached. (For example, if your labeling were 54321, the cups would be numbered 5, 4, 3, 2, 1, and then start counting up from 6 until one million is reached.) In this way, every number from one through one million is used exactly once.

+

After discovering where you made the mistake in translating Crab Numbers, you realize the small crab isn't going to do merely 100 moves; the crab is going to do ten million (10000000) moves!

+

The crab is going to hide your stars - one each - under the two cups that will end up immediately clockwise of cup 1. You can have them if you predict what the labels on those cups will be when the crab is finished.

+

In the above example (389125467), this would be 934001 and then 159792; multiplying these together produces 149245887792.

+

Determine which two cups will end up immediately clockwise of cup 1. What do you get if you multiply their labels together?

+
+

Your puzzle answer was 157410423276.

Both parts of this puzzle are complete! They provide two gold stars: **

+

At this point, you should return to your Advent calendar and try another puzzle.

+

Your puzzle input was 538914762.

+

You can also this puzzle.

+
+ + + + + + \ No newline at end of file diff --git a/stack.yaml b/stack.yaml index 86013c9..021f4ba 100644 --- a/stack.yaml +++ b/stack.yaml @@ -57,6 +57,7 @@ packages: - advent20 - advent21 - advent22 +- advent23 # Dependency packages to be pulled from upstream that are not in the resolver. # These entries can reference officially published versions as well as -- 2.34.1