Skip to content

Commit

Permalink
Year 2017: Day 17
Browse files Browse the repository at this point in the history
  • Loading branch information
joshleaves committed Mar 30, 2024
1 parent d7c4452 commit 91d32b3
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ Of note:
- The changelog 2015.5.2 has been rewritten from each commit content.
- This file may be amended entirely in the future to adhere to the [GNU Changelog style](https://www.gnu.org/prep/standards/html_node/Style-of-Change-Logs.html#Style-of-Change-Logs)

## [2017.17.1]
### Added
- Solved [exercice for 2017, day 17](src/year_2017/17.rs).

## [2017.16.1]
### Added
- Solved [exercice for 2017, day 16](src/year_2017/16.rs).
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "advent-rs"
version = "2017.16.1"
version = "2017.17.1"
edition = "2021"
authors = ["Arnaud 'red' Rouyer"]
readme = "README.md"
Expand Down
4 changes: 4 additions & 0 deletions NOTES_2017.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,7 @@ The most interesting part of these exercises is often [all the obscure maths you
## Day 16: Permutation Promenade

Nothing hard, until you see how much ONE BILLION ITERATIONS actually is. Good news though: "All loops are about knowing when to stop". In that case, we can just...look for a repetition of the pattern.

## Day 17: Spinlock

As always, there's a trick to avoid looping too much: since `0` is ALWAYS at the 0th position, we just have to store the last number we are inserting (allegedly, no need to allocate a real Vec) after it.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ As I said, [Consistency is hard](https://github.com/joshleaves/advent-rb), and I
I'm also adding notes that may be useful if you're (like me) discovering Rust:
- [2015, complete!](NOTES_2015.md)
- [2016, complete!](NOTES_2016.md)
- [2017, up to day 16](NOTES_2017.md)
- [2017, up to day 17](NOTES_2017.md)

# Regarding style rules
I'm gonna use a mix of what `cargo fmt` does, with some stuff that feels more natural to me.
Expand Down
19 changes: 18 additions & 1 deletion benches/year_2017.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use advent_rs::year_2017::day_13;
use advent_rs::year_2017::day_14;
use advent_rs::year_2017::day_15;
use advent_rs::year_2017::day_16;
use advent_rs::year_2017::day_17;
use criterion::{black_box, criterion_group, criterion_main, Criterion};

fn year_2017_day_01(c: &mut Criterion) {
Expand Down Expand Up @@ -259,6 +260,21 @@ fn year_2017_day_16(c: &mut Criterion) {
g2017_day_16.finish();
}

fn year_2017_day_17(c: &mut Criterion) {
let input_day_17 = include_str!("../inputs/year_2017/day_17_input");
assert_eq!(day_17::day_17_v1(input_day_17), 1_173);
assert_eq!(day_17::day_17_v2(input_day_17), 1_930_815);

let mut g2017_day_17 = c.benchmark_group("year_2017::day_17");
g2017_day_17.bench_function("year_2017::day_17_v1", |b| {
b.iter(|| day_17::day_17_v1(black_box(input_day_17)))
});
g2017_day_17.bench_function("year_2017::day_17_v2", |b| {
b.iter(|| day_17::day_17_v2(black_box(input_day_17)))
});
g2017_day_17.finish();
}

criterion_group!(
benches,
year_2017_day_01,
Expand All @@ -276,6 +292,7 @@ criterion_group!(
year_2017_day_13,
year_2017_day_14,
year_2017_day_15,
year_2017_day_16
year_2017_day_16,
year_2017_day_17
);
criterion_main!(benches);
1 change: 1 addition & 0 deletions inputs/year_2017/day_17_input
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
304
9 changes: 9 additions & 0 deletions src/year_2017.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub mod day_13;
pub mod day_14;
pub mod day_15;
pub mod day_16;
pub mod day_17;

pub fn solve(day: u8, part: u8, input: impl Into<String>) -> Option<String> {
if part > 2 {
Expand All @@ -43,6 +44,7 @@ pub fn solve(day: u8, part: u8, input: impl Into<String>) -> Option<String> {
14 => Some(day_14::day_14(part, input).to_string()),
15 => Some(day_15::day_15(part, input).to_string()),
16 => Some(day_16::day_16(part, input).to_string()),
17 => Some(day_17::day_17(part, input).to_string()),
_ => None,
}
}
Expand Down Expand Up @@ -162,4 +164,11 @@ mod tests {
assert_eq!(day_16::day_16_v1(input), "lgpkniodmjacfbeh");
assert_eq!(day_16::day_16_v2(input), "hklecbpnjigoafmd");
}

#[test]
fn day_17() {
let input = include_str!("../inputs/year_2017/day_17_input");
assert_eq!(day_17::day_17_v1(input), 1_173);
assert_eq!(day_17::day_17_v2(input), 1_930_815);
}
}
38 changes: 38 additions & 0 deletions src/year_2017/day_17.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
pub fn day_17_v1(input: impl Into<String>) -> u32 {
let input = input.into().trim_end().parse::<u32>().unwrap();
let mut spinlock: Vec<u32> = vec![0];
let mut idx = 0;
for i in 1..=2017 {
idx = ((idx + input) % spinlock.len() as u32) + 1;
spinlock.insert(idx as usize, i);
}

idx = spinlock.iter().position(|n| *n == 2017).unwrap() as u32;
idx = (idx + 1) % spinlock.len() as u32;
spinlock[idx as usize]
}

pub fn day_17_v2(input: impl Into<String>) -> u32 {
let input = input.into().trim_end().parse::<u32>().unwrap();
let mut answer = 0;
let mut idx = 0;
for i in 1..=5_000_000 {
idx = (idx + input) % i;
if idx == 0 {
answer = i;
}
idx += 1;
}
answer
}
solvable!(day_17, day_17_v1, day_17_v2, u32);

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn works_with_samples_v1() {
assert_eq!(day_17_v1("3"), 638);
}
}

0 comments on commit 91d32b3

Please sign in to comment.