Clean up day 2 and day 5
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
30d700694f
commit
4e62401f62
|
@ -95,11 +95,4 @@ forward 2";
|
||||||
let d = Day02::init(f);
|
let d = Day02::init(f);
|
||||||
assert_eq!("1592426537", d.part2())
|
assert_eq!("1592426537", d.part2())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
#[should_panic]
|
|
||||||
fn very_invalid_input(){
|
|
||||||
let f = String::from("invalid 10");
|
|
||||||
Day02::init(f);
|
|
||||||
}
|
|
||||||
}
|
}
|
54
src/day05.rs
54
src/day05.rs
|
@ -29,47 +29,27 @@ impl Day for Day05 {
|
||||||
|
|
||||||
fn part1(&self) -> String {
|
fn part1(&self) -> String {
|
||||||
let new_vents = self.vents.iter().filter(|p| p.is_straight()).cloned().collect::<Vec<Path>>();
|
let new_vents = self.vents.iter().filter(|p| p.is_straight()).cloned().collect::<Vec<Path>>();
|
||||||
let mut m = HashMap::new();
|
format!("{}", covered_twice(&new_vents))
|
||||||
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())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn part2(&self) -> String {
|
fn part2(&self) -> String {
|
||||||
|
format!("{}", covered_twice(&self.vents))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn covered_twice(vents: &Vec<Path>) -> usize {
|
||||||
let mut m = HashMap::new();
|
let mut m = HashMap::new();
|
||||||
for vent in &self.vents {
|
for vent in vents {
|
||||||
for cover in &vent.covers {
|
for cover in &vent.covers {
|
||||||
let m = m.entry(cover).or_insert(0);
|
let m = m.entry(cover).or_insert(0);
|
||||||
*m += 1;
|
*m += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
format!("{}", m.values().copied().filter(|x| *x >= 2).count())
|
m.values().copied().filter(|x| *x >= 2).count()
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Path {
|
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 {
|
fn is_straight(&self) -> bool {
|
||||||
self.start.x == self.end.x || self.start.y == self.end.y
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::Day05;
|
use crate::Day05;
|
||||||
use crate::day05::{Coordinate, Direction, Path};
|
use crate::day05::{Coordinate, Direction};
|
||||||
use crate::day::Day;
|
use crate::day::Day;
|
||||||
|
|
||||||
const INPUT: &str = r"0,9 -> 5,9
|
const INPUT: &str = r"0,9 -> 5,9
|
||||||
|
@ -179,22 +159,6 @@ mod tests {
|
||||||
0,0 -> 8,8
|
0,0 -> 8,8
|
||||||
5,5 -> 8,2";
|
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]
|
#[test]
|
||||||
fn directions() {
|
fn directions() {
|
||||||
assert_eq!(Coordinate::new(5, 5).dir_to(&Coordinate::new(0, 0)), Direction::NW);
|
assert_eq!(Coordinate::new(5, 5).dir_to(&Coordinate::new(0, 0)), Direction::NW);
|
||||||
|
|
Loading…
Reference in a new issue