diff --git a/y2016/resources/2_input.txt b/y2016/resources/2_input.txt new file mode 100644 index 0000000..24f75e7 --- /dev/null +++ b/y2016/resources/2_input.txt @@ -0,0 +1,5 @@ +LLULLLRLDLLLRLUURDDLRDLDURULRLUULUDDUDDLLLURRLDRRLDRRRLDUDLRDLRRDLLDUDUDUDRLUDUUDLLLRDURUDUULUDLRDUUUDUUDURLDUULLRDLULDUURUDRDDLDRLURLRURRDUURLRLUURURUUULLRLLULRUURLULURDLLRRUDLUDULDRDRLRULUURRDRULLRUUUDLRLDLUURRRURDLUDDRRUDRLUDRDLLLLLRULLDUDRLRRDDULDLRUURRRRRLDLDLRDURDRUUURDLRDDDDULURRRRDUURLULLLDLRULRDULRUDLRRLRDLLRLLLUDDLRDRURDDLLLLDUDRDLRURRDLRDDDLDULDRLRULUUDRRRUUULLLURRDDUULURULDURRLLULLDRURUUULRLRDRRUDRDRRDURRUUUULDRDDDUDLDDURLLRR +LDLRRRUURDLDDRLRRDLLULRULLLUDUUDUDLRULLDRUDRULLDULURDRDDLRURDDULLLLDLRDRDRDDURLURLURLUDRDDRDULULUDDRURRDLLDUURDRDDLRLLURRDLRDDULDLULURDRDLUDRRUUDULLULURRDUDRUUUDRULDLDURLRRUDURLDLRRUURRRURDLUDRLDUDRRUDUURURUDDUUDRDULRDLUDRRRLDRURLLRDDDLUDRDUDURDDDRRDDRRRLLRRDDLDDLRUURRURDLLDRLRRDLLUDRRRURURLRDRLLRLRLRULLRURLDLRRULLRRRDULUUULDRDLLURDDLDLRDRLUUDLLUDDLDRRLDLRUDRUDLLUURLLULURUDUDRLULLUDRURDDLDLDDUDLRDDRRURLRLLUDDUDRUURRURRULDRLDDRLLRRLDDURRDLDULLLURULLLRUURLRRRRUUULRLLLURRLRLRUDRDUUUDUUUDDLULLDLLLLDLDRULDRUUULDDDLURLDLRLULRUDDDDURDDLU +RURLURRDLDULLULDDDLRUULLUURLRUDRUDRRUDDLDDDDRRDLRURLRURLDDDUDDUURRDRULDRRRULRDRDDLRUDULRLURDUUDRRLDLRDRURDLDRRRRDRURUUDDDLLRDRDUDUDUDLLULURULRRLRURUULUULDDDDURULRULLRUDUURLURDUDLUDLUDRLLDUUDUULRLRLUUDRDULDULRURDRRRULRUDLRURDDULUDULLRLRURURUULLULDRURLLRRUUDDUUURRDLURUURULRDRRDDUDULRDDLUDLURURUURDRULLRDDLLRDDLDRDUDRRDLUURRLRLUURRULUDURLDDRLLURRDDDLDDRURULLDDRLUDDLRLURDUDULLRDULLLDLLUDDRUDRUDDUUDRDRULRL +RLRDRDULULUDLUDRDRLUDLDLLUDURULDDDUDLRURLLRLRLDLDRLDURDLRRURLULLULURLLDRRDRLUDRLRDLLULRULURRURURUULRDUDLLRDLRRRRRLUURDRRRDLRUDLLDLLDLRUUUDLLLDDDLRDULLRUUDDRLDDURRRDLRLRLDDDDLRDRULLUURUUDRRLLRLLRDDLLRURRRRDRULRRLLRLLLRLDRRLDDDURRURLDURUURRLRLRLDRURULLRLRUDLDUURDLLRLDLURUUUDLLRDRDDDDDDRLDRRRLRRRRURUDLDDRDLLURUDLRRLDDDLUDUDUULRDULULUDDULUUDLLLLRLDDUUULRLRDULURDURRRURRULURRRDRDLDDURDLURUDURRRDDRLRLUDLUDDLUULLDURLURDDUDDLRUUUDRLLDRURL +ULUDLLUDDULRUURDRURDUDUDLUURDDDRRLUDURURDRURRLDRDURLRLLRRDDRRDRRRUULURUDURUDULRRRRDDLDURRLRRDUDDDRLLLULDRLRLURRDUURDURRRURRDLUDUDDRLDLURRRDDRLLRDRDDRDURRRRLURRLUDDURRULRUDUDULDRUDDRULLUUULDURRRLDRULLURULLRUDLDUDDLDULDLUUDRULULDLLDRULLRUULDUDUUDRLRRLDLUULUDLLDDRLRRDDLLURURDULRRDDRURDRLRLULDLDURULLUUUDURURDLDUDDDDUUULUDLUURRULLDLRLURDLURLRLDDURRLDDRRRDUUULLUULDLLDLLDDRLRRUDLULDRLULDULULRRLRULUUURURUUURDUUDDURLLUDDRLRDDLUURRUULRDLDDRLULUULRDRURLUURDRDUURUDLRR diff --git a/y2016/src/bin/d2.rs b/y2016/src/bin/d2.rs new file mode 100644 index 0000000..8340949 --- /dev/null +++ b/y2016/src/bin/d2.rs @@ -0,0 +1,20 @@ +use std::fs; + +use y2016::days::d2; + +fn main() { + part1(); + part2(); +} + +fn part1() { + let root = env!("CARGO_MANIFEST_DIR"); + let content = fs::read_to_string(format!("{root}/resources/2_input.txt")).unwrap(); + println!("{}", d2::process_part1(&content)); +} + +fn part2() { + let root = env!("CARGO_MANIFEST_DIR"); + let content = fs::read_to_string(format!("{root}/resources/2_input.txt")).unwrap(); + println!("{}", d2::process_part2(&content)); +} diff --git a/y2016/src/days/d2.rs b/y2016/src/days/d2.rs new file mode 100644 index 0000000..f2bd6d3 --- /dev/null +++ b/y2016/src/days/d2.rs @@ -0,0 +1,133 @@ +use core::panic; + +pub fn process_part1(input: &str) -> String { + let mut position = (0, 0); + input + .lines() + .map(|line| { + for char in line.chars() { + match char { + 'U' => { + if position.1 != -1 { + position.1 -= 1; + } + } + 'D' => { + if position.1 != 1 { + position.1 += 1; + } + } + 'L' => { + if position.0 != -1 { + position.0 -= 1; + } + } + 'R' => { + if position.0 != 1 { + position.0 += 1; + } + } + _ => panic!("Unknown direction {char}"), + } + } + key_from_position(position) + }) + .collect::>() + .join("") +} + +fn key_from_position(position: (i32, i32)) -> String { + match position { + (-1, -1) => "1", + (0, -1) => "2", + (1, -1) => "3", + (-1, 0) => "4", + (0, 0) => "5", + (1, 0) => "6", + (-1, 1) => "7", + (0, 1) => "8", + (1, 1) => "9", + (_, _) => panic!("No key at position {}, {}", position.0, position.1), + } + .to_string() +} + +pub fn process_part2(input: &str) -> String { + let mut position: (i32, i32) = (-2, 0); + input + .lines() + .map(|line| { + for char in line.chars() { + let v_bound = 2 - position.0.abs(); + let h_bound = 2 - position.1.abs(); + match char { + 'U' => { + if position.1 > -v_bound { + position.1 -= 1; + } + } + 'D' => { + if position.1 < v_bound { + position.1 += 1; + } + } + 'L' => { + if position.0 > -h_bound { + position.0 -= 1; + } + } + 'R' => { + if position.0 < h_bound { + position.0 += 1; + } + } + _ => panic!("Unknown direction {char}"), + } + } + key_from_position_p2(position) + }) + .collect::>() + .join("") +} + +fn key_from_position_p2(position: (i32, i32)) -> String { + match position { + (0, -2) => "1", + (-1, -1) => "2", + (0, -1) => "3", + (1, -1) => "4", + (-2, 0) => "5", + (-1, 0) => "6", + (0, 0) => "7", + (1, 0) => "8", + (2, 0) => "9", + (-1, 1) => "A", + (0, 1) => "B", + (1, 1) => "C", + (0, 2) => "D", + (_, _) => panic!("No key at position {}, {}", position.0, position.1), + } + .to_string() +} + +#[cfg(test)] +mod tests { + use super::*; + + const INPUT: &str = "ULL +RRDDD +LURDL +UUUUD"; + + #[test] + fn part1() { + let result = process_part1(INPUT); + assert_eq!(result, "1985".to_string()); + } + + #[test] + fn part2() { + let result = process_part2(INPUT); + assert_eq!(result, "5DB3".to_string()); + } +} diff --git a/y2016/src/days/mod.rs b/y2016/src/days/mod.rs index 52d31d2..7f76109 100644 --- a/y2016/src/days/mod.rs +++ b/y2016/src/days/mod.rs @@ -1 +1,3 @@ pub mod d1; + +pub mod d2;