From 4c620530e2a25f8d372f0f4357b917f78517879d Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Wed, 8 Dec 2021 16:19:21 +0200 Subject: Day 06 --- day06/input.txt | 1 + day06/main.hs | 37 +++++++++++++++++++++++++++++++++++++ day06/test.txt | 1 + 3 files changed, 39 insertions(+) create mode 100644 day06/input.txt create mode 100644 day06/main.hs create mode 100644 day06/test.txt diff --git a/day06/input.txt b/day06/input.txt new file mode 100644 index 0000000..715ea56 --- /dev/null +++ b/day06/input.txt @@ -0,0 +1 @@ +1,4,1,1,1,1,1,1,1,4,3,1,1,3,5,1,5,3,2,1,1,2,3,1,1,5,3,1,5,1,1,2,1,2,1,1,3,1,5,1,1,1,3,1,1,1,1,1,1,4,5,3,1,1,1,1,1,1,2,1,1,1,1,4,4,4,1,1,1,1,5,1,2,4,1,1,4,1,2,1,1,1,2,1,5,1,1,1,3,4,1,1,1,3,2,1,1,1,4,1,1,1,5,1,1,4,1,1,2,1,4,1,1,1,3,1,1,1,1,1,3,1,3,1,1,2,1,4,1,1,1,1,3,1,1,1,1,1,1,2,1,3,1,1,1,1,4,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,5,1,1,1,2,2,1,1,3,5,1,1,1,1,3,1,3,3,1,1,1,1,3,5,2,1,1,1,1,5,1,1,1,1,1,1,1,2,1,2,1,1,1,2,1,1,1,1,1,2,1,1,1,1,1,5,1,4,3,3,1,3,4,1,1,1,1,1,1,1,1,1,1,4,3,5,1,1,1,1,1,1,1,1,1,1,1,1,1,5,2,1,4,1,1,1,1,1,1,1,1,1,1,1,1,1,5,1,1,1,1,1,1,1,1,2,1,4,4,1,1,1,1,1,1,1,5,1,1,2,5,1,1,4,1,3,1,1 diff --git a/day06/main.hs b/day06/main.hs new file mode 100644 index 0000000..fd310c0 --- /dev/null +++ b/day06/main.hs @@ -0,0 +1,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 diff --git a/day06/test.txt b/day06/test.txt new file mode 100644 index 0000000..55129f1 --- /dev/null +++ b/day06/test.txt @@ -0,0 +1 @@ +3,4,3,1,2 -- cgit v1.3