Skip to content

Commit

Permalink
Year 2016: Day 15
Browse files Browse the repository at this point in the history
  • Loading branch information
joshleaves committed Mar 19, 2024
1 parent 7a4e0e4 commit be01bc0
Show file tree
Hide file tree
Showing 8 changed files with 110 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)

## [2016.15.1]
### Added
- Solved [exercice for 2016, day 15](src/year_2016/day_15.rs).

## [2016.14.1]
### Added
- Solved [exercice for 2016, day 14](src/year_2016/day_14.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 = "2016.14.1"
version = "2016.15.1"
edition = "2021"
authors = ["Arnaud 'red' Rouyer"]
readme = "README.md"
Expand Down
20 changes: 20 additions & 0 deletions NOTES_2016.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,23 @@ Looking at it carefully proved very smart: both galenelias and me lost too much
## Day 10: Balance Bots

This exercise got me to face the worst part of Rust: iterating over a hashmap while mutating it.

## Day 11: Radioisotope Thermoelectric Generators

Ugh. I hated this just as much as the first time.

## Day 12: Leonardo's Monorail

Finally, we are back into interesting exercises!

## Day 13: A Maze of Twisty Little Cubicles

...

## Day 14: One-Time Pad

Computing MD5 hashes is never interesting.

## Day 15: Timing is Everything

Finally, something funny to do.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,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, up to day 14](NOTES_2016.md)
- [2016, up to day 15](NOTES_2016.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
16 changes: 15 additions & 1 deletion benches/year_2016.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use advent_rs::year_2016::day_11;
use advent_rs::year_2016::day_12;
use advent_rs::year_2016::day_13;
use advent_rs::year_2016::day_14;
use advent_rs::year_2016::day_15;
use criterion::{black_box, criterion_group, criterion_main, Criterion};

pub fn year_2016_day_01(c: &mut Criterion) {
Expand Down Expand Up @@ -182,6 +183,18 @@ pub fn year_2016_day_14(c: &mut Criterion) {
g2016_day_14.finish();
}

pub fn year_2016_day_15(c: &mut Criterion) {
let mut g2016_day_15 = c.benchmark_group("year_2016::day_15");
let input_year_2016_day_15 = include_str!("../inputs/year_2016/day_15_input");
g2016_day_15.bench_function("year_2016::day_15_v1", |b| {
b.iter(|| day_15::day_15_v1(black_box(input_year_2016_day_15)))
});
g2016_day_15.bench_function("year_2016::day_15_v2", |b| {
b.iter(|| day_15::day_15_v2(black_box(input_year_2016_day_14)))
});
g2016_day_15.finish();
}

criterion_group!(
benches,
year_2016_day_01,
Expand All @@ -197,6 +210,7 @@ criterion_group!(
year_2016_day_11,
year_2016_day_12,
year_2016_day_13,
year_2016_day_14
year_2016_day_14,
year_2016_day_15
);
criterion_main!(benches);
6 changes: 6 additions & 0 deletions inputs/year_2016/day_15_input
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Disc #1 has 13 positions; at time=0, it is at position 10.
Disc #2 has 17 positions; at time=0, it is at position 15.
Disc #3 has 19 positions; at time=0, it is at position 17.
Disc #4 has 7 positions; at time=0, it is at position 1.
Disc #5 has 5 positions; at time=0, it is at position 0.
Disc #6 has 3 positions; at time=0, it is at position 1.
9 changes: 9 additions & 0 deletions src/year_2016.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub mod day_11;
pub mod day_12;
pub mod day_13;
pub mod day_14;
pub mod day_15;

pub fn solve(day: u8, part: u8, input: impl Into<String>) -> Option<String> {
if part != 1 && part != 2 {
Expand All @@ -38,6 +39,7 @@ pub fn solve(day: u8, part: u8, input: impl Into<String>) -> Option<String> {
12 => Some(format!("{}", day_12::day_12(part, input))),
13 => Some(format!("{}", day_13::day_13(part, input))),
14 => Some(format!("{}", day_14::day_14(part, input))),
15 => Some(format!("{}", day_15::day_15(part, input))),
_ => None,
}
}
Expand Down Expand Up @@ -152,4 +154,11 @@ mod tests {
assert_eq!(day_14::day_14_v1(input), 25_427);
assert_eq!(day_14::day_14_v2(input), 22_045);
}

#[test]
fn day_15() {
let input = include_str!("../inputs/year_2016/day_15_input");
assert_eq!(day_15::day_15_v1(input), 203_660);
assert_eq!(day_15::day_15_v2(input), 2_408_135);
}
}
54 changes: 54 additions & 0 deletions src/year_2016/day_15.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
fn parse_disc(input: &str) -> (u32, u32, u32) {
let words: Vec<&str> = input.split_whitespace().collect();
let disc = words[1].strip_prefix('#').unwrap().parse::<u32>().unwrap();
let npos = words[3].parse::<u32>().unwrap();
let spos = words[11].strip_suffix('.').unwrap().parse::<u32>().unwrap();

(disc, npos, spos)
}

fn spin_the_discs(discs: Vec<(u32, u32, u32)>) -> u32 {
let mut period = 1;
let mut time = 0;
for (disc, npos, spos) in discs.iter() {
while ((time + disc + spos) % npos) != 0 {
time += period;
}
period *= npos;
}

time
}

pub fn day_15_v1(input: impl Into<String>) -> u32 {
let mut discs: Vec<(u32, u32, u32)> = vec![];
for line in input.into().lines() {
discs.push(parse_disc(line));
}

spin_the_discs(discs)
}

pub fn day_15_v2(input: impl Into<String>) -> u32 {
let mut discs: Vec<(u32, u32, u32)> = vec![];
for line in input.into().lines() {
discs.push(parse_disc(line));
}
discs.push(((discs.len() + 1) as u32, 11, 0));

spin_the_discs(discs)
}

solvable!(day_15, day_15_v1, day_15_v2, u32);

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

#[test]
fn works_with_samples_v1() {
let sample_one = "Disc #1 has 5 positions; at time=0, it is at position 4.\n\
Disc #2 has 2 positions; at time=0, it is at position 1.";
assert_eq!(day_15_v1(sample_one), 5);
}
}

0 comments on commit be01bc0

Please sign in to comment.