Clean up day 2 and day 5
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Julius 2021-12-05 14:27:26 +01:00
parent 30d700694f
commit 4e62401f62
Signed by: j00lz
GPG key ID: AF241B0AA237BBA2
2 changed files with 13 additions and 56 deletions

View file

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

View file

@ -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::<Vec<Path>>();
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 {
format!("{}", covered_twice(&self.vents))
}
}
fn covered_twice(vents: &Vec<Path>) -> usize {
let mut m = HashMap::new();
for vent in &self.vents {
for vent in 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())
}
m.values().copied().filter(|x| *x >= 2).count()
}
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;
}
false
}
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);