diff --git a/6/Parsing.hs b/6/Parsing.hs new file mode 100644 index 0000000..8ad0f3f --- /dev/null +++ b/6/Parsing.hs @@ -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) diff --git a/6/fishexploder.hs b/6/fishexploder.hs new file mode 100644 index 0000000..f676ca2 --- /dev/null +++ b/6/fishexploder.hs @@ -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) diff --git a/6/input.txt b/6/input.txt new file mode 100644 index 0000000..3d35529 --- /dev/null +++ b/6/input.txt @@ -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