aboutsummaryrefslogtreecommitdiff
path: root/execs
diff options
context:
space:
mode:
Diffstat (limited to 'execs')
-rw-r--r--execs/Day14.hs9
-rw-r--r--execs/Day15.hs21
2 files changed, 26 insertions, 4 deletions
diff --git a/execs/Day14.hs b/execs/Day14.hs
index d82424a..7f4368a 100644
--- a/execs/Day14.hs
+++ b/execs/Day14.hs
@@ -5,6 +5,7 @@ import Text.ParserCombinators.Parsec
5import Text.Parsec.Char 5import Text.Parsec.Char
6import Data.Either 6import Data.Either
7import Data.Char 7import Data.Char
8import Data.Bifunctor (bimap)
8import Data.Strings 9import Data.Strings
9import Numeric (showIntAtBase, readInt) 10import Numeric (showIntAtBase, readInt)
10import Data.Map (Map) 11import Data.Map (Map)
@@ -15,11 +16,11 @@ data Stmt = Mask String | Mem Int Int deriving Show
15parseMask :: Parser Stmt 16parseMask :: Parser Stmt
16parseMask = string "mask = " >> Mask <$> many anyChar 17parseMask = string "mask = " >> Mask <$> many anyChar
17 18
19parseNumber :: Parser Int
20parseNumber = read <$> many1 digit
21
18parseMem :: Parser Stmt 22parseMem :: Parser Stmt
19parseMem = do 23parseMem = Mem <$ string "mem[" <*> parseNumber <* string "] = " <*> parseNumber
20 one <- string "mem[" >> many1 digit
21 two <- string "] = " >> many1 digit
22 return $ Mem (read one) (read two)
23 24
24parseLine :: Parser Stmt 25parseLine :: Parser Stmt
25parseLine = try parseMask <|> parseMem 26parseLine = try parseMask <|> parseMem
diff --git a/execs/Day15.hs b/execs/Day15.hs
new file mode 100644
index 0000000..cd498fb
--- /dev/null
+++ b/execs/Day15.hs
@@ -0,0 +1,21 @@
1module Main where
2
3import Data.Maybe
4import Data.List.Split
5import qualified Data.Map as M
6
7run :: [Int] -> Int -> [Int] -> Int
8run ls start input = fst $ foldl fn (start, startMap) ls
9 where startMap = M.fromList $ zip input [1..]
10 fn (last, seen) i = (i - last', seen')
11 where last' = fromMaybe i (M.lookup last seen)
12 seen' = M.insert last i seen
13
14main :: IO ()
15main = do
16 n <- map read . splitOn "," <$> readFile "input/15"
17
18 -- holy off-by-one errors
19 print $ run [8..2020 - 1] 0 n
20 print $ run [8..30000000 - 1] 0 n
21