diff --git a/2021/Cargo.lock b/2021/Cargo.lock index 145cf10..243822b 100644 --- a/2021/Cargo.lock +++ b/2021/Cargo.lock @@ -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" diff --git a/2021/Cargo.toml b/2021/Cargo.toml index 7cacf6e..b493610 100644 --- a/2021/Cargo.toml +++ b/2021/Cargo.toml @@ -7,4 +7,5 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -color-eyre = "^0.5.11" \ No newline at end of file +color-eyre = "^0.5.11" +num = "^0.4.0" \ No newline at end of file diff --git a/2021/src/day5.rs b/2021/src/day5.rs new file mode 100644 index 0000000..53dbca1 --- /dev/null +++ b/2021/src/day5.rs @@ -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 { + 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 { + rows: [[usize; W]; H], +}