y2016d2
This commit is contained in:
parent
fa52f5bd13
commit
4c62525fa7
5
y2016/resources/2_input.txt
Normal file
5
y2016/resources/2_input.txt
Normal file
@ -0,0 +1,5 @@
|
||||
LLULLLRLDLLLRLUURDDLRDLDURULRLUULUDDUDDLLLURRLDRRLDRRRLDUDLRDLRRDLLDUDUDUDRLUDUUDLLLRDURUDUULUDLRDUUUDUUDURLDUULLRDLULDUURUDRDDLDRLURLRURRDUURLRLUURURUUULLRLLULRUURLULURDLLRRUDLUDULDRDRLRULUURRDRULLRUUUDLRLDLUURRRURDLUDDRRUDRLUDRDLLLLLRULLDUDRLRRDDULDLRUURRRRRLDLDLRDURDRUUURDLRDDDDULURRRRDUURLULLLDLRULRDULRUDLRRLRDLLRLLLUDDLRDRURDDLLLLDUDRDLRURRDLRDDDLDULDRLRULUUDRRRUUULLLURRDDUULURULDURRLLULLDRURUUULRLRDRRUDRDRRDURRUUUULDRDDDUDLDDURLLRR
|
||||
LDLRRRUURDLDDRLRRDLLULRULLLUDUUDUDLRULLDRUDRULLDULURDRDDLRURDDULLLLDLRDRDRDDURLURLURLUDRDDRDULULUDDRURRDLLDUURDRDDLRLLURRDLRDDULDLULURDRDLUDRRUUDULLULURRDUDRUUUDRULDLDURLRRUDURLDLRRUURRRURDLUDRLDUDRRUDUURURUDDUUDRDULRDLUDRRRLDRURLLRDDDLUDRDUDURDDDRRDDRRRLLRRDDLDDLRUURRURDLLDRLRRDLLUDRRRURURLRDRLLRLRLRULLRURLDLRRULLRRRDULUUULDRDLLURDDLDLRDRLUUDLLUDDLDRRLDLRUDRUDLLUURLLULURUDUDRLULLUDRURDDLDLDDUDLRDDRRURLRLLUDDUDRUURRURRULDRLDDRLLRRLDDURRDLDULLLURULLLRUURLRRRRUUULRLLLURRLRLRUDRDUUUDUUUDDLULLDLLLLDLDRULDRUUULDDDLURLDLRLULRUDDDDURDDLU
|
||||
RURLURRDLDULLULDDDLRUULLUURLRUDRUDRRUDDLDDDDRRDLRURLRURLDDDUDDUURRDRULDRRRULRDRDDLRUDULRLURDUUDRRLDLRDRURDLDRRRRDRURUUDDDLLRDRDUDUDUDLLULURULRRLRURUULUULDDDDURULRULLRUDUURLURDUDLUDLUDRLLDUUDUULRLRLUUDRDULDULRURDRRRULRUDLRURDDULUDULLRLRURURUULLULDRURLLRRUUDDUUURRDLURUURULRDRRDDUDULRDDLUDLURURUURDRULLRDDLLRDDLDRDUDRRDLUURRLRLUURRULUDURLDDRLLURRDDDLDDRURULLDDRLUDDLRLURDUDULLRDULLLDLLUDDRUDRUDDUUDRDRULRL
|
||||
RLRDRDULULUDLUDRDRLUDLDLLUDURULDDDUDLRURLLRLRLDLDRLDURDLRRURLULLULURLLDRRDRLUDRLRDLLULRULURRURURUULRDUDLLRDLRRRRRLUURDRRRDLRUDLLDLLDLRUUUDLLLDDDLRDULLRUUDDRLDDURRRDLRLRLDDDDLRDRULLUURUUDRRLLRLLRDDLLRURRRRDRULRRLLRLLLRLDRRLDDDURRURLDURUURRLRLRLDRURULLRLRUDLDUURDLLRLDLURUUUDLLRDRDDDDDDRLDRRRLRRRRURUDLDDRDLLURUDLRRLDDDLUDUDUULRDULULUDDULUUDLLLLRLDDUUULRLRDULURDURRRURRULURRRDRDLDDURDLURUDURRRDDRLRLUDLUDDLUULLDURLURDDUDDLRUUUDRLLDRURL
|
||||
ULUDLLUDDULRUURDRURDUDUDLUURDDDRRLUDURURDRURRLDRDURLRLLRRDDRRDRRRUULURUDURUDULRRRRDDLDURRLRRDUDDDRLLLULDRLRLURRDUURDURRRURRDLUDUDDRLDLURRRDDRLLRDRDDRDURRRRLURRLUDDURRULRUDUDULDRUDDRULLUUULDURRRLDRULLURULLRUDLDUDDLDULDLUUDRULULDLLDRULLRUULDUDUUDRLRRLDLUULUDLLDDRLRRDDLLURURDULRRDDRURDRLRLULDLDURULLUUUDURURDLDUDDDDUUULUDLUURRULLDLRLURDLURLRLDDURRLDDRRRDUUULLUULDLLDLLDDRLRRUDLULDRLULDULULRRLRULUUURURUUURDUUDDURLLUDDRLRDDLUURRUULRDLDDRLULUULRDRURLUURDRDUURUDLRR
|
20
y2016/src/bin/d2.rs
Normal file
20
y2016/src/bin/d2.rs
Normal file
@ -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));
|
||||
}
|
133
y2016/src/days/d2.rs
Normal file
133
y2016/src/days/d2.rs
Normal file
@ -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::<Vec<String>>()
|
||||
.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::<Vec<String>>()
|
||||
.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());
|
||||
}
|
||||
}
|
@ -1 +1,3 @@
|
||||
pub mod d1;
|
||||
|
||||
pub mod d2;
|
||||
|
Loading…
Reference in New Issue
Block a user