Solutions for 2022, as well as 2015-2018 and 2019 up to day 11
This commit is contained in:
commit
1895197c49
722 changed files with 375457 additions and 0 deletions
8
2017/day17-spinlock/Cargo.toml
Normal file
8
2017/day17-spinlock/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "day17-spinlock"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
59
2017/day17-spinlock/challenge.txt
Normal file
59
2017/day17-spinlock/challenge.txt
Normal file
|
@ -0,0 +1,59 @@
|
|||
\--- Day 17: Spinlock ---
|
||||
----------
|
||||
|
||||
Suddenly, whirling in the distance, you notice what looks like a massive, pixelated hurricane: a deadly [spinlock](https://en.wikipedia.org/wiki/Spinlock). This spinlock isn't just consuming computing power, but memory, too; vast, digital mountains are being ripped from the ground and consumed by the vortex.
|
||||
|
||||
If you don't move quickly, fixing that printer will be the least of your problems.
|
||||
|
||||
This spinlock's algorithm is simple but efficient, quickly consuming everything in its path. It starts with a circular buffer containing only the value `0`, which it marks as the *current position*. It then steps forward through the circular buffer some number of steps (your puzzle input) before inserting the first new value, `1`, after the value it stopped on. The inserted value becomes the *current position*. Then, it steps forward from there the same number of steps, and wherever it stops, inserts after it the second new value, `2`, and uses that as the new *current position* again.
|
||||
|
||||
It repeats this process of *stepping forward*, *inserting a new value*, and *using the location of the inserted value as the new current position* a total of `*2017*` times, inserting `2017` as its final operation, and ending with a total of `2018` values (including `0`) in the circular buffer.
|
||||
|
||||
For example, if the spinlock were to step `3` times per insert, the circular buffer would begin to evolve like this (using parentheses to mark the current position after each iteration of the algorithm):
|
||||
|
||||
* `(0)`, the initial state before any insertions.
|
||||
* `0 (1)`: the spinlock steps forward three times (`0`, `0`, `0`), and then inserts the first value, `1`, after it. `1` becomes the current position.
|
||||
* `0 (2) 1`: the spinlock steps forward three times (`0`, `1`, `0`), and then inserts the second value, `2`, after it. `2` becomes the current position.
|
||||
* `0 2 (3) 1`: the spinlock steps forward three times (`1`, `0`, `2`), and then inserts the third value, `3`, after it. `3` becomes the current position.
|
||||
|
||||
And so on:
|
||||
|
||||
* `0 2 (4) 3 1`
|
||||
* `0 (5) 2 4 3 1`
|
||||
* `0 5 2 4 3 (6) 1`
|
||||
* `0 5 (7) 2 4 3 6 1`
|
||||
* `0 5 7 2 4 3 (8) 6 1`
|
||||
* `0 (9) 5 7 2 4 3 8 6 1`
|
||||
|
||||
Eventually, after 2017 insertions, the section of the circular buffer near the last insertion looks like this:
|
||||
|
||||
```
|
||||
1512 1134 151 (2017) 638 1513 851
|
||||
```
|
||||
|
||||
Perhaps, if you can identify the value that will ultimately be *after* the last value written (`2017`), you can short-circuit the spinlock. In this example, that would be `638`.
|
||||
|
||||
*What is the value after `2017`* in your completed circular buffer?
|
||||
|
||||
Your puzzle answer was `1244`.
|
||||
|
||||
\--- Part Two ---
|
||||
----------
|
||||
|
||||
The spinlock does not short-circuit. Instead, it gets *more* angry. At least, you assume that's what happened; it's spinning significantly faster than it was a moment ago.
|
||||
|
||||
You have good news and bad news.
|
||||
|
||||
The good news is that you have improved calculations for how to stop the spinlock. They indicate that you actually need to identify *the value after `0`* in the current state of the circular buffer.
|
||||
|
||||
The bad news is that while you were determining this, the spinlock has just finished inserting its fifty millionth value (`50000000`).
|
||||
|
||||
*What is the value after `0`* the moment `50000000` is inserted?
|
||||
|
||||
Your puzzle answer was `11162912`.
|
||||
|
||||
Both parts of this puzzle are complete! They provide two gold stars: \*\*
|
||||
|
||||
At this point, all that is left is for you to [admire your Advent calendar](/2017).
|
||||
|
||||
Your puzzle input was `370`.
|
34
2017/day17-spinlock/src/lib.rs
Normal file
34
2017/day17-spinlock/src/lib.rs
Normal file
|
@ -0,0 +1,34 @@
|
|||
pub fn run(step: usize) -> (usize, usize) {
|
||||
let mut buffer = Vec::from([0]);
|
||||
let mut current_position = 0;
|
||||
for iteration in 1..=2017 {
|
||||
current_position = (current_position + step) % iteration + 1;
|
||||
buffer.insert(current_position, iteration);
|
||||
}
|
||||
let first = buffer[(current_position+1) % 2018];
|
||||
let mut second = buffer[1];
|
||||
for iteration in 2018..=50_000_000 {
|
||||
current_position = (current_position + step) % iteration + 1;
|
||||
if current_position == 1 {
|
||||
second = iteration
|
||||
}
|
||||
}
|
||||
(first, second)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_sample() {
|
||||
let sample_input = 3;
|
||||
assert_eq!(run(sample_input), (638, 1222153));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_challenge() {
|
||||
let challenge_input = 370;
|
||||
assert_eq!(run(challenge_input), (1244, 11162912));
|
||||
}
|
||||
}
|
0
2017/day17-spinlock/tests/challenge_input
Normal file
0
2017/day17-spinlock/tests/challenge_input
Normal file
0
2017/day17-spinlock/tests/sample_input
Normal file
0
2017/day17-spinlock/tests/sample_input
Normal file
Loading…
Add table
Add a link
Reference in a new issue