day6: Complete first puzzle
This commit is contained in:
parent
0a5ed079ac
commit
127d8a89cb
23
6/Parsing.hs
Normal file
23
6/Parsing.hs
Normal 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
33
6/fishexploder.hs
Normal 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
1
6/input.txt
Normal 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
|
Loading…
Reference in a new issue