-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
72 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
2 72 8949 0 981038 86311 246 7636740 |
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,55 @@ | ||
{-# Language QuasiQuotes, ImportQualifiedPost #-} | ||
{-| | ||
Module : Main | ||
Description : Day 11 solution | ||
Copyright : (c) Eric Mertens, 2024 | ||
License : ISC | ||
Maintainer : [email protected] | ||
<https://adventofcode.com/2024/day/11> | ||
This solution runs efficiently by remembering how many of each stone | ||
there are and blinking all of that same kind of stone all at once. | ||
While the problem does state that order is preserved, the question | ||
it asks about the stones does not depend on order, so we forget that | ||
order! | ||
>>> :main + "125 17\n" | ||
55312 | ||
65601038650482 | ||
-} | ||
module Main (main) where | ||
|
||
import Advent (format, times) | ||
import Data.Map (Map) | ||
import Data.Map.Strict qualified as Map | ||
|
||
-- | >>> :main | ||
-- 202019 | ||
-- 239321955280205 | ||
main :: IO () | ||
main = | ||
do input <- [format|2024 11 %u& %n|] | ||
print (solve 25 input) | ||
print (solve 75 input) | ||
|
||
-- | Compute the number of stones resulting from a starting set of stones | ||
-- and a number of blink iterations. | ||
solve :: Int -> [Int] -> Int | ||
solve n input = sum (times n blinks (Map.fromListWith (+) [(i, 1) | i <- input])) | ||
|
||
-- | Blink all the stones at once. Stone numbers are mapped to multiplicity. | ||
blinks :: Map Int Int -> Map Int Int | ||
blinks stones = Map.fromListWith (+) [(stone', n) | (stone, n) <- Map.assocs stones, stone' <- blink stone] | ||
|
||
-- | Blink a single stone and figure out what stones it turns into. | ||
blink :: Int -> [Int] | ||
blink 0 = [1] -- 0 -> 1 | ||
blink n -- split in half if even length | ||
| let str = show n | ||
, let len = length str | ||
, even len | ||
, (l, r) <- splitAt (len `quot` 2) str | ||
= [read l, read r] | ||
blink n = [n * 2024] -- otherwise multiply by 2024 |