aboutsummaryrefslogtreecommitdiff
path: root/execs/Day15.hs
blob: cd498fb497e2a2cde63d82e80f134391f0c26614 (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.Maybe
import Data.List.Split
import qualified Data.Map as M

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