[hs] Migrate 2019_02
This commit is contained in:
parent
3e82db1f0c
commit
dddc5ec451
3 changed files with 27 additions and 16 deletions
|
|
@ -14,7 +14,10 @@ import Data.Char
|
||||||
import Data.Void
|
import Data.Void
|
||||||
|
|
||||||
import qualified Data.Text as T
|
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
|
||||||
import Text.Megaparsec.Char.Lexer (binary, decimal, float,
|
import Text.Megaparsec.Char.Lexer (binary, decimal, float,
|
||||||
hexadecimal, octal, scientific,
|
hexadecimal, octal, scientific,
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,10 @@ module Aoc.Y2019
|
||||||
|
|
||||||
import Aoc.Day
|
import Aoc.Day
|
||||||
import qualified Aoc.Y2019.D01 as D01
|
import qualified Aoc.Y2019.D01 as D01
|
||||||
|
import qualified Aoc.Y2019.D02 as D02
|
||||||
|
|
||||||
year :: Year
|
year :: Year
|
||||||
year = Year 2019
|
year = Year 2019
|
||||||
[ ( 1, D01.day)
|
[ ( 1, D01.day)
|
||||||
|
, ( 2, D02.day)
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -6,15 +6,17 @@
|
||||||
-- day requires an intcode machine, instead of maintaining a single global
|
-- day requires an intcode machine, instead of maintaining a single global
|
||||||
-- intcode machine implementation.
|
-- intcode machine implementation.
|
||||||
|
|
||||||
module Aoc.Y2019.A02
|
module Aoc.Y2019.D02
|
||||||
( solve201902
|
( day
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Control.Monad
|
import Control.Monad
|
||||||
import Data.Foldable
|
import Data.Foldable
|
||||||
|
|
||||||
import qualified Data.Map.Strict as M
|
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 }
|
newtype Memory = Memory { unmemory :: M.Map Int Int }
|
||||||
|
|
||||||
|
|
@ -53,7 +55,7 @@ data StepError
|
||||||
readAt :: State -> Int -> Either StepError Int
|
readAt :: State -> Int -> Either StepError Int
|
||||||
readAt s i = case readMem i $ stateMem s of
|
readAt s i = case readMem i $ stateMem s of
|
||||||
Nothing -> Left $ CouldNotRead i
|
Nothing -> Left $ CouldNotRead i
|
||||||
Just v -> Right v
|
Just v -> Right v
|
||||||
|
|
||||||
writeAt :: Int -> Int -> State -> State
|
writeAt :: Int -> Int -> State -> State
|
||||||
writeAt addr val s = s{stateMem = writeMem addr val $ stateMem s}
|
writeAt addr val s = s{stateMem = writeMem addr val $ stateMem s}
|
||||||
|
|
@ -63,10 +65,10 @@ step s = do
|
||||||
let idx = stateIdx s
|
let idx = stateIdx s
|
||||||
opcode <- readAt s idx
|
opcode <- readAt s idx
|
||||||
case opcode of
|
case opcode of
|
||||||
1 -> increaseIdx <$> opcodeWith (+) s
|
1 -> increaseIdx <$> opcodeWith (+) s
|
||||||
2 -> increaseIdx <$> opcodeWith (*) s
|
2 -> increaseIdx <$> opcodeWith (*) s
|
||||||
99 -> Left Exited
|
99 -> Left Exited
|
||||||
_ -> Left $ UnknownOpcode idx opcode
|
_ -> Left $ UnknownOpcode idx opcode
|
||||||
|
|
||||||
opcodeWith :: (Int -> Int -> Int) -> State -> Either StepError State
|
opcodeWith :: (Int -> Int -> Int) -> State -> Either StepError State
|
||||||
opcodeWith f s = do
|
opcodeWith f s = do
|
||||||
|
|
@ -80,21 +82,22 @@ opcodeWith f s = do
|
||||||
|
|
||||||
run :: State -> (State, StepError)
|
run :: State -> (State, StepError)
|
||||||
run s = case step s of
|
run s = case step s of
|
||||||
Left e -> (s, e)
|
Left e -> (s, e)
|
||||||
Right s' -> run s'
|
Right s' -> run s'
|
||||||
|
|
||||||
patch :: Int -> Int -> Memory -> Memory
|
patch :: Int -> Int -> Memory -> Memory
|
||||||
patch noun verb = writeMem 2 verb . writeMem 1 noun
|
patch noun verb = writeMem 2 verb . writeMem 1 noun
|
||||||
|
|
||||||
solve201902 :: FilePath -> IO ()
|
parser :: Parser Memory
|
||||||
solve201902 f = do
|
parser = newMem <$> (decimal `sepBy` char ',') <* newline
|
||||||
values <- map (read . T.unpack) . T.splitOn "," <$> T.readFile f
|
|
||||||
let mem = newMem values
|
|
||||||
|
|
||||||
|
solver :: Memory -> IO ()
|
||||||
|
solver mem = do
|
||||||
putStrLn ">> Part 1"
|
putStrLn ">> Part 1"
|
||||||
let (s1, _) = run $ newState $ patch 12 2 mem
|
let (s1, _) = run $ newState $ patch 12 2 mem
|
||||||
putStrLn $ "Value at position 0: " <> show (readMem 0 $ stateMem s1)
|
putStrLn $ "Value at position 0: " <> show (readMem 0 $ stateMem s1)
|
||||||
|
|
||||||
|
putStrLn ""
|
||||||
putStrLn ">> Part 2"
|
putStrLn ">> Part 2"
|
||||||
let attempts = [(noun, verb) | noun <- [0..99], verb <- [0..99]]
|
let attempts = [(noun, verb) | noun <- [0..99], verb <- [0..99]]
|
||||||
for_ attempts $ \(noun, verb) -> do
|
for_ attempts $ \(noun, verb) -> do
|
||||||
|
|
@ -102,3 +105,6 @@ solve201902 f = do
|
||||||
Just result = readMem 0 $ stateMem s2
|
Just result = readMem 0 $ stateMem s2
|
||||||
when (result == 19690720) $
|
when (result == 19690720) $
|
||||||
putStrLn $ "100 * noun + verb = " <> show (100 * noun + verb)
|
putStrLn $ "100 * noun + verb = " <> show (100 * noun + verb)
|
||||||
|
|
||||||
|
day :: Day
|
||||||
|
day = dayParse parser solver
|
||||||
Loading…
Add table
Add a link
Reference in a new issue