cecograph
kiwifarms.net
- Joined
- Oct 17, 2018
Bit of a bastard. The first part was easy enough, but the second part required a fair bit of refactoring of my Intcode computer implementation. But with that done, the solution isn't too bad (writing mutable code in Haskell isn't pretty).Let me know what you think of day 7.
Code:
unblock1 :: (STRef s (State s), Yield, Yield) -> ST s ()
unblock1 (state,MoreInput,Output n) = pushInput state n
unblock1 _ = pure ()
feedback :: V.Vector Int -> [Int] -> Maybe Int
feedback prog (setting:settings) = runST $ do
firstState <- newSTRef =<< State 0 [setting,0] <$> V.thaw prog
restStates <- sequence
[ newSTRef =<< State 0 [s] <$> V.thaw prog | s <- settings ]
let states = reverse $ firstState : restStates
loop Nothing states =<< traverse run1 states
where loop output _ (Halt:_) = pure output
loop output states (y:ys) = do
let output' =
case y of
Output o -> Just o
_ -> output
traverse unblock1 (zip3 states (y:ys) (ys ++ [y]))
loop output' states =<< traverse run1 states
part2 :: IO Int
part2 = maximum . (\prog -> mapMaybe (feedback prog) (permutations [5..9]))
<$> readProgram
My guess is that they've toned it down massively this year, probably because of complaints of last year's being too difficult.
How did you tackle day 3?
Last edited: