Now uses a Reader monad
[advent-of-code-19.git] / advent02 / src / advent02.hs
1
2 import qualified Data.Text.IO as TIO
3
4 import Intcode
5
6 import qualified Data.Map as M
7 import Data.Map ((!))
8
9 main :: IO ()
10 main = do
11 text <- TIO.readFile "data/advent02.txt"
12 let mem = parseMachineMemory text
13 let machine = makeMachine mem
14 print $ part1 machine
15 print $ part2 machine
16
17
18 -- part1 machine = (_memory $ execState runAll machine1202)!0
19 -- where machine1202 = machine { _memory = M.insert 1 12 $ M.insert 2 2 $ _memory machine }
20
21
22 part1 = nounVerbResult 12 2
23
24 part2Target = 19690720
25
26 part2 machine = noun * 100 + verb
27 where (noun, verb) = head $ [(n, v) | n <- [0..99], v <- [0..99],
28 nounVerbResult n v machine == part2Target ]
29
30
31 nounVerbResult :: Integer -> Integer -> Machine -> Integer
32 nounVerbResult noun verb machine = machineOutput machine'
33 where (_, machine', _) = runMachine [] nvMachine
34 nvMachine = machineNounVerb machine noun verb
35
36 machineNounVerb :: Machine -> Integer -> Integer -> Machine
37 machineNounVerb machine noun verb = machine { _memory = M.insert 1 noun $ M.insert 2 verb $ _memory machine }
38
39 machineOutput :: Machine -> Integer
40 machineOutput machine = (_memory machine)!0
41