start work on day 5

This commit is contained in:
Joseph Montanaro
2021-12-08 14:53:33 -08:00
parent 2fbc98de3d
commit 444a7f6e47
3 changed files with 156 additions and 1 deletions

77
2021/src/day5.rs Normal file
View File

@ -0,0 +1,77 @@
use color_eyre::eyre;
use num::{Integer, integer::gcd};
#[derive(Copy, Clone)]
struct Point {
x: isize,
y: isize,
}
#[derive(Copy, Clone)]
struct Line {
start: Point,
end: Point,
}
#[derive(Copy, Clone)]
struct Slope {
rise: isize,
run: isize,
}
impl Line {
fn slope(&self) -> Slope {
let mut rise = self.end.y - self.start.y;
let mut run = self.end.x - self.start.x;
if rise != 0 && run != 0 {
let divisor = gcd(rise, run);
rise /= divisor;
run /= divisor;
}
Slope {rise, run}
}
}
struct PointsIter<'a> {
line: &'a Line,
pos: Point,
slope: Slope,
}
impl PointsIter<'_> {
fn from(line: &Line) -> Self {
PointsIter {
line,
pos: line.start,
slope: line.slope(),
}
}
}
impl Iterator for PointsIter {
type Item = Point;
fn next(&mut self) -> Option<Self::Item> {
if self.pos.x = self.line.start.x && self.pos.y == self.line.start.y {
return Some(self.pos);
}
if self.pos.x != self.line.end.x && self.pos.y != self.line.end.y {
self.pos.y += self.slope.rise;
self.pos.x += self.slope.run;
return Some(self.pos);
}
else {
return None;
}
}
}
struct GridCounter<const W: usize, const H: usize> {
rows: [[usize; W]; H],
}