Skip to content

Commit

Permalink
Year 2016: Day 19
Browse files Browse the repository at this point in the history
  • Loading branch information
joshleaves committed Mar 21, 2024
1 parent 0ce6297 commit 05ab0f5
Show file tree
Hide file tree
Showing 8 changed files with 82 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.19.1]
### Added
- Solved [exercice for 2016, day 19](src/year_2016/19.rs).

## [2016.18.1]
### Added
- Solved [exercice for 2016, day 18](src/year_2016/18.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.18.1"
version = "2016.19.1"
edition = "2021"
authors = ["Arnaud 'red' Rouyer"]
readme = "README.md"
Expand Down
4 changes: 4 additions & 0 deletions NOTES_2016.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,7 @@ There was some confusion between all the generics, abstractions, and types, espe
## Day 18: Like a Rogue

Easy come, easy go.

## Day 19: An Elephant Named Joseph

As easy in Rust as it was in Ruby.
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 18](NOTES_2016.md)
- [2016, up to day 19](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 @@ -16,6 +16,7 @@ use advent_rs::year_2016::day_15;
use advent_rs::year_2016::day_16;
use advent_rs::year_2016::day_17;
use advent_rs::year_2016::day_18;
use advent_rs::year_2016::day_19;
use criterion::{black_box, criterion_group, criterion_main, Criterion};

pub fn year_2016_day_01(c: &mut Criterion) {
Expand Down Expand Up @@ -234,6 +235,18 @@ pub fn year_2016_day_18(c: &mut Criterion) {
g2016_day_18.finish();
}

pub fn year_2016_day_19(c: &mut Criterion) {
let mut g2016_day_19 = c.benchmark_group("year_2016::day_19");
let input_year_2016_day_19 = include_str!("../inputs/year_2016/day_19_input");
g2016_day_19.bench_function("year_2016::day_19_v1", |b| {
b.iter(|| day_19::day_19_v1(black_box(input_year_2016_day_19)))
});
g2016_day_19.bench_function("year_2016::day_19_v2", |b| {
b.iter(|| day_19::day_19_v2(black_box(input_year_2016_day_19)))
});
g2016_day_19.finish();
}

criterion_group!(
benches,
year_2016_day_01,
Expand All @@ -253,6 +266,7 @@ criterion_group!(
year_2016_day_15,
year_2016_day_16,
year_2016_day_17,
year_2016_day_18
year_2016_day_18,
year_2016_day_19
);
criterion_main!(benches);
1 change: 1 addition & 0 deletions inputs/year_2016/day_19_input
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3012210
10 changes: 10 additions & 0 deletions src/year_2016.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub mod day_15;
pub mod day_16;
pub mod day_17;
pub mod day_18;
pub mod day_19;

pub fn solve(day: u8, part: u8, input: impl Into<String>) -> Option<String> {
if part != 1 && part != 2 {
Expand All @@ -45,6 +46,8 @@ pub fn solve(day: u8, part: u8, input: impl Into<String>) -> Option<String> {
15 => Some(format!("{}", day_15::day_15(part, input))),
16 => Some(format!("{}", day_16::day_16(part, input))),
17 => Some(format!("{}", day_17::day_17(part, input))),
18 => Some(format!("{}", day_18::day_18(part, input))),
19 => Some(format!("{}", day_19::day_19(part, input))),
_ => None,
}
}
Expand Down Expand Up @@ -187,4 +190,11 @@ mod tests {
assert_eq!(day_18::day_18_v1(input), 1926);
assert_eq!(day_18::day_18_v2(input), 19_986_699);
}

#[test]
fn day_19() {
let input = include_str!("../inputs/year_2016/day_19_input");
assert_eq!(day_19::day_19_v1(input), 1_830_117);
assert_eq!(day_19::day_19_v2(input), 1_417_887);
}
}
46 changes: 46 additions & 0 deletions src/year_2016/day_19.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use itertools::Itertools;
use std::collections::VecDeque;

pub fn day_19_v1(input: impl Into<String>) -> u32 {
let number = input.into().parse::<u32>().unwrap();
let mut binary = format!("{number:b}").chars().collect_vec();
binary.rotate_left(1);

u32::from_str_radix(&binary.iter().collect::<String>(), 2).unwrap()
}

pub fn day_19_v2(input: impl Into<String>) -> u32 {
let number = input.into().parse::<u32>().unwrap();
let mid = (number + 1) / 2;
let mut arr_v1: VecDeque<u32> = (1..mid).collect();
let mut arr_v2: VecDeque<u32> = (mid..(number + 1)).collect();
loop {
if arr_v2.len() >= arr_v1.len() {
arr_v2.pop_front();
if arr_v2.is_empty() {
return arr_v1[0];
}
} else {
arr_v1.pop_back();
}
arr_v1.push_back(arr_v2.pop_front().unwrap());
arr_v2.push_back(arr_v1.pop_front().unwrap());
}
}

solvable!(day_19, day_19_v1, day_19_v2, u32);

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

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

#[test]
fn works_with_samples_v2() {
assert_eq!(day_19_v2("5"), 2);
}
}

0 comments on commit 05ab0f5

Please sign in to comment.