diff options
Diffstat (limited to 'execs')
-rw-r--r-- | execs/Day14.hs | 9 | ||||
-rw-r--r-- | execs/Day15.hs | 21 |
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 | |||
5 | import Text.Parsec.Char | 5 | import Text.Parsec.Char |
6 | import Data.Either | 6 | import Data.Either |
7 | import Data.Char | 7 | import Data.Char |
8 | import Data.Bifunctor (bimap) | ||
8 | import Data.Strings | 9 | import Data.Strings |
9 | import Numeric (showIntAtBase, readInt) | 10 | import Numeric (showIntAtBase, readInt) |
10 | import Data.Map (Map) | 11 | import Data.Map (Map) |
@@ -15,11 +16,11 @@ data Stmt = Mask String | Mem Int Int deriving Show | |||
15 | parseMask :: Parser Stmt | 16 | parseMask :: Parser Stmt |
16 | parseMask = string "mask = " >> Mask <$> many anyChar | 17 | parseMask = string "mask = " >> Mask <$> many anyChar |
17 | 18 | ||
19 | parseNumber :: Parser Int | ||
20 | parseNumber = read <$> many1 digit | ||
21 | |||
18 | parseMem :: Parser Stmt | 22 | parseMem :: Parser Stmt |
19 | parseMem = do | 23 | parseMem = 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 | ||
24 | parseLine :: Parser Stmt | 25 | parseLine :: Parser Stmt |
25 | parseLine = try parseMask <|> parseMem | 26 | parseLine = 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 @@ | |||
1 | module Main where | ||
2 | |||
3 | import Data.Maybe | ||
4 | import Data.List.Split | ||
5 | import qualified Data.Map as M | ||
6 | |||
7 | run :: [Int] -> Int -> [Int] -> Int | ||
8 | run 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 | |||
14 | main :: IO () | ||
15 | main = 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 | |||