Day 6
This commit is contained in:
parent
143abc7d8b
commit
5ad43785a1
|
@ -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
|
|
@ -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
|
Loading…
Reference in New Issue