Safe Haskell | None |
---|
Day23
Description
General - Read careful. There is nothing difficult about this. if you follow the instructions and implement the actions correctly you will find/get the solution.
One small trick/simplification. The position of current does not matter. And that means we can *just* reorder the list in/with the last action of every move/cycle to make current the first element of the list.
Part 1 - So far so good.
Part 2 - Big surprise: The (initial) list based solution does not work. Way to slow. To get a grip on this I ...
- ... introduced a CircularList type
- ... that was (initially) using a list (means we get no performance improvements, but we get a nice interface)
- ... and then I experimented with various data-structures to improve the performance
There are various day23p2-* branches that you can take a look at to see/understand the options I looked at. At the end I went with a vector based implementation (that is still very slow).
Synopsis
- type Moves = Int
- type Pickup = Int
- data State = State Moves CircularList [Pickup]
- input :: String -> State
- removeCups :: State -> State
- selectDestination :: State -> State
- placePickupCups :: State -> State
- newCurrentCup :: State -> State
- actions :: State -> State
- executeMoves :: State -> State
- collect :: Int -> CircularList -> [Int]
- ints2Int :: [Int] -> Int
- part1 :: State -> Int
- collect' :: Int -> CircularList -> (Int, Int)
- part2 :: State -> Int
Documentation
The State to maintain between moves.
Constructors
State Moves CircularList [Pickup] |
removeCups :: State -> State #
As part of a move: Remove 3 cups (and put them into pickup).
selectDestination :: State -> State #
As part of a move: Select the next destination/current.
placePickupCups :: State -> State #
As part of a move: Place the cups from pickup.
newCurrentCup :: State -> State #
As part of a move: Determine the new current cup.
executeMoves :: State -> State #
Execute moves (until there are no more moves to make).
collect :: Int -> CircularList -> [Int] #
Collect the cups after cup label
collect' :: Int -> CircularList -> (Int, Int) #
Collect values of the 2 ups after cup label