start work on day 5
This commit is contained in:
parent
2fbc98de3d
commit
444a7f6e47
77
2021/Cargo.lock
generated
77
2021/Cargo.lock
generated
@ -22,6 +22,7 @@ name = "advent-2021"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"color-eyre",
|
||||
"num",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -134,6 +135,82 @@ dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "43db66d1170d347f9a065114077f7dccb00c1b9478c89384490a3425279a4606"
|
||||
dependencies = [
|
||||
"num-bigint",
|
||||
"num-complex",
|
||||
"num-integer",
|
||||
"num-iter",
|
||||
"num-rational",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-bigint"
|
||||
version = "0.4.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f93ab6289c7b344a8a9f60f88d80aa20032336fe78da341afc91c8a2341fc75f"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-complex"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "26873667bbbb7c5182d4a37c1add32cdf09f841af72da53318fdb81543c15085"
|
||||
dependencies = [
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-integer"
|
||||
version = "0.1.44"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-iter"
|
||||
version = "0.1.42"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b2021c8337a54d21aca0d59a92577a029af9431cb59b909b03252b9c164fad59"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-rational"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d41702bd167c2df5520b384281bc111a4b5efcf7fbc4c9c222c815b07e0a6a6a"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-bigint",
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "object"
|
||||
version = "0.27.1"
|
||||
|
@ -8,3 +8,4 @@ edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
color-eyre = "^0.5.11"
|
||||
num = "^0.4.0"
|
77
2021/src/day5.rs
Normal file
77
2021/src/day5.rs
Normal 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],
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user