summaryrefslogtreecommitdiff
path: root/day11/day11.hs
blob: 158e951e353e0d76862f74d77e8fbbd9b4abf083 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import Data.List

type Stone = Int

atoi :: String -> Stone
atoi = read

maybeSplitDigs :: Stone -> Maybe (Stone, Stone)
maybeSplitDigs s
  | even ln = Just (s `divMod` md)
  | otherwise = Nothing
  where
    ln = ceiling $ logBase 10 $ fromIntegral s
    md = 10 ^ (ln `div` 2)

blink :: [Stone] -> [Stone] -> [Stone]
blink acc [] = reverse acc
blink acc (h:t)
  | h == 0 =
      blink (1:acc) t
  | h == 1 =
      blink (2024:acc) t
  | Just (left, right) <- maybeSplitDigs h =
      blink (right:left:acc) t
  | otherwise =
      blink (h * 2024:acc) t

consume :: String -> [Stone]
consume = map atoi . words

applyN :: Int -> (a -> a) -> a -> a
applyN n f = foldr (.) id (replicate n f)

part1 :: [Stone] -> Int
part1 = length . applyN 25 (blink [])

part2 :: [Stone] -> Int
part2 = length . applyN 75 (blink [])

main = do
  file <- readFile "day11.input"
  let stones = consume file
  putStrLn ("Part 1: " ++ (show $ part1 stones))
  --putStrLn ("Part 2: " ++ (show $ part2 stones))