diff --git a/y2016/resources/1_input.txt b/y2016/resources/1_input.txt new file mode 100644 index 0000000..5897ba6 --- /dev/null +++ b/y2016/resources/1_input.txt @@ -0,0 +1 @@ +L5, R1, L5, L1, R5, R1, R1, L4, L1, L3, R2, R4, L4, L1, L1, R2, R4, R3, L1, R4, L4, L5, L4, R4, L5, R1, R5, L2, R1, R3, L2, L4, L4, R1, L192, R5, R1, R4, L5, L4, R5, L1, L1, R48, R5, R5, L2, R4, R4, R1, R3, L1, L4, L5, R1, L4, L2, L5, R5, L2, R74, R4, L1, R188, R5, L4, L2, R5, R2, L4, R4, R3, R3, R2, R1, L3, L2, L5, L5, L2, L1, R1, R5, R4, L3, R5, L1, L3, R4, L1, L3, L2, R1, R3, R2, R5, L3, L1, L1, R5, L4, L5, R5, R2, L5, R2, L1, L5, L3, L5, L5, L1, R1, L4, L3, L1, R2, R5, L1, L3, R4, R5, L4, L1, R5, L1, R5, R5, R5, R2, R1, R2, L5, L5, L5, R4, L5, L4, L4, R5, L2, R1, R5, L1, L5, R4, L3, R4, L2, R3, R3, R3, L2, L2, L2, L1, L4, R3, L4, L2, R2, R5, L1, R2 diff --git a/y2016/src/bin/d1.rs b/y2016/src/bin/d1.rs new file mode 100644 index 0000000..30bd4ff --- /dev/null +++ b/y2016/src/bin/d1.rs @@ -0,0 +1,20 @@ +use std::fs; + +use y2016::days::d1; + +fn main() { + part1(); + part2(); +} + +fn part1() { + let root = env!("CARGO_MANIFEST_DIR"); + let content = fs::read_to_string(format!("{root}/resources/1_input.txt")).unwrap(); + println!("{}", d1::process_part1(&content)); +} + +fn part2() { + let root = env!("CARGO_MANIFEST_DIR"); + let content = fs::read_to_string(format!("{root}/resources/1_input.txt")).unwrap(); + println!("{}", d1::process_part2(&content)); +} diff --git a/y2016/src/days/d1.rs b/y2016/src/days/d1.rs new file mode 100644 index 0000000..ca058c1 --- /dev/null +++ b/y2016/src/days/d1.rs @@ -0,0 +1,105 @@ +use core::panic; +use std::collections::{HashMap, HashSet}; + +#[derive(Clone, Copy, Hash, PartialEq, Eq)] +enum Orientation { + North, + East, + South, + West, +} + +impl Orientation { + fn turn(&mut self, direction: &str) { + match direction { + "R" => match self { + Orientation::North => *self = Self::East, + Orientation::East => *self = Self::South, + Orientation::South => *self = Self::West, + Orientation::West => *self = Self::North, + }, + "L" => match self { + Orientation::North => *self = Self::West, + Orientation::West => *self = Self::South, + Orientation::South => *self = Self::East, + Orientation::East => *self = Self::North, + }, + _ => panic!("Wrong direction. Only R and L allowed"), + } + } +} + +pub fn process_part1(input: &str) -> i32 { + let mut orientation = Orientation::North; + let mut movements = HashMap::new(); + let mut input = input.to_string(); + input.pop(); + for movement in input.split(", ") { + let (direction, blocks) = movement.split_at(1); + orientation.turn(direction); + let blocks = blocks.parse::().unwrap(); + movements + .entry(orientation) + .and_modify(|distance| *distance += blocks) + .or_insert(blocks); + } + let up = movements.get(&Orientation::North).unwrap_or(&0); + let down = movements.get(&Orientation::South).unwrap_or(&0); + let left = movements.get(&Orientation::West).unwrap_or(&0); + let right = movements.get(&Orientation::East).unwrap_or(&0); + (up - down).abs() + (left - right).abs() +} + +pub fn process_part2(input: &str) -> i32 { + let mut orientation = Orientation::North; + let mut visited_coords = HashSet::new(); + let mut horizontal: i32 = 0; + let mut vertical: i32 = 0; + let mut input = input.to_string(); + input.pop(); + for movement in input.split(", ") { + let (direction, blocks) = movement.split_at(1); + orientation.turn(direction); + let blocks = blocks.parse::().unwrap(); + for _block in 1..=blocks { + match orientation { + Orientation::North => vertical += 1, + Orientation::South => vertical -= 1, + Orientation::East => horizontal += 1, + Orientation::West => horizontal -= 1, + } + if visited_coords.contains(&(horizontal, vertical)) { + return horizontal.abs() + vertical.abs(); + } else { + visited_coords.insert((horizontal, vertical)); + } + } + } + panic!("Did not find easter bunny hq"); +} + +#[cfg(test)] +mod tests { + use super::*; + + const INPUT1: &str = "R2, L3 "; + const INPUT2: &str = "R2, R2, R2 "; + const INPUT3: &str = "R5, L5, R5, R3 "; + const INPUT4: &str = "R8, R4, R4, R8 "; + + #[test] + fn part1() { + let result = process_part1(INPUT1); + assert_eq!(result, 5); + let result = process_part1(INPUT2); + assert_eq!(result, 2); + let result = process_part1(INPUT3); + assert_eq!(result, 12); + } + + #[test] + fn part2() { + let result = process_part2(INPUT4); + assert_eq!(result, 4); + } +} diff --git a/y2016/src/days/mod.rs b/y2016/src/days/mod.rs new file mode 100644 index 0000000..52d31d2 --- /dev/null +++ b/y2016/src/days/mod.rs @@ -0,0 +1 @@ +pub mod d1; diff --git a/y2016/src/lib.rs b/y2016/src/lib.rs index 8b13789..c73659c 100644 --- a/y2016/src/lib.rs +++ b/y2016/src/lib.rs @@ -1 +1 @@ - +pub mod days;