From dddc5ec451ff028c8f0a5a4869a0178d47e13abf Mon Sep 17 00:00:00 2001 From: Joscha Date: Sun, 6 Dec 2020 11:32:14 +0000 Subject: [PATCH] [hs] Migrate 2019_02 --- hs/src/Aoc/Parse.hs | 5 +++- hs/src/Aoc/Y2019.hs | 2 ++ hs/src/Aoc/Y2019/{A02.hs => D02.hs} | 36 +++++++++++++++++------------ 3 files changed, 27 insertions(+), 16 deletions(-) rename hs/src/Aoc/Y2019/{A02.hs => D02.hs} (83%) diff --git a/hs/src/Aoc/Parse.hs b/hs/src/Aoc/Parse.hs index 512bd7b..3261298 100644 --- a/hs/src/Aoc/Parse.hs +++ b/hs/src/Aoc/Parse.hs @@ -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, diff --git a/hs/src/Aoc/Y2019.hs b/hs/src/Aoc/Y2019.hs index 966dc7e..bb5bd84 100644 --- a/hs/src/Aoc/Y2019.hs +++ b/hs/src/Aoc/Y2019.hs @@ -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) ] diff --git a/hs/src/Aoc/Y2019/A02.hs b/hs/src/Aoc/Y2019/D02.hs similarity index 83% rename from hs/src/Aoc/Y2019/A02.hs rename to hs/src/Aoc/Y2019/D02.hs index 99ac345..6421730 100644 --- a/hs/src/Aoc/Y2019/A02.hs +++ b/hs/src/Aoc/Y2019/D02.hs @@ -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 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 } @@ -53,7 +55,7 @@ data StepError readAt :: State -> Int -> Either StepError Int readAt s i = case readMem i $ stateMem s of Nothing -> Left $ CouldNotRead i - Just v -> Right v + Just v -> Right v writeAt :: Int -> Int -> State -> State writeAt addr val s = s{stateMem = writeMem addr val $ stateMem s} @@ -63,10 +65,10 @@ step s = do let idx = stateIdx s opcode <- readAt s idx case opcode of - 1 -> increaseIdx <$> opcodeWith (+) s - 2 -> increaseIdx <$> opcodeWith (*) s + 1 -> increaseIdx <$> opcodeWith (+) s + 2 -> increaseIdx <$> opcodeWith (*) s 99 -> Left Exited - _ -> Left $ UnknownOpcode idx opcode + _ -> Left $ UnknownOpcode idx opcode opcodeWith :: (Int -> Int -> Int) -> State -> Either StepError State opcodeWith f s = do @@ -80,21 +82,22 @@ opcodeWith f s = do run :: State -> (State, StepError) run s = case step s of - Left e -> (s, e) + Left e -> (s, e) Right s' -> run s' 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