This commit is contained in:
Agatha Lovelace 2021-12-06 19:03:19 +02:00
parent 143abc7d8b
commit 5ad43785a1
2 changed files with 53 additions and 0 deletions

23
Day6.hs Normal file
View File

@ -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

30
Day6_1.hs Normal file
View File

@ -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