Compare commits
4 commits
4022aa6e68
...
1b59efa250
Author | SHA1 | Date | |
---|---|---|---|
1b59efa250 | |||
f41c0177d8 | |||
7db15c9cf7 | |||
26700274c3 |
7 changed files with 280 additions and 2 deletions
2
2022/data/day14.expected
Normal file
2
2022/data/day14.expected
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
610
|
||||||
|
27194
|
123
2022/data/day14.input
Normal file
123
2022/data/day14.input
Normal file
|
@ -0,0 +1,123 @@
|
||||||
|
512,122 -> 512,118 -> 512,122 -> 514,122 -> 514,120 -> 514,122 -> 516,122 -> 516,121 -> 516,122
|
||||||
|
503,45 -> 503,41 -> 503,45 -> 505,45 -> 505,43 -> 505,45 -> 507,45 -> 507,37 -> 507,45 -> 509,45 -> 509,35 -> 509,45 -> 511,45 -> 511,37 -> 511,45
|
||||||
|
516,125 -> 516,129 -> 515,129 -> 515,136 -> 526,136 -> 526,129 -> 519,129 -> 519,125
|
||||||
|
503,45 -> 503,41 -> 503,45 -> 505,45 -> 505,43 -> 505,45 -> 507,45 -> 507,37 -> 507,45 -> 509,45 -> 509,35 -> 509,45 -> 511,45 -> 511,37 -> 511,45
|
||||||
|
512,122 -> 512,118 -> 512,122 -> 514,122 -> 514,120 -> 514,122 -> 516,122 -> 516,121 -> 516,122
|
||||||
|
511,86 -> 515,86
|
||||||
|
485,22 -> 489,22
|
||||||
|
508,62 -> 508,56 -> 508,62 -> 510,62 -> 510,52 -> 510,62 -> 512,62 -> 512,60 -> 512,62 -> 514,62 -> 514,58 -> 514,62 -> 516,62 -> 516,55 -> 516,62
|
||||||
|
509,150 -> 513,150
|
||||||
|
516,65 -> 516,69 -> 508,69 -> 508,74 -> 521,74 -> 521,69 -> 520,69 -> 520,65
|
||||||
|
503,45 -> 503,41 -> 503,45 -> 505,45 -> 505,43 -> 505,45 -> 507,45 -> 507,37 -> 507,45 -> 509,45 -> 509,35 -> 509,45 -> 511,45 -> 511,37 -> 511,45
|
||||||
|
494,157 -> 494,159 -> 491,159 -> 491,167 -> 503,167 -> 503,159 -> 499,159 -> 499,157
|
||||||
|
494,157 -> 494,159 -> 491,159 -> 491,167 -> 503,167 -> 503,159 -> 499,159 -> 499,157
|
||||||
|
512,139 -> 512,142 -> 509,142 -> 509,145 -> 521,145 -> 521,142 -> 517,142 -> 517,139
|
||||||
|
512,139 -> 512,142 -> 509,142 -> 509,145 -> 521,145 -> 521,142 -> 517,142 -> 517,139
|
||||||
|
503,45 -> 503,41 -> 503,45 -> 505,45 -> 505,43 -> 505,45 -> 507,45 -> 507,37 -> 507,45 -> 509,45 -> 509,35 -> 509,45 -> 511,45 -> 511,37 -> 511,45
|
||||||
|
503,45 -> 503,41 -> 503,45 -> 505,45 -> 505,43 -> 505,45 -> 507,45 -> 507,37 -> 507,45 -> 509,45 -> 509,35 -> 509,45 -> 511,45 -> 511,37 -> 511,45
|
||||||
|
494,24 -> 498,24
|
||||||
|
516,65 -> 516,69 -> 508,69 -> 508,74 -> 521,74 -> 521,69 -> 520,69 -> 520,65
|
||||||
|
494,157 -> 494,159 -> 491,159 -> 491,167 -> 503,167 -> 503,159 -> 499,159 -> 499,157
|
||||||
|
497,154 -> 501,154
|
||||||
|
508,62 -> 508,56 -> 508,62 -> 510,62 -> 510,52 -> 510,62 -> 512,62 -> 512,60 -> 512,62 -> 514,62 -> 514,58 -> 514,62 -> 516,62 -> 516,55 -> 516,62
|
||||||
|
523,80 -> 527,80
|
||||||
|
512,139 -> 512,142 -> 509,142 -> 509,145 -> 521,145 -> 521,142 -> 517,142 -> 517,139
|
||||||
|
505,17 -> 510,17
|
||||||
|
510,104 -> 510,96 -> 510,104 -> 512,104 -> 512,101 -> 512,104 -> 514,104 -> 514,100 -> 514,104 -> 516,104 -> 516,103 -> 516,104
|
||||||
|
498,17 -> 503,17
|
||||||
|
512,122 -> 512,118 -> 512,122 -> 514,122 -> 514,120 -> 514,122 -> 516,122 -> 516,121 -> 516,122
|
||||||
|
512,139 -> 512,142 -> 509,142 -> 509,145 -> 521,145 -> 521,142 -> 517,142 -> 517,139
|
||||||
|
494,157 -> 494,159 -> 491,159 -> 491,167 -> 503,167 -> 503,159 -> 499,159 -> 499,157
|
||||||
|
516,125 -> 516,129 -> 515,129 -> 515,136 -> 526,136 -> 526,129 -> 519,129 -> 519,125
|
||||||
|
510,104 -> 510,96 -> 510,104 -> 512,104 -> 512,101 -> 512,104 -> 514,104 -> 514,100 -> 514,104 -> 516,104 -> 516,103 -> 516,104
|
||||||
|
516,125 -> 516,129 -> 515,129 -> 515,136 -> 526,136 -> 526,129 -> 519,129 -> 519,125
|
||||||
|
509,31 -> 509,32 -> 513,32 -> 513,31
|
||||||
|
504,90 -> 504,91 -> 513,91 -> 513,90
|
||||||
|
493,48 -> 493,49 -> 512,49 -> 512,48
|
||||||
|
479,26 -> 483,26
|
||||||
|
488,24 -> 492,24
|
||||||
|
512,152 -> 516,152
|
||||||
|
491,26 -> 495,26
|
||||||
|
493,48 -> 493,49 -> 512,49 -> 512,48
|
||||||
|
503,45 -> 503,41 -> 503,45 -> 505,45 -> 505,43 -> 505,45 -> 507,45 -> 507,37 -> 507,45 -> 509,45 -> 509,35 -> 509,45 -> 511,45 -> 511,37 -> 511,45
|
||||||
|
512,122 -> 512,118 -> 512,122 -> 514,122 -> 514,120 -> 514,122 -> 516,122 -> 516,121 -> 516,122
|
||||||
|
517,80 -> 521,80
|
||||||
|
510,104 -> 510,96 -> 510,104 -> 512,104 -> 512,101 -> 512,104 -> 514,104 -> 514,100 -> 514,104 -> 516,104 -> 516,103 -> 516,104
|
||||||
|
503,45 -> 503,41 -> 503,45 -> 505,45 -> 505,43 -> 505,45 -> 507,45 -> 507,37 -> 507,45 -> 509,45 -> 509,35 -> 509,45 -> 511,45 -> 511,37 -> 511,45
|
||||||
|
508,62 -> 508,56 -> 508,62 -> 510,62 -> 510,52 -> 510,62 -> 512,62 -> 512,60 -> 512,62 -> 514,62 -> 514,58 -> 514,62 -> 516,62 -> 516,55 -> 516,62
|
||||||
|
494,157 -> 494,159 -> 491,159 -> 491,167 -> 503,167 -> 503,159 -> 499,159 -> 499,157
|
||||||
|
514,83 -> 518,83
|
||||||
|
510,104 -> 510,96 -> 510,104 -> 512,104 -> 512,101 -> 512,104 -> 514,104 -> 514,100 -> 514,104 -> 516,104 -> 516,103 -> 516,104
|
||||||
|
510,104 -> 510,96 -> 510,104 -> 512,104 -> 512,101 -> 512,104 -> 514,104 -> 514,100 -> 514,104 -> 516,104 -> 516,103 -> 516,104
|
||||||
|
529,86 -> 533,86
|
||||||
|
526,83 -> 530,83
|
||||||
|
497,13 -> 502,13
|
||||||
|
516,108 -> 516,109 -> 520,109 -> 520,108
|
||||||
|
512,139 -> 512,142 -> 509,142 -> 509,145 -> 521,145 -> 521,142 -> 517,142 -> 517,139
|
||||||
|
516,125 -> 516,129 -> 515,129 -> 515,136 -> 526,136 -> 526,129 -> 519,129 -> 519,125
|
||||||
|
508,62 -> 508,56 -> 508,62 -> 510,62 -> 510,52 -> 510,62 -> 512,62 -> 512,60 -> 512,62 -> 514,62 -> 514,58 -> 514,62 -> 516,62 -> 516,55 -> 516,62
|
||||||
|
503,154 -> 507,154
|
||||||
|
494,15 -> 499,15
|
||||||
|
520,77 -> 524,77
|
||||||
|
506,152 -> 510,152
|
||||||
|
516,65 -> 516,69 -> 508,69 -> 508,74 -> 521,74 -> 521,69 -> 520,69 -> 520,65
|
||||||
|
516,65 -> 516,69 -> 508,69 -> 508,74 -> 521,74 -> 521,69 -> 520,69 -> 520,65
|
||||||
|
503,45 -> 503,41 -> 503,45 -> 505,45 -> 505,43 -> 505,45 -> 507,45 -> 507,37 -> 507,45 -> 509,45 -> 509,35 -> 509,45 -> 511,45 -> 511,37 -> 511,45
|
||||||
|
494,157 -> 494,159 -> 491,159 -> 491,167 -> 503,167 -> 503,159 -> 499,159 -> 499,157
|
||||||
|
512,139 -> 512,142 -> 509,142 -> 509,145 -> 521,145 -> 521,142 -> 517,142 -> 517,139
|
||||||
|
508,62 -> 508,56 -> 508,62 -> 510,62 -> 510,52 -> 510,62 -> 512,62 -> 512,60 -> 512,62 -> 514,62 -> 514,58 -> 514,62 -> 516,62 -> 516,55 -> 516,62
|
||||||
|
493,48 -> 493,49 -> 512,49 -> 512,48
|
||||||
|
510,104 -> 510,96 -> 510,104 -> 512,104 -> 512,101 -> 512,104 -> 514,104 -> 514,100 -> 514,104 -> 516,104 -> 516,103 -> 516,104
|
||||||
|
516,108 -> 516,109 -> 520,109 -> 520,108
|
||||||
|
512,122 -> 512,118 -> 512,122 -> 514,122 -> 514,120 -> 514,122 -> 516,122 -> 516,121 -> 516,122
|
||||||
|
508,62 -> 508,56 -> 508,62 -> 510,62 -> 510,52 -> 510,62 -> 512,62 -> 512,60 -> 512,62 -> 514,62 -> 514,58 -> 514,62 -> 516,62 -> 516,55 -> 516,62
|
||||||
|
503,45 -> 503,41 -> 503,45 -> 505,45 -> 505,43 -> 505,45 -> 507,45 -> 507,37 -> 507,45 -> 509,45 -> 509,35 -> 509,45 -> 511,45 -> 511,37 -> 511,45
|
||||||
|
503,45 -> 503,41 -> 503,45 -> 505,45 -> 505,43 -> 505,45 -> 507,45 -> 507,37 -> 507,45 -> 509,45 -> 509,35 -> 509,45 -> 511,45 -> 511,37 -> 511,45
|
||||||
|
508,62 -> 508,56 -> 508,62 -> 510,62 -> 510,52 -> 510,62 -> 512,62 -> 512,60 -> 512,62 -> 514,62 -> 514,58 -> 514,62 -> 516,62 -> 516,55 -> 516,62
|
||||||
|
508,62 -> 508,56 -> 508,62 -> 510,62 -> 510,52 -> 510,62 -> 512,62 -> 512,60 -> 512,62 -> 514,62 -> 514,58 -> 514,62 -> 516,62 -> 516,55 -> 516,62
|
||||||
|
508,62 -> 508,56 -> 508,62 -> 510,62 -> 510,52 -> 510,62 -> 512,62 -> 512,60 -> 512,62 -> 514,62 -> 514,58 -> 514,62 -> 516,62 -> 516,55 -> 516,62
|
||||||
|
488,20 -> 492,20
|
||||||
|
500,152 -> 504,152
|
||||||
|
509,31 -> 509,32 -> 513,32 -> 513,31
|
||||||
|
509,154 -> 513,154
|
||||||
|
503,45 -> 503,41 -> 503,45 -> 505,45 -> 505,43 -> 505,45 -> 507,45 -> 507,37 -> 507,45 -> 509,45 -> 509,35 -> 509,45 -> 511,45 -> 511,37 -> 511,45
|
||||||
|
508,62 -> 508,56 -> 508,62 -> 510,62 -> 510,52 -> 510,62 -> 512,62 -> 512,60 -> 512,62 -> 514,62 -> 514,58 -> 514,62 -> 516,62 -> 516,55 -> 516,62
|
||||||
|
516,125 -> 516,129 -> 515,129 -> 515,136 -> 526,136 -> 526,129 -> 519,129 -> 519,125
|
||||||
|
523,86 -> 527,86
|
||||||
|
516,125 -> 516,129 -> 515,129 -> 515,136 -> 526,136 -> 526,129 -> 519,129 -> 519,125
|
||||||
|
491,22 -> 495,22
|
||||||
|
497,26 -> 501,26
|
||||||
|
506,148 -> 510,148
|
||||||
|
516,65 -> 516,69 -> 508,69 -> 508,74 -> 521,74 -> 521,69 -> 520,69 -> 520,65
|
||||||
|
512,122 -> 512,118 -> 512,122 -> 514,122 -> 514,120 -> 514,122 -> 516,122 -> 516,121 -> 516,122
|
||||||
|
503,45 -> 503,41 -> 503,45 -> 505,45 -> 505,43 -> 505,45 -> 507,45 -> 507,37 -> 507,45 -> 509,45 -> 509,35 -> 509,45 -> 511,45 -> 511,37 -> 511,45
|
||||||
|
510,104 -> 510,96 -> 510,104 -> 512,104 -> 512,101 -> 512,104 -> 514,104 -> 514,100 -> 514,104 -> 516,104 -> 516,103 -> 516,104
|
||||||
|
510,104 -> 510,96 -> 510,104 -> 512,104 -> 512,101 -> 512,104 -> 514,104 -> 514,100 -> 514,104 -> 516,104 -> 516,103 -> 516,104
|
||||||
|
503,45 -> 503,41 -> 503,45 -> 505,45 -> 505,43 -> 505,45 -> 507,45 -> 507,37 -> 507,45 -> 509,45 -> 509,35 -> 509,45 -> 511,45 -> 511,37 -> 511,45
|
||||||
|
508,62 -> 508,56 -> 508,62 -> 510,62 -> 510,52 -> 510,62 -> 512,62 -> 512,60 -> 512,62 -> 514,62 -> 514,58 -> 514,62 -> 516,62 -> 516,55 -> 516,62
|
||||||
|
494,157 -> 494,159 -> 491,159 -> 491,167 -> 503,167 -> 503,159 -> 499,159 -> 499,157
|
||||||
|
516,108 -> 516,109 -> 520,109 -> 520,108
|
||||||
|
501,15 -> 506,15
|
||||||
|
516,125 -> 516,129 -> 515,129 -> 515,136 -> 526,136 -> 526,129 -> 519,129 -> 519,125
|
||||||
|
491,17 -> 496,17
|
||||||
|
516,65 -> 516,69 -> 508,69 -> 508,74 -> 521,74 -> 521,69 -> 520,69 -> 520,65
|
||||||
|
508,62 -> 508,56 -> 508,62 -> 510,62 -> 510,52 -> 510,62 -> 512,62 -> 512,60 -> 512,62 -> 514,62 -> 514,58 -> 514,62 -> 516,62 -> 516,55 -> 516,62
|
||||||
|
512,122 -> 512,118 -> 512,122 -> 514,122 -> 514,120 -> 514,122 -> 516,122 -> 516,121 -> 516,122
|
||||||
|
512,139 -> 512,142 -> 509,142 -> 509,145 -> 521,145 -> 521,142 -> 517,142 -> 517,139
|
||||||
|
504,90 -> 504,91 -> 513,91 -> 513,90
|
||||||
|
503,45 -> 503,41 -> 503,45 -> 505,45 -> 505,43 -> 505,45 -> 507,45 -> 507,37 -> 507,45 -> 509,45 -> 509,35 -> 509,45 -> 511,45 -> 511,37 -> 511,45
|
||||||
|
508,62 -> 508,56 -> 508,62 -> 510,62 -> 510,52 -> 510,62 -> 512,62 -> 512,60 -> 512,62 -> 514,62 -> 514,58 -> 514,62 -> 516,62 -> 516,55 -> 516,62
|
||||||
|
482,24 -> 486,24
|
||||||
|
510,104 -> 510,96 -> 510,104 -> 512,104 -> 512,101 -> 512,104 -> 514,104 -> 514,100 -> 514,104 -> 516,104 -> 516,103 -> 516,104
|
||||||
|
509,31 -> 509,32 -> 513,32 -> 513,31
|
||||||
|
516,65 -> 516,69 -> 508,69 -> 508,74 -> 521,74 -> 521,69 -> 520,69 -> 520,65
|
||||||
|
512,122 -> 512,118 -> 512,122 -> 514,122 -> 514,120 -> 514,122 -> 516,122 -> 516,121 -> 516,122
|
||||||
|
515,154 -> 519,154
|
||||||
|
517,86 -> 521,86
|
||||||
|
485,26 -> 489,26
|
||||||
|
504,90 -> 504,91 -> 513,91 -> 513,90
|
||||||
|
510,104 -> 510,96 -> 510,104 -> 512,104 -> 512,101 -> 512,104 -> 514,104 -> 514,100 -> 514,104 -> 516,104 -> 516,103 -> 516,104
|
||||||
|
510,104 -> 510,96 -> 510,104 -> 512,104 -> 512,101 -> 512,104 -> 514,104 -> 514,100 -> 514,104 -> 516,104 -> 516,103 -> 516,104
|
||||||
|
503,150 -> 507,150
|
||||||
|
520,83 -> 524,83
|
||||||
|
508,62 -> 508,56 -> 508,62 -> 510,62 -> 510,52 -> 510,62 -> 512,62 -> 512,60 -> 512,62 -> 514,62 -> 514,58 -> 514,62 -> 516,62 -> 516,55 -> 516,62
|
10
2022/day14/rust/Cargo.toml
Normal file
10
2022/day14/rust/Cargo.toml
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
[package]
|
||||||
|
name = "rust_2022_14"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
aoc = { path = "../../../common/rust" }
|
||||||
|
itertools = "0.10.5"
|
80
2022/day14/rust/src/main.rs
Normal file
80
2022/day14/rust/src/main.rs
Normal file
|
@ -0,0 +1,80 @@
|
||||||
|
#![warn(clippy::pedantic)]
|
||||||
|
|
||||||
|
use std::{collections::HashSet, io::stdin};
|
||||||
|
|
||||||
|
use aoc::vec2::{Line, Vec2};
|
||||||
|
use itertools::Itertools;
|
||||||
|
|
||||||
|
fn parse_points(s: &str) -> impl Iterator<Item = Vec2> + '_ {
|
||||||
|
s.split(" -> ").map(|p| {
|
||||||
|
let (x, y) = p.split_once(',').unwrap();
|
||||||
|
let x = x.parse().unwrap();
|
||||||
|
let y = y.parse().unwrap();
|
||||||
|
(x, y).into()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn simulate(structures: HashSet<Vec2>, floor: Option<usize>) -> usize {
|
||||||
|
let mut points = structures;
|
||||||
|
let lowest = points.iter().map(|v| v.y).max().unwrap();
|
||||||
|
let start = Vec2::new(500, 0);
|
||||||
|
|
||||||
|
let mut pieces = 0;
|
||||||
|
'pieces: loop {
|
||||||
|
let mut pos = start;
|
||||||
|
'fall: loop {
|
||||||
|
let new_pos = [(0, 1), (-1, 1), (1, 1)]
|
||||||
|
.into_iter()
|
||||||
|
.map(|direction| pos + direction.into())
|
||||||
|
.find(|pos| !points.contains(pos));
|
||||||
|
|
||||||
|
if let Some(new_pos) = new_pos {
|
||||||
|
pos = new_pos;
|
||||||
|
|
||||||
|
match floor {
|
||||||
|
Some(offset) => {
|
||||||
|
if pos.y >= lowest + i32::try_from(offset).unwrap() - 1 {
|
||||||
|
// landed on the floor
|
||||||
|
break 'fall;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
if pos.y >= lowest {
|
||||||
|
// fell into the void
|
||||||
|
break 'pieces;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// stuck
|
||||||
|
break 'fall;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
points.insert(pos);
|
||||||
|
pieces += 1;
|
||||||
|
|
||||||
|
if pos == start {
|
||||||
|
// all filled up
|
||||||
|
break 'pieces;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pieces
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let points: HashSet<Vec2> = stdin()
|
||||||
|
.lines()
|
||||||
|
.flat_map(|line| {
|
||||||
|
parse_points(&line.unwrap())
|
||||||
|
.tuple_windows()
|
||||||
|
.map(|(start, end)| Line { start, end })
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
})
|
||||||
|
.flat_map(|line| line.points().unwrap())
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
println!("{}", simulate(points.clone(), None));
|
||||||
|
println!("{}", simulate(points, Some(2)));
|
||||||
|
}
|
8
Cargo.lock
generated
8
Cargo.lock
generated
|
@ -593,6 +593,14 @@ dependencies = [
|
||||||
"nom",
|
"nom",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rust_2022_14"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"aoc",
|
||||||
|
"itertools",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ryu"
|
name = "ryu"
|
||||||
version = "1.0.11"
|
version = "1.0.11"
|
||||||
|
|
|
@ -23,4 +23,5 @@ members = [
|
||||||
"2022/day11/rust",
|
"2022/day11/rust",
|
||||||
"2022/day12/rust",
|
"2022/day12/rust",
|
||||||
"2022/day13/rust",
|
"2022/day13/rust",
|
||||||
|
"2022/day14/rust",
|
||||||
]
|
]
|
||||||
|
|
|
@ -6,8 +6,8 @@ use std::{
|
||||||
|
|
||||||
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||||
pub struct Vec2 {
|
pub struct Vec2 {
|
||||||
x: i32, // increases toward right
|
pub x: i32, // increases toward right
|
||||||
y: i32, // increases toward top
|
pub y: i32, // increases toward top
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Vec2 {
|
impl Vec2 {
|
||||||
|
@ -140,3 +140,57 @@ impl FromStr for Direction {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A line from one point to another.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub struct Line {
|
||||||
|
pub start: Vec2,
|
||||||
|
pub end: Vec2,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Line {
|
||||||
|
/// Returns an iterator over the line's points represented as [`Vec2`], including the start and
|
||||||
|
/// end points.
|
||||||
|
///
|
||||||
|
/// Only works for orthogonal lines.
|
||||||
|
#[must_use]
|
||||||
|
pub fn points(self) -> Option<LinePoints> {
|
||||||
|
LinePoints::try_from(self).ok()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// An iterator over a line's points. See [`Line::points()`] for details.
|
||||||
|
pub struct LinePoints {
|
||||||
|
line: Option<Line>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TryFrom<Line> for LinePoints {
|
||||||
|
type Error = ();
|
||||||
|
|
||||||
|
fn try_from(line: Line) -> Result<Self, Self::Error> {
|
||||||
|
if line.start.x == line.end.x || line.start.y == line.end.y {
|
||||||
|
Ok(Self { line: Some(line) })
|
||||||
|
} else {
|
||||||
|
Err(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Iterator for LinePoints {
|
||||||
|
type Item = Vec2;
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
let Some(line) = self.line.as_mut() else { return None };
|
||||||
|
|
||||||
|
let delta = (line.end - line.start).map(i32::signum);
|
||||||
|
|
||||||
|
let p = line.start;
|
||||||
|
line.start += delta;
|
||||||
|
|
||||||
|
if delta == Vec2::new(0, 0) {
|
||||||
|
self.line = None;
|
||||||
|
}
|
||||||
|
|
||||||
|
Some(p)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue