Metadata
-
Date
-
Tagged
-
Part of series
- Advent of Code 2024 Day 1
- Advent of Code 2024 Day 2
- Advent of Code 2024 Day 3
- Advent of Code 2024 Day 4
- Advent of Code 2024 Day 5
- Advent of Code 2024 Day 6
- Advent of Code 2024 Day 7
- Advent of Code 2024 Day 8
- Advent of Code 2024 Day 9
- Advent of Code 2024 Day 10
- Advent of Code 2024 Day 11
- Advent of Code 2024 Day 12
- Advent of Code 2024 Day 13
- Advent of Code 2024 Day 14
- Advent of Code 2024 Day 15
- Advent of Code 2024 Day 16
- Advent of Code 2024 Day 17
- Advent of Code 2024 Day 18
- Advent of Code 2024 Day 19
- Advent of Code 2024 Day 20
- Advent of Code 2024 Day 21
- Advent of Code 2024 Day 22
- Advent of Code 2024 Day 23
- Advent of Code 2024 Day 24
- Advent of Code 2024 Day 25
-
Older post
-
Newer post
Advent of Code 2024 Day 22
Day 22: Keypad Conundrum
https://adventofcode.com/2024/day/22
Another day, another familiar … adversary.
A monkey stole the device.
To trade it back, aqcuire currency bananas from the local market.
Every buyer on the market sets their prices seemingly random, but they’re not really, they’re pseudorandom. And a sneaky historian hands you the list of seed numbers for the pseudorandom number generator.
An example input looks like this:
The pseudorandom generation goes like this:
- Number is set to the result of multiplying the secret number by 64.
- Number is set to the result of dividing the secret number by 32. Round the result down to the nearest integer.
- Number is set to the result of multiplying the secret number by 2048.
Between each step, mix the step result into the previous number, then prune the number.
- mixing: step result XOR number
- prune: mix result modulo 16777216
Not bothering with a parsing function today, each line is a number.
Part 1
The question asks for the sum of the 2000th secret number for all buyers.
A “read the instructions and code them” question, nothing special.
Part 2
You figured out what every buyer’s secret numbers are going to be. That means you know what their prices are going to be.
A price is the last digit of a secret number.
You’ve got stuff to sell, and want as much bananas as possible.
You instruct a monkey to sell one thing when it sees a specific sequence of 4 consecutive differences in price. This sequence is the same for every buyer, and the monkey only sells the first time it sees the sequence (or not at all if it doesn’t see the sequence).
The question asks what the most bananas you can get is.
The code builds up a map that maps a specific sequence to an amount of bananas it gets from all buyers combined. The answer to the question is the maximum value in that map.
For each buyer:
- Generate 2000 prices (the last digit of each pseudorandom number).
- Looks through sliding windows of 5 numbers (so 4 differences). This produces a sequence of differences, and a price at that sequence. The first time that specific sequence is seen, put it into the map.