[hs] Migrate 2019_02

This commit is contained in:
Joscha 2020-12-06 11:32:14 +00:00
parent 3e82db1f0c
commit dddc5ec451
3 changed files with 27 additions and 16 deletions

View file

@ -14,7 +14,10 @@ import Data.Char
import Data.Void
import qualified Data.Text as T
import Text.Megaparsec
import Text.Megaparsec hiding (InvalidPosException, Pos,
PosState, SourcePos, State,
defaultTabWidth, initialPos, mkPos,
pos1, sourcePosPretty, unPos)
import Text.Megaparsec.Char
import Text.Megaparsec.Char.Lexer (binary, decimal, float,
hexadecimal, octal, scientific,

View file

@ -4,8 +4,10 @@ module Aoc.Y2019
import Aoc.Day
import qualified Aoc.Y2019.D01 as D01
import qualified Aoc.Y2019.D02 as D02
year :: Year
year = Year 2019
[ ( 1, D01.day)
, ( 2, D02.day)
]

View file

@ -6,15 +6,17 @@
-- day requires an intcode machine, instead of maintaining a single global
-- intcode machine implementation.
module Aoc.Y2019.A02
( solve201902
module Aoc.Y2019.D02
( day
) where
import Control.Monad
import Data.Foldable
import qualified Data.Map.Strict as M
import qualified Data.Text as T
import qualified Data.Text.IO as T
import Aoc.Day
import Aoc.Parse
newtype Memory = Memory { unmemory :: M.Map Int Int }
@ -86,15 +88,16 @@ run s = case step s of
patch :: Int -> Int -> Memory -> Memory
patch noun verb = writeMem 2 verb . writeMem 1 noun
solve201902 :: FilePath -> IO ()
solve201902 f = do
values <- map (read . T.unpack) . T.splitOn "," <$> T.readFile f
let mem = newMem values
parser :: Parser Memory
parser = newMem <$> (decimal `sepBy` char ',') <* newline
solver :: Memory -> IO ()
solver mem = do
putStrLn ">> Part 1"
let (s1, _) = run $ newState $ patch 12 2 mem
putStrLn $ "Value at position 0: " <> show (readMem 0 $ stateMem s1)
putStrLn ""
putStrLn ">> Part 2"
let attempts = [(noun, verb) | noun <- [0..99], verb <- [0..99]]
for_ attempts $ \(noun, verb) -> do
@ -102,3 +105,6 @@ solve201902 f = do
Just result = readMem 0 $ stateMem s2
when (result == 19690720) $
putStrLn $ "100 * noun + verb = " <> show (100 * noun + verb)
day :: Day
day = dayParse parser solver