diff --git a/Day6.hs b/Day6.hs new file mode 100644 index 0000000..c7cee60 --- /dev/null +++ b/Day6.hs @@ -0,0 +1,23 @@ +module Day6 where +import System.IO (openFile, IOMode (ReadMode), hClose, hGetContents) +import Data.Text (splitOn, unpack, pack) +import Prelude hiding (splitAt) + +splitAt :: String -> String -> [String] +splitAt delimiter str = map unpack $ splitOn (pack delimiter) $ pack str + +grow :: Int -> [Int] +grow f + | f > 0 = [f - 1] + | otherwise = [6, 8] + +main :: IO () +main = do + input <- openFile "inputs/6.txt" ReadMode + contents <- hGetContents input + + let fishies = map read $ splitAt "," contents :: [Int] + + print . length . (!! 80) $ iterate (concatMap grow) fishies + + hClose input \ No newline at end of file diff --git a/Day6_1.hs b/Day6_1.hs new file mode 100644 index 0000000..c5d4e44 --- /dev/null +++ b/Day6_1.hs @@ -0,0 +1,30 @@ +module Day6_1 where +import System.IO (openFile, IOMode (ReadMode), hClose, hGetContents) +import Data.Text (splitOn, unpack, pack) +import Prelude hiding (splitAt) +import Data.List (group, sort) +import Data.Map.Strict (toList, fromListWith) + +splitAt :: String -> String -> [String] +splitAt delimiter str = map unpack $ splitOn (pack delimiter) $ pack str + +count :: [Int] -> [(Int, Int)] +count fish = map (\x -> (head x, length x)) $ group $ sort fish + +grow :: (Int, Int) -> [(Int, Int)] +grow fish + | fst fish > 0 = [(fst fish - 1, snd fish)] + | otherwise = [(6, snd fish), (8, snd fish)] + +sumFishes :: [(Int, Int)] -> [(Int, Int)] +sumFishes = toList . fromListWith (+) + +main :: IO () +main = do + input <- openFile "inputs/6.txt" ReadMode + contents <- hGetContents input + + let fishies = map read $ splitAt "," contents :: [Int] + print . sum . map snd . (!! 256) . iterate (sumFishes . concatMap grow) $ count fishies + + hClose input \ No newline at end of file