{-# Language QuasiQuotes #-}
module Main (main) where
import Advent.Format (format)
import Intcode
main :: IO ()
IO ()
main =
do Machine
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|]
Int -> IO ()
forall a. Show a => a -> IO ()
print (Int -> Int -> Machine -> Int
startup Int
12 Int
2 Machine
pgm)
Int -> IO ()
forall a. Show a => a -> IO ()
print ([Int] -> Int
forall a. HasCallStack => [a] -> a
head [ Int
100 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
noun Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
verb
| Int
noun <- [Int
0..Int
99]
, Int
verb <- [Int
0..Int
99]
, Int -> Int -> Machine -> Int
startup Int
noun Int
verb Machine
pgm Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
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"