aboutsummaryrefslogtreecommitdiffstats
path: root/day06/main.hs
blob: fd310c091181b7f0ee5539d2fe5c07a7ccfbe816 (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
module Main where

import Data.Map ((!))
import qualified Data.Map as Map
import Data.Text (pack, split, unpack)
import Utils

e1 :: Int -> [Int] -> Int
e1 n fish
  | n == 0 = length fish
  | otherwise =
    let updatedNested = map (\n -> if n == 0 then [6, 8] else [n - 1]) fish
        updated = concat updatedNested
     in e1 (n - 1) updated

update :: Map.Map Int Int -> Int -> Int -> Int
update hmap 8 _ = hmap ! 0
update hmap 6 _ = (hmap ! 0) + (hmap ! 7)
update hmap n _ = hmap ! (n + 1)

e2 :: Int -> Map.Map Int Int -> Int
e2 n hmap
  | n == 0 = sum (Map.elems hmap)
  | otherwise =
    let newHmap = Map.mapWithKey (update hmap) hmap
     in e2 (n - 1) newHmap

main :: IO ()
main =
  do
    contents <- getContents
    let initial = contents $> pack .> split (== ',') .> map (unpack .> read) :: [Int]

    e1 80 initial $> print
    let hmapInit = Map.fromList [(k, v) | k <- [0 .. 8], v <- [0]]
    let hmap = foldr (\fish -> Map.insertWith (+) fish 1) hmapInit initial
    e2 256 hmap $> print