Skip to content

Commit 80b70a1

Browse files
committed
Day 2
1 parent 2c40020 commit 80b70a1

File tree

5 files changed

+65
-3
lines changed

5 files changed

+65
-3
lines changed

.ghcid

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
--command="cabal v2-repl --repl-options=-fno-code --repl-options=-fno-break-on-exception --repl-options=-fno-break-on-error --repl-options=-v1 --repl-options=-ferror-spans --repl-options=-j --repl-options=-Wall"
1+
--command="cabal v2-repl --repl-options=-fno-break-on-exception --repl-options=-fno-break-on-error --repl-options=-v1 --repl-options=-ferror-spans --repl-options=-j --repl-options=-Wall"

advent2019.cabal

+6
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,9 @@ executable Day01
3434
build-depends: advent2019, base,
3535
hs-source-dirs: execs
3636
default-language: Haskell2010
37+
38+
executable Day02
39+
main-is: Day02.hs
40+
build-depends: advent2019, base, containers
41+
hs-source-dirs: execs
42+
default-language: Haskell2010

common/Advent.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
module Advent
22
( module Advent
3-
, satisfy, anySingle
3+
, satisfy, anySingle, sepBy
44
) where
55

66
import Control.Applicative
77
import System.Environment
88
import Text.Printf
99
import Data.Foldable (toList)
10-
import Text.Megaparsec (setInput, anySingle, satisfy, parse, Parsec, eof)
10+
import Text.Megaparsec (setInput, anySingle, satisfy, parse, Parsec, eof, sepBy)
1111
import Text.Megaparsec.Char (newline)
1212
import Text.Megaparsec.Char.Lexer (decimal, signed)
1313
import Text.Megaparsec.Error (errorBundlePretty)

execs/Day02.hs

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{-# Options_GHC -w #-}
2+
{-# Language OverloadedStrings #-}
3+
{-|
4+
Module : Main
5+
Description : Day 2 solution
6+
Copyright : (c) Eric Mertens, 2019
7+
License : ISC
8+
Maintainer : [email protected]
9+
10+
<https://adventofcode.com/2019/day/2>
11+
12+
-}
13+
module Main (main) where
14+
15+
import Advent
16+
import Data.IntMap (IntMap)
17+
import qualified Data.IntMap as IntMap
18+
19+
type Program = IntMap Int
20+
21+
(!) :: Program -> Int -> Int
22+
(!) = (IntMap.!)
23+
24+
newProgram :: [Int] -> Program
25+
newProgram = IntMap.fromList . zip [0..]
26+
27+
main :: IO ()
28+
main =
29+
do inp <- getParsedInput 2 (number `sepBy` ",")
30+
let pgm = newProgram inp
31+
print (startup 12 2 pgm)
32+
print (head [ 100 * noun + verb
33+
| noun <- [0..99]
34+
, verb <- [0..99]
35+
, startup noun verb pgm == 19690720 ])
36+
37+
-- | Run the given program after assigning the given noun and verb.
38+
startup :: Int {- ^ noun -} -> Int {- ^ verb -} -> Program -> Int
39+
startup noun verb
40+
= run 0
41+
. IntMap.insert 1 noun
42+
. IntMap.insert 2 verb
43+
44+
-- | Run the given program starting at the given program counter
45+
-- returning the initial memory value once the program halts.
46+
run :: Int {- ^ program counter -} -> Program -> Int
47+
run i pgm =
48+
case arg 0 of
49+
1 -> math (+)
50+
2 -> math (*)
51+
99 -> pgm ! 0
52+
where
53+
math f = run (i+4) (IntMap.insert (arg 3) (val 1 `f` val 2) pgm)
54+
val j = pgm ! arg j
55+
arg j = pgm ! (i+j)

inputs/input02.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,13,1,19,1,19,10,23,1,23,13,27,1,6,27,31,1,9,31,35,2,10,35,39,1,39,6,43,1,6,43,47,2,13,47,51,1,51,6,55,2,6,55,59,2,59,6,63,2,63,13,67,1,5,67,71,2,9,71,75,1,5,75,79,1,5,79,83,1,83,6,87,1,87,6,91,1,91,5,95,2,10,95,99,1,5,99,103,1,10,103,107,1,107,9,111,2,111,10,115,1,115,9,119,1,13,119,123,1,123,9,127,1,5,127,131,2,13,131,135,1,9,135,139,1,2,139,143,1,13,143,0,99,2,0,14,0

0 commit comments

Comments
 (0)