Skip to content

Commit

Permalink
Initial day 22
Browse files Browse the repository at this point in the history
  • Loading branch information
mstksg committed Dec 22, 2024
1 parent e95050d commit 297aa73
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 9 deletions.
64 changes: 55 additions & 9 deletions 2024/AOC2024/Day22.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
-- solution. You can delete the type signatures completely and GHC
-- will recommend what should go in place of the underscores.
module AOC2024.Day22 (
-- day22a,
-- day22b

day22a,
day22b,
)
where

Expand All @@ -42,6 +41,7 @@ import qualified Data.OrdPSQ as PSQ
import qualified Data.Sequence as Seq
import qualified Data.Sequence.NonEmpty as NESeq
import qualified Data.Set as S
import Data.Bits
import qualified Data.Set.NonEmpty as NES
import qualified Data.Text as T
import qualified Data.Vector as V
Expand All @@ -53,21 +53,67 @@ import qualified Text.Megaparsec.Char.Lexer as PP
day22a :: _ :~> _
day22a =
MkSol
{ sParse =
noFail $
lines
{ sParse = parseMaybe' $
sepByLines pDecimal
-- noFail $
-- lines
, sShow = show
, sSolve =
noFail $
id
sum . map ((!! 2000) . iterate step)
}

step :: Int -> Int
step n = n'''
where
n' = prune $ (n*64) `xor` n
n'' = prune $ (n' `div` 32) `xor` n'
n''' = prune $ (n'' * 2048) `xor` n''
prune = (`mod` 16777216)

-- - Calculate the result of *multiplying the secret number by `64`*.
-- Then, *mix* this result into the secret number. Finally, *prune* the
-- secret number.
-- - Calculate the result of *dividing the secret number by `32`*. Round
-- the result down to the nearest integer. Then, *mix* this result into
-- the secret number. Finally, *prune* the secret number.
-- - Calculate the result of *multiplying the secret number by `2048`*.
-- Then, *mix* this result into the secret number. Finally, *prune* the
-- secret number.

-- Each step of the above process involves *mixing* and *pruning*:

-- - To *mix* a value into the secret number, calculate the [bitwise
-- XOR](https://en.wikipedia.org/wiki/Bitwise_operation#XOR){target="_blank"}
-- of the given value and the secret number. Then, the secret number
-- becomes the result of that operation. (If the secret number is `42`
-- and you were to *mix* `15` into the secret number, the secret number
-- would become `37`.)
-- - To *prune* the secret number, calculate the value of the secret
-- number
-- [modulo](https://en.wikipedia.org/wiki/Modulo){target="_blank"}
-- `16777216`. Then, the secret number becomes the result of that
-- operation. (If the secret number is `100000000` and you were to
-- *prune* the secret number, the secret number would become
-- `16113920`.)

day22b :: _ :~> _
day22b =
MkSol
{ sParse = sParse day22a
, sShow = show
, sSolve =
noFail $
id
noFail $ \xs ->
let serieses = xs <&> \x ->
let ps = take 2000 $ map (`mod` 10) $ iterate step x
dPs = zipWith (\p0 p1 -> (p1, p1 - p0)) ps (drop 1 ps)
windows = slidingWindows 4 dPs <&> \w -> (snd <$> w, fst $ last (toList w))
seqMap = M.fromListWith (flip const) windows
in seqMap
bests = M.unionsWith (+) serieses
in snd $ maximumBy (comparing snd) (M.toList bests)
-- bests = M.unionsWith (<>) $ map (fmap (:[])) serieses
-- in maximumBy (comparing (sum . snd)) (M.toList bests)
}
where
diffs xs = zipWith subtract xs (drop 1 xs)
5 changes: 5 additions & 0 deletions test-data/2024/22a.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
1
10
100
2024
>>> 37327623
5 changes: 5 additions & 0 deletions test-data/2024/22b.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
1
2
3
2024
>>> 23

0 comments on commit 297aa73

Please sign in to comment.