day6: Complete first puzzle

This commit is contained in:
Tristan Daniël Maat 2021-12-08 02:02:03 +00:00
parent 0a5ed079ac
commit 127d8a89cb
Signed by: tlater
GPG key ID: 49670FD774E43268
3 changed files with 57 additions and 0 deletions

23
6/Parsing.hs Normal file
View file

@ -0,0 +1,23 @@
module Parsing (
splitByString
) where
import Data.List (isPrefixOf)
splitByString :: String -> String -> [String]
splitByString _ "" = []
splitByString splitter string =
let (chunk, rest) = spanNextSplit string
in
chunk:(splitByString splitter rest)
where
spanNextSplit :: String -> (String, String)
spanNextSplit [] = ([], [])
spanNextSplit everything@(char:rest)
| splitter `isPrefixOf` rest =
([char], (drop ((length splitter) + 1) everything))
| otherwise =
let
(start, end) = spanNextSplit rest
in
(char:start, end)

33
6/fishexploder.hs Normal file
View file

@ -0,0 +1,33 @@
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)

1
6/input.txt Normal file
View file

@ -0,0 +1 @@
3,1,5,4,4,4,5,3,4,4,1,4,2,3,1,3,3,2,3,2,5,1,1,4,4,3,2,4,2,4,1,5,3,3,2,2,2,5,5,1,3,4,5,1,5,5,1,1,1,4,3,2,3,3,3,4,4,4,5,5,1,3,3,5,4,5,5,5,1,1,2,4,3,4,5,4,5,2,2,3,5,2,1,2,4,3,5,1,3,1,4,4,1,3,2,3,2,4,5,2,4,1,4,3,1,3,1,5,1,3,5,4,3,1,5,3,3,5,4,2,3,4,1,2,1,1,4,4,4,3,1,1,1,1,1,4,2,5,1,1,2,1,5,3,4,1,5,4,1,3,3,1,4,4,5,3,1,1,3,3,3,1,1,5,4,2,5,1,1,5,5,1,4,2,2,5,3,1,1,3,3,5,3,3,2,4,3,2,5,2,5,4,5,4,3,2,4,3,5,1,2,2,4,3,1,5,5,1,3,1,3,2,2,4,5,4,2,3,2,3,4,1,3,4,2,5,4,4,2,2,1,4,1,5,1,5,4,3,3,3,3,3,5,2,1,5,5,3,5,2,1,1,4,2,2,5,1,4,3,3,4,4,2,3,2,1,3,1,5,2,1,5,1,3,1,4,2,4,5,1,4,5,5,3,5,1,5,4,1,3,4,1,1,4,5,5,2,1,3,3