Day eleven, but correct
This commit is contained in:
@@ -26,6 +26,7 @@ library
|
|||||||
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints
|
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints
|
||||||
build-depends:
|
build-depends:
|
||||||
base >=4.7 && <5
|
base >=4.7 && <5
|
||||||
|
, containers
|
||||||
, flow
|
, flow
|
||||||
default-language: Haskell2010
|
default-language: Haskell2010
|
||||||
|
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ library:
|
|||||||
source-dirs: src
|
source-dirs: src
|
||||||
dependencies:
|
dependencies:
|
||||||
- flow
|
- flow
|
||||||
|
- containers
|
||||||
|
|
||||||
executables:
|
executables:
|
||||||
day11-haskell-exe:
|
day11-haskell-exe:
|
||||||
|
|||||||
@@ -1,13 +1,14 @@
|
|||||||
module Lib (task1, task2) where
|
module Lib (task1, task2) where
|
||||||
|
|
||||||
import Control.Monad (join)
|
import Control.Monad (join)
|
||||||
|
import qualified Data.Map.Strict as Map
|
||||||
import Flow
|
import Flow
|
||||||
|
|
||||||
task1 :: String -> Int
|
task1 :: String -> Int
|
||||||
task1 input = parse input |> recurse blink 25 |> length
|
task1 input = parse input |> map (\stone -> (stone, 1)) |> recurse blink 25 |> map (\(_, cnt) -> cnt) |> sum
|
||||||
|
|
||||||
task2 :: String -> Int
|
task2 :: String -> Int
|
||||||
task2 input = parse input |> recurse blink 75 |> length
|
task2 input = parse input |> map (\stone -> (stone, 1)) |> recurse blink 75 |> map (\(_, cnt) -> cnt) |> sum
|
||||||
|
|
||||||
parse :: String -> [Int]
|
parse :: String -> [Int]
|
||||||
parse input = words input |> map read
|
parse input = words input |> map read
|
||||||
@@ -15,14 +16,15 @@ parse input = words input |> map read
|
|||||||
recurse :: (a -> a) -> Int -> a -> a
|
recurse :: (a -> a) -> Int -> a -> a
|
||||||
recurse fun count start = iterate fun start !! count
|
recurse fun count start = iterate fun start !! count
|
||||||
|
|
||||||
blink :: [Int] -> [Int]
|
blink :: [(Int, Int)] -> [(Int, Int)]
|
||||||
blink stones = map blinkItem stones |> join
|
blink stones = map (\(stone, mult) -> blinkItem stone |> map (\s -> (s, mult))) stones |> join |> Map.fromListWith (+) |> Map.toList
|
||||||
|
|
||||||
blinkItem :: Int -> [Int]
|
blinkItem :: Int -> [Int]
|
||||||
blinkItem 0 = [1]
|
blinkItem 0 = [1]
|
||||||
blinkItem number = let digits = show number |> map (read . pure) :: [Int]
|
blinkItem number = let str = show number
|
||||||
in if length digits `mod` 2 == 0
|
in if length str `mod` 2 == 0
|
||||||
then case splitAt (length digits `div` 2) digits of
|
then case splitAt (length str `div` 2) str of
|
||||||
(first, second) -> [map show first |> join |> read, map show second |> join |> read]
|
(first, second) -> [read first, read second]
|
||||||
else [number * 2024]
|
else [number * 2024]
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user