Skip to content

Commit

Permalink
Year 2016: Day 02
Browse files Browse the repository at this point in the history
  • Loading branch information
joshleaves committed Mar 15, 2024
1 parent 821d3a5 commit e9dac70
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
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.1.1"
version = "2016.2.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 @@ -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.
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 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.
Expand Down
15 changes: 14 additions & 1 deletion benches/year_2016.rs
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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);
5 changes: 5 additions & 0 deletions inputs/year_2016/day_02_input
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
UULLULLUULLLURDLDUURRDRRLDURDULLRURDUDULLLUULURURLRDRRRRULDRUULLLLUUDURDULDRRDRUDLRRLDLUDLDDRURURUURRRDDDLLRUDURDULUULLRRULLRULDUDRDRLDLURURUDDUDLURUDUDURLURURRURLUDDRURRDLUURLLRURRDUDLULULUDULDLLRRRDLRDLDUDRDDDRRUURRRRRUURRDRRDLURDRRURDLLUULULLRURDLDDDRRLLRRUURULURUUDDLRRUDDRURUUDLRLRDLRURRRDULLDLRUDDUULRDULURUURDULUDLLRRLDDLRDLRUDRLDDRLRRRDURDULLRRRDRRLUURURDRRDRRLDLUDURURLDUURDRUDRDDRLDRRLDLURURULLUURUDUUDLRLL
LLLULLULDDULRLLURLLLRUUDDLRUULRLULLDLLRRDRLRLRLLDRUUURULDRDDLUDLLDUDULLLRLULLLRULDRDRUDLLRLRLLUDULRRRLDRUULDDULLDULULLUDUDLDRDURDLDLLDUDRRRDLUURRUURULLURLDURLRRLLDDUUULDRLUUDUDLURLULUDURRDRLLDDDDDRRULLRLDULULDDRUURRDLUDDDUDURDDRDRULULLLLUURDURUUUULUDLRURRULRDDRURURLLRLUUDUUURDLLDDLUDRLLLUDLLLLULRLURDRRRDUUDLLDLDDDURRDDRURUURDDRURRLDDDURDLLUURUUULRLUURRUDRLLDLURDUDRLULDLRLULULUDDLRDUDRUDLUULUULDURDRRRRLRULLUDRDDRDLDUDRDRRLDLLLLUDDLRULDLLDDUULDDRRULRRUURUDRDURLLLDDUUDRUUDLULLDR
UDUUULLDDDDLUDLDULRLRDLULLDDRULDURRLURRUDLRRUDURRDUDRRRUULRLLRLUDLDRRDUURDDRDRDUUUDUDLDLLRRLUURLUUUDDDUURLULURRLURRRDRDURURUDRLRUURUDRUDDDRDRDLDRDURDLDRRDUUDLLURLDDURRRLULDRDRLLRLLLRURLDURDRLDRUURRLDLDRLDDDRLDLRLDURURLLLLDDRDUDLRULULLRDDLLUDRDRRLUUULDRLDURURDUDURLLDRRDUULDUUDLLDDRUUULRRULDDUDRDRLRULUUDUURULLDLLURLRRLDDDLLDRRDDRLDDLURRUDURULUDLLLDUDDLDLDLRUDUDRDUDDLDDLDULURDDUDRRUUURLDUURULLRLULUURLLLLDUUDURUUDUULULDRULRLRDULDLLURDLRUUUDDURLLLLDUDRLUUDUDRRURURRDRDDRULDLRLURDLLRRDRUUUURLDRURDUUDLDURUDDLRDDDDURRLRLUDRRDDURDDRLDDLLRR
ULDRUDURUDULLUDUDURLDLLRRULRRULRUDLULLLDRULLDURUULDDURDUUDLRDRUDUDDLDRDLUULRRDLRUULULUUUDUUDDRDRLLULLRRDLRRLUDRLULLUUUUURRDURLLRURRULLLRLURRULRDUURRLDDRRDRLULDDRRDRLULLRDLRRURUDURULRLUDRUDLUDDDUDUDDUDLLRDLLDRURULUDRLRRULRDDDDDRLDLRRLUUDLUURRDURRDLDLDUDRLULLULRLDRDUDLRULLULLRLDDRURLLLRLDDDLLLRURDDDLLUDLDLRLUULLLRULDRRDUDLRRDDULRLLDUURLLLLLDRULDRLLLUURDURRULURLDDLRRUDULUURRLULRDRDDLULULRRURLDLRRRUDURURDURDULURULLRLDD
DURLRRRDRULDLULUDULUURURRLULUDLURURDDURULLRRUUDLRURLDLRUDULDLLRRULLLLRRLRUULDLDLLRDUDLLRLULRLLUUULULRDLDLRRURLUDDRRLUUDDRRUDDRRURLRRULLDDULLLURRULUDLRRRURRULRLLLRULLRRURDRLURULLDULRLLLULLRLRLLLDRRRRDDDDDDULUUDUDULRURDRUDRLUULURDURLURRDRRRRDRRLLLLUDLRRDURURLLULUDDLRLRLRRUURLLURLDUULLRRDURRULRULURLLLRLUURRULLLURDDDRURDUDDULLRULUUUDDRURUUDUURURRDRURDUDRLLRRULURUDLDURLDLRRRRLLUURRLULDDDUUUURUULDLDRLDUDULDRRULDRDULURRUURDU
9 changes: 9 additions & 0 deletions src/year_2016.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
//! Year 2016
//!
pub mod day_01;
pub mod day_02;

pub fn solve(day: u8, part: u8, input: impl Into<String>) -> Option<String> {
if part != 1 && part != 2 {
return None;
}
match day {
1 => Some(format!("{}", day_01::day_01(part, input))),
2 => Some(format!("{}", day_02::day_02(part, input))),
_ => None,
}
}
Expand All @@ -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");
}
}
116 changes: 116 additions & 0 deletions src/year_2016/day_02.rs
Original file line number Diff line number Diff line change
@@ -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>) -> String {
let mut result: Vec<char> = vec![];
let mut curbut = '5';
for line in input.into().lines() {
curbut = matcher_v1(curbut, line);
result.push(curbut);
}

result.iter().collect::<String>()
}

pub fn day_02_v2(input: impl Into<String>) -> String {
let mut result: Vec<char> = vec![];
let mut curbut = '5';
for line in input.into().lines() {
curbut = matcher_v2(curbut, line);
result.push(curbut);
}

result.iter().collect::<String>()
}

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");
}
}

0 comments on commit e9dac70

Please sign in to comment.