-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #83 from ephemient/hs/day11
- Loading branch information
Showing
6 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{-# LANGUAGE MultiWayIf #-} | ||
|
||
-- | | ||
-- Module: Day11 | ||
-- Description: <https://adventofcode.com/2024/day/11 Day 11: Plutonian Pebbles> | ||
module Day11 (part1, part2, solve) where | ||
|
||
import Common (readEntire) | ||
import Data.IntMap (IntMap) | ||
import Data.IntMap qualified as IntMap (toList) | ||
import Data.IntMap.Strict qualified as IntMap (fromListWith) | ||
import Data.Text (Text) | ||
import Data.Text qualified as T (words) | ||
import Data.Text.Read qualified as T (decimal) | ||
|
||
part1, part2 :: Text -> Either String Int | ||
part1 = solve 25 | ||
part2 = solve 75 | ||
|
||
solve :: Int -> Text -> Either String Int | ||
solve n input = do | ||
nums <- mapM (readEntire T.decimal) $ T.words input | ||
pure $ foldl' (+) 0 $ iterate step (IntMap.fromListWith (+) $ (,1) <$> nums) !! n | ||
|
||
step :: IntMap Int -> IntMap Int | ||
step counts = IntMap.fromListWith (+) $ do | ||
(x, n) <- IntMap.toList counts | ||
if | ||
| x == 0 -> [(1, n)] | ||
| s <- show x, (l, 0) <- length s `divMod` 2, (a, b) <- splitAt l s -> [(read a, n), (read b, n)] | ||
| otherwise -> [(2024 * x, n)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module Day11Spec (spec) where | ||
|
||
import Data.Text (Text) | ||
import Day11 (solve) | ||
import Test.Hspec (Spec, describe, it, shouldBe) | ||
|
||
example1, example2 :: Text | ||
example1 = "0 1 10 99 999\n" | ||
example2 = "125 17\n" | ||
|
||
spec :: Spec | ||
spec = do | ||
describe "part 1" $ do | ||
it "examples" $ do | ||
solve 1 example1 `shouldBe` Right 7 | ||
solve 6 example2 `shouldBe` Right 22 | ||
solve 25 example2 `shouldBe` Right 55312 |