Day eleven
This commit is contained in:
28
day-11-haskell/src/Lib.hs
Normal file
28
day-11-haskell/src/Lib.hs
Normal file
@@ -0,0 +1,28 @@
|
||||
module Lib (task1, task2) where
|
||||
|
||||
import Control.Monad (join)
|
||||
import Flow
|
||||
|
||||
task1 :: String -> Int
|
||||
task1 input = parse input |> recurse blink 25 |> length
|
||||
|
||||
task2 :: String -> Int
|
||||
task2 input = parse input |> recurse blink 75 |> length
|
||||
|
||||
parse :: String -> [Int]
|
||||
parse input = words input |> map read
|
||||
|
||||
recurse :: (a -> a) -> Int -> a -> a
|
||||
recurse fun count start = iterate fun start !! count
|
||||
|
||||
blink :: [Int] -> [Int]
|
||||
blink stones = map blinkItem stones |> join
|
||||
|
||||
blinkItem :: Int -> [Int]
|
||||
blinkItem 0 = [1]
|
||||
blinkItem number = let digits = show number |> map (read . pure) :: [Int]
|
||||
in if length digits `mod` 2 == 0
|
||||
then case splitAt (length digits `div` 2) digits of
|
||||
(first, second) -> [map show first |> join |> read, map show second |> join |> read]
|
||||
else [number * 2024]
|
||||
|
||||
Reference in New Issue
Block a user