aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-12-15 13:51:04 +0000
committerAkshay <[email protected]>2020-12-15 13:51:04 +0000
commit38f3a717b0f8095fb05b9be0bebd158474eb7618 (patch)
tree4e2f2a58e5c3780138bbe66b79ebe57f0b524c1a
parent18821356ad629cdcd68c88ad4e2e664fd3a232a8 (diff)
add day15
-rw-r--r--aoc.cabal6
-rw-r--r--execs/Day14.hs9
-rw-r--r--execs/Day15.hs21
-rw-r--r--input/151
4 files changed, 33 insertions, 4 deletions
diff --git a/aoc.cabal b/aoc.cabal
index ad67f92..2d1d976 100644
--- a/aoc.cabal
+++ b/aoc.cabal
@@ -105,3 +105,9 @@ executable Day14
105 build-depends: base, aoc, containers, parsec, strings 105 build-depends: base, aoc, containers, parsec, strings
106 default-language: Haskell2010 106 default-language: Haskell2010
107 hs-source-dirs: execs 107 hs-source-dirs: execs
108
109executable Day15
110 main-is: Day15.hs
111 build-depends: base, aoc, containers, split
112 default-language: Haskell2010
113 hs-source-dirs: execs
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
diff --git a/input/15 b/input/15
new file mode 100644
index 0000000..134afd4
--- /dev/null
+++ b/input/15
@@ -0,0 +1 @@
0,20,7,16,1,18,15