From 4e62401f62434cab8d254d6bff9016702297a3f3 Mon Sep 17 00:00:00 2001 From: Julius de Jeu Date: Sun, 5 Dec 2021 14:27:26 +0100 Subject: [PATCH] Clean up day 2 and day 5 --- src/day02.rs | 7 ------ src/day05.rs | 62 +++++++++++----------------------------------------- 2 files changed, 13 insertions(+), 56 deletions(-) diff --git a/src/day02.rs b/src/day02.rs index 4c656d0..6107445 100644 --- a/src/day02.rs +++ b/src/day02.rs @@ -95,11 +95,4 @@ forward 2"; let d = Day02::init(f); assert_eq!("1592426537", d.part2()) } - - #[test] - #[should_panic] - fn very_invalid_input(){ - let f = String::from("invalid 10"); - Day02::init(f); - } } \ No newline at end of file diff --git a/src/day05.rs b/src/day05.rs index a57a4a9..ec7033b 100644 --- a/src/day05.rs +++ b/src/day05.rs @@ -29,47 +29,27 @@ impl Day for Day05 { fn part1(&self) -> String { let new_vents = self.vents.iter().filter(|p| p.is_straight()).cloned().collect::>(); - let mut m = HashMap::new(); - for vent in &new_vents { - for cover in &vent.covers { - let m = m.entry(cover).or_insert(0); - *m += 1; - } - } - - format!("{}", m.values().copied().filter(|x| *x >= 2).count()) + format!("{}", covered_twice(&new_vents)) } fn part2(&self) -> String { - let mut m = HashMap::new(); - for vent in &self.vents { - for cover in &vent.covers { - let m = m.entry(cover).or_insert(0); - *m += 1; - } - } - - format!("{}", m.values().copied().filter(|x| *x >= 2).count()) + format!("{}", covered_twice(&self.vents)) } } -impl Path { - #[cfg(test)] - fn covers_diag(&self, target: &Coordinate) -> bool { - let dir = self.start.dir_to(&self.end).coord_diff(); - // println!("{:?}, {:?}, {:?}", self.start, self.end, dir); - let mut coord = (&self.start).clone(); - while coord != self.end { - if coord == target.clone() { - return true; - } - coord.x += dir.0; - coord.y += dir.1; +fn covered_twice(vents: &Vec) -> usize { + let mut m = HashMap::new(); + for vent in vents { + for cover in &vent.covers { + let m = m.entry(cover).or_insert(0); + *m += 1; } - - false } + m.values().copied().filter(|x| *x >= 2).count() +} + +impl Path { fn is_straight(&self) -> bool { self.start.x == self.end.x || self.start.y == self.end.y } @@ -165,7 +145,7 @@ fn str_to_vent(st: &str) -> Path { #[cfg(test)] mod tests { use crate::Day05; - use crate::day05::{Coordinate, Direction, Path}; + use crate::day05::{Coordinate, Direction}; use crate::day::Day; const INPUT: &str = r"0,9 -> 5,9 @@ -179,22 +159,6 @@ mod tests { 0,0 -> 8,8 5,5 -> 8,2"; - #[test] - fn covers_test_1() { - let start = Coordinate { x: 0, y: 0 }; - let end = Coordinate { x: 3, y: 0 }; - let path = Path { start, end, covers: vec![] }; - assert!(path.covers_diag(&Coordinate { x: 2, y: 0 })); - } - - #[test] - fn covers_test_2() { - let start = Coordinate { x: 0, y: 0 }; - let end = Coordinate { x: 0, y: 3 }; - let path = Path { start, end, covers: vec![] }; - assert!(path.covers_diag(&Coordinate { x: 0, y: 2 })); - } - #[test] fn directions() { assert_eq!(Coordinate::new(5, 5).dir_to(&Coordinate::new(0, 0)), Direction::NW);