adventofcode-2021/6/fishexploder.hs

34 lines
776 B
Haskell

import Parsing (splitByString)
main :: IO ()
main = do
input <- getContents
let
fish = parseFish input
(putStrLn . show . solution1) fish
solution1 :: [Int] -> Int
solution1 = length . (simulateDays 80)
parseFish :: String -> [Int]
parseFish = (map read) . (splitByString ",")
simulateDays :: Int -> [Int] -> [Int]
simulateDays days fish = foldr (\f acc -> f acc) fish (replicate days tickDay)
tickDay :: [Int] -> [Int]
tickDay [] = []
tickDay (fish:otherFish)
| fish == 0 = 6:8:(tickDay otherFish)
| otherwise = (fish - 1):(tickDay otherFish)
-- Tests
testInput = "3,4,3,1,2"
testParsed = parseFish testInput
test1 = solution1 (parseFish testInput)
testTick = (tickDay . tickDay) testParsed
testSimulate = length (simulateDays 80 testParsed)