From e9dac7071305cf985aa74b4f475d0d0ae9d51738 Mon Sep 17 00:00:00 2001 From: red Date: Fri, 15 Mar 2024 07:22:57 +0100 Subject: [PATCH] Year 2016: Day 02 --- CHANGELOG.md | 6 ++ Cargo.toml | 2 +- NOTES_2016.md | 4 ++ README.md | 2 +- benches/year_2016.rs | 15 ++++- inputs/year_2016/day_02_input | 5 ++ src/year_2016.rs | 9 +++ src/year_2016/day_02.rs | 116 ++++++++++++++++++++++++++++++++++ 8 files changed, 156 insertions(+), 3 deletions(-) create mode 100644 inputs/year_2016/day_02_input create mode 100644 src/year_2016/day_02.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 16c7394..4db6995 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,12 @@ 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.2.1] +### Added +- Solved [exercice for 2016, day 02](src/year_2015/day_02.rs). +### Changed +- Moved all inputs from 2015 into their own folder (`inputs/year_2015`). + ## [2016.1.1] ### Added - Solved [exercice for 2016, day 01](src/year_2015/day_01.rs). diff --git a/Cargo.toml b/Cargo.toml index e438e41..af7524f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "advent-rs" -version = "2016.1.1" +version = "2016.2.1" edition = "2021" authors = ["Arnaud 'red' Rouyer"] readme = "README.md" diff --git a/NOTES_2016.md b/NOTES_2016.md index 7da583a..98fb999 100644 --- a/NOTES_2016.md +++ b/NOTES_2016.md @@ -4,3 +4,7 @@ Nice to get back into it! A funny thing to note in `day_01_v2`: I thought that using `match direction {}` inside the `for _i in 1..=steps {}` loop would prove costly, but doing it outside the loop to store which part of `position` to modify and by how much, ends up being slower. + +## Day 02: Bathroom Security + +I must admit: the biggest pleasure in learning a new language is when it compiles right on your first time. diff --git a/README.md b/README.md index 099b479..f4cc54a 100644 --- a/README.md +++ b/README.md @@ -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 01](NOTES_2016.md) +- [2016, up to day 02](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. diff --git a/benches/year_2016.rs b/benches/year_2016.rs index 423aa41..f97b9c9 100644 --- a/benches/year_2016.rs +++ b/benches/year_2016.rs @@ -1,4 +1,5 @@ use advent_rs::year_2016::day_01; +use advent_rs::year_2016::day_02; use criterion::{black_box, criterion_group, criterion_main, Criterion}; pub fn year_2016_day_01(c: &mut Criterion) { @@ -13,5 +14,17 @@ pub fn year_2016_day_01(c: &mut Criterion) { g2016_day_01.finish(); } -criterion_group!(benches, year_2016_day_01); +pub fn year_2016_day_02(c: &mut Criterion) { + let mut g2016_day_02 = c.benchmark_group("year_2016::day_02"); + let input_year_2016_day_02 = include_str!("../inputs/year_2016/day_02_input"); + g2016_day_02.bench_function("year_2016::day_02_v1", |b| { + b.iter(|| day_02::day_02_v1(black_box(input_year_2016_day_02))) + }); + g2016_day_02.bench_function("year_2016::day_02_v2", |b| { + b.iter(|| day_02::day_02_v2(black_box(input_year_2016_day_02))) + }); + g2016_day_02.finish(); +} + +criterion_group!(benches, year_2016_day_01, year_2016_day_02); criterion_main!(benches); diff --git a/inputs/year_2016/day_02_input b/inputs/year_2016/day_02_input new file mode 100644 index 0000000..92f908f --- /dev/null +++ b/inputs/year_2016/day_02_input @@ -0,0 +1,5 @@ +UULLULLUULLLURDLDUURRDRRLDURDULLRURDUDULLLUULURURLRDRRRRULDRUULLLLUUDURDULDRRDRUDLRRLDLUDLDDRURURUURRRDDDLLRUDURDULUULLRRULLRULDUDRDRLDLURURUDDUDLURUDUDURLURURRURLUDDRURRDLUURLLRURRDUDLULULUDULDLLRRRDLRDLDUDRDDDRRUURRRRRUURRDRRDLURDRRURDLLUULULLRURDLDDDRRLLRRUURULURUUDDLRRUDDRURUUDLRLRDLRURRRDULLDLRUDDUULRDULURUURDULUDLLRRLDDLRDLRUDRLDDRLRRRDURDULLRRRDRRLUURURDRRDRRLDLUDURURLDUURDRUDRDDRLDRRLDLURURULLUURUDUUDLRLL +LLLULLULDDULRLLURLLLRUUDDLRUULRLULLDLLRRDRLRLRLLDRUUURULDRDDLUDLLDUDULLLRLULLLRULDRDRUDLLRLRLLUDULRRRLDRUULDDULLDULULLUDUDLDRDURDLDLLDUDRRRDLUURRUURULLURLDURLRRLLDDUUULDRLUUDUDLURLULUDURRDRLLDDDDDRRULLRLDULULDDRUURRDLUDDDUDURDDRDRULULLLLUURDURUUUULUDLRURRULRDDRURURLLRLUUDUUURDLLDDLUDRLLLUDLLLLULRLURDRRRDUUDLLDLDDDURRDDRURUURDDRURRLDDDURDLLUURUUULRLUURRUDRLLDLURDUDRLULDLRLULULUDDLRDUDRUDLUULUULDURDRRRRLRULLUDRDDRDLDUDRDRRLDLLLLUDDLRULDLLDDUULDDRRULRRUURUDRDURLLLDDUUDRUUDLULLDR +UDUUULLDDDDLUDLDULRLRDLULLDDRULDURRLURRUDLRRUDURRDUDRRRUULRLLRLUDLDRRDUURDDRDRDUUUDUDLDLLRRLUURLUUUDDDUURLULURRLURRRDRDURURUDRLRUURUDRUDDDRDRDLDRDURDLDRRDUUDLLURLDDURRRLULDRDRLLRLLLRURLDURDRLDRUURRLDLDRLDDDRLDLRLDURURLLLLDDRDUDLRULULLRDDLLUDRDRRLUUULDRLDURURDUDURLLDRRDUULDUUDLLDDRUUULRRULDDUDRDRLRULUUDUURULLDLLURLRRLDDDLLDRRDDRLDDLURRUDURULUDLLLDUDDLDLDLRUDUDRDUDDLDDLDULURDDUDRRUUURLDUURULLRLULUURLLLLDUUDURUUDUULULDRULRLRDULDLLURDLRUUUDDURLLLLDUDRLUUDUDRRURURRDRDDRULDLRLURDLLRRDRUUUURLDRURDUUDLDURUDDLRDDDDURRLRLUDRRDDURDDRLDDLLRR +ULDRUDURUDULLUDUDURLDLLRRULRRULRUDLULLLDRULLDURUULDDURDUUDLRDRUDUDDLDRDLUULRRDLRUULULUUUDUUDDRDRLLULLRRDLRRLUDRLULLUUUUURRDURLLRURRULLLRLURRULRDUURRLDDRRDRLULDDRRDRLULLRDLRRURUDURULRLUDRUDLUDDDUDUDDUDLLRDLLDRURULUDRLRRULRDDDDDRLDLRRLUUDLUURRDURRDLDLDUDRLULLULRLDRDUDLRULLULLRLDDRURLLLRLDDDLLLRURDDDLLUDLDLRLUULLLRULDRRDUDLRRDDULRLLDUURLLLLLDRULDRLLLUURDURRULURLDDLRRUDULUURRLULRDRDDLULULRRURLDLRRRUDURURDURDULURULLRLDD +DURLRRRDRULDLULUDULUURURRLULUDLURURDDURULLRRUUDLRURLDLRUDULDLLRRULLLLRRLRUULDLDLLRDUDLLRLULRLLUUULULRDLDLRRURLUDDRRLUUDDRRUDDRRURLRRULLDDULLLURRULUDLRRRURRULRLLLRULLRRURDRLURULLDULRLLLULLRLRLLLDRRRRDDDDDDULUUDUDULRURDRUDRLUULURDURLURRDRRRRDRRLLLLUDLRRDURURLLULUDDLRLRLRRUURLLURLDUULLRRDURRULRULURLLLRLUURRULLLURDDDRURDUDDULLRULUUUDDRURUUDUURURRDRURDUDRLLRRULURUDLDURLDLRRRRLLUURRLULDDDUUUURUULDLDRLDUDULDRRULDRDULURRUURDU \ No newline at end of file diff --git a/src/year_2016.rs b/src/year_2016.rs index ab86c24..2d16970 100644 --- a/src/year_2016.rs +++ b/src/year_2016.rs @@ -3,6 +3,7 @@ //! Year 2016 //! pub mod day_01; +pub mod day_02; pub fn solve(day: u8, part: u8, input: impl Into) -> Option { if part != 1 && part != 2 { @@ -10,6 +11,7 @@ pub fn solve(day: u8, part: u8, input: impl Into) -> Option { } match day { 1 => Some(format!("{}", day_01::day_01(part, input))), + 2 => Some(format!("{}", day_02::day_02(part, input))), _ => None, } } @@ -24,4 +26,11 @@ mod tests { assert_eq!(day_01::day_01_v1(input), 146); assert_eq!(day_01::day_01_v2(input), 131); } + + #[test] + fn day_02() { + let input = include_str!("../inputs/year_2016/day_02_input"); + assert_eq!(day_02::day_02_v1(input), "45973"); + assert_eq!(day_02::day_02_v2(input), "27CA4"); + } } diff --git a/src/year_2016/day_02.rs b/src/year_2016/day_02.rs new file mode 100644 index 0000000..12335ac --- /dev/null +++ b/src/year_2016/day_02.rs @@ -0,0 +1,116 @@ +fn matcher_v1(curbut: char, line: &str) -> char { + let mut nextbut: char = curbut; + for direction in line.chars() { + nextbut = match (nextbut, direction) { + ('1', 'R') => '2', + ('1', 'D') => '4', + ('2', 'L') => '1', + ('2', 'R') => '3', + ('2', 'D') => '5', + ('3', 'L') => '2', + ('3', 'D') => '6', + ('4', 'U') => '1', + ('4', 'R') => '5', + ('4', 'D') => '7', + ('5', 'U') => '2', + ('5', 'L') => '4', + ('5', 'R') => '6', + ('5', 'D') => '8', + ('6', 'L') => '5', + ('6', 'U') => '3', + ('6', 'D') => '9', + ('7', 'R') => '8', + ('7', 'U') => '4', + ('8', 'L') => '7', + ('8', 'R') => '9', + ('8', 'U') => '5', + ('9', 'L') => '8', + ('9', 'U') => '6', + _ => nextbut, + } + } + nextbut +} + +fn matcher_v2(curbut: char, line: &str) -> char { + let mut nextbut: char = curbut; + for direction in line.chars() { + nextbut = match (nextbut, direction) { + ('1', 'D') => '3', + ('2', 'R') => '3', + ('2', 'D') => '6', + ('3', 'L') => '2', + ('3', 'U') => '1', + ('3', 'D') => '7', + ('3', 'R') => '4', + ('4', 'L') => '3', + ('4', 'D') => '8', + ('5', 'R') => '6', + ('6', 'U') => '2', + ('6', 'L') => '5', + ('6', 'R') => '7', + ('6', 'D') => 'A', + ('7', 'U') => '3', + ('7', 'L') => '6', + ('7', 'R') => '8', + ('7', 'D') => 'B', + ('8', 'U') => '4', + ('8', 'L') => '7', + ('8', 'R') => '9', + ('8', 'D') => 'C', + ('9', 'L') => '8', + ('A', 'R') => 'B', + ('A', 'U') => '6', + ('B', 'L') => 'A', + ('B', 'U') => '7', + ('B', 'D') => 'D', + ('B', 'R') => 'C', + ('C', 'L') => 'B', + ('C', 'U') => '8', + ('D', 'U') => 'B', + _ => nextbut, + } + } + nextbut +} + +pub fn day_02_v1(input: impl Into) -> String { + let mut result: Vec = vec![]; + let mut curbut = '5'; + for line in input.into().lines() { + curbut = matcher_v1(curbut, line); + result.push(curbut); + } + + result.iter().collect::() +} + +pub fn day_02_v2(input: impl Into) -> String { + let mut result: Vec = vec![]; + let mut curbut = '5'; + for line in input.into().lines() { + curbut = matcher_v2(curbut, line); + result.push(curbut); + } + + result.iter().collect::() +} + +solvable!(day_02, day_02_v1, day_02_v2, String); + +#[cfg(test)] +mod tests { + use super::*; + + const SAMPLE: &str = "ULL\nRRDDD\nLURDL\nUUUUD"; + + #[test] + fn works_with_samples_v1() { + assert_eq!(day_02_v1(SAMPLE), "1985"); + } + + #[test] + fn works_with_samples_v2() { + assert_eq!(day_02_v2(SAMPLE), "5DB3"); + } +}