{-# Language QuasiQuotes #-}
module Main (main) where
import Advent.Format (format)
import Intcode
main :: IO ()
IO ()
main =
do pgm <- [Int] -> Machine
new ([Int] -> Machine) -> IO [Int] -> IO Machine
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [format|2019 2 %d&,%n|]
print (startup 12 2 pgm)
print (head [ 100 * noun + verb
| noun <- [0..99]
, verb <- [0..99]
, startup noun verb pgm == 19690720 ])
startup :: Int -> Int -> Machine -> Int
startup :: Int -> Int -> Machine -> Int
startup Int
noun Int
verb
= (Machine -> Int -> Int
! Int
0)
(Machine -> Int) -> (Machine -> Machine) -> Machine -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Machine -> Machine
runPgm
(Machine -> Machine) -> (Machine -> Machine) -> Machine -> Machine
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Int -> Machine -> Machine
set Int
1 Int
noun
(Machine -> Machine) -> (Machine -> Machine) -> Machine -> Machine
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Int -> Machine -> Machine
set Int
2 Int
verb
runPgm :: Machine -> Machine
runPgm :: Machine -> Machine
runPgm Machine
mach =
case Machine -> Step
step Machine
mach of
Step Machine
mach' -> Machine -> Machine
runPgm Machine
mach'
Step
StepHalt -> Machine
mach
Step
_ -> String -> Machine
forall a. HasCallStack => String -> a
error String
"Unexpected step on day 2"