aboutsummaryrefslogtreecommitdiff
path: root/execs/Day15.hs
blob: c54e3311ceabab27fbf0ede98f6fcba357dcc078 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
module Main where

import           Data.List.Split
import qualified Data.Map        as M
import           Data.Maybe

run :: [Int] -> Int -> [Int] -> Int
run ls start input = fst $ foldl fn (start, startMap) ls
    where startMap = M.fromList $ zip input [1..]
          fn (last, seen) i = (i - last', seen')
              where last' = fromMaybe i (M.lookup last seen)
                    seen' = M.insert last i seen

main :: IO ()
main = do
    n <- map read . splitOn "," <$> readFile "input/15"

    -- holy off-by-one errors
    print $ run [8..2020 - 1] 0 n
    print $ run [8..30000000 - 1] 0 n