2022 day15/rust: add bad part1 solution
This commit is contained in:
parent
3d47372967
commit
633591acf8
4 changed files with 103 additions and 0 deletions
9
2022/day15/rust/Cargo.toml
Normal file
9
2022/day15/rust/Cargo.toml
Normal file
|
@ -0,0 +1,9 @@
|
|||
[package]
|
||||
name = "rust_2022_15"
|
||||
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" }
|
86
2022/day15/rust/src/main.rs
Normal file
86
2022/day15/rust/src/main.rs
Normal file
|
@ -0,0 +1,86 @@
|
|||
#![warn(clippy::pedantic)]
|
||||
|
||||
use std::io::stdin;
|
||||
|
||||
use aoc::vec2::{Line, Vec2};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
struct Sensor {
|
||||
position: Vec2,
|
||||
closest_beacon: Vec2,
|
||||
}
|
||||
|
||||
impl Sensor {
|
||||
fn beacon_distance(self) -> usize {
|
||||
self.closest_beacon.manhattan_dist(self.position)
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let sensors: Vec<_> = stdin()
|
||||
.lines()
|
||||
.map(|line| {
|
||||
let line = line.unwrap();
|
||||
let (sensor, beacon) = line.split_once(": ").unwrap();
|
||||
let point = |s: &str| {
|
||||
let (x, y) = s.split_once(", ").unwrap();
|
||||
let x = x.strip_prefix("x=").unwrap().parse().unwrap();
|
||||
let y = y.strip_prefix("y=").unwrap().parse().unwrap();
|
||||
(x, y).into()
|
||||
};
|
||||
let sensor = sensor.strip_prefix("Sensor at ").unwrap();
|
||||
let beacon = beacon.strip_prefix("closest beacon is at ").unwrap();
|
||||
|
||||
Sensor {
|
||||
position: point(sensor),
|
||||
closest_beacon: point(beacon),
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
for sensor in &sensors {
|
||||
let dist = sensor.beacon_distance();
|
||||
let candidates_x = sensor
|
||||
.position
|
||||
.on_x_with_manhattan_dist(sensor.closest_beacon.x, dist)
|
||||
.unwrap();
|
||||
let candidates_y = sensor
|
||||
.position
|
||||
.on_y_with_manhattan_dist(sensor.closest_beacon.y, dist)
|
||||
.unwrap();
|
||||
let candidates = [
|
||||
candidates_x.0,
|
||||
candidates_x.1,
|
||||
candidates_y.0,
|
||||
candidates_y.1,
|
||||
];
|
||||
assert!(candidates.contains(&sensor.closest_beacon));
|
||||
}
|
||||
let border_points = sensors.iter().filter_map(|sensor| {
|
||||
sensor
|
||||
.position
|
||||
.on_y_with_manhattan_dist(2_000_000, sensor.beacon_distance())
|
||||
});
|
||||
|
||||
let leftmost = border_points
|
||||
.clone()
|
||||
.map(|(left, _right)| left)
|
||||
.min()
|
||||
.unwrap();
|
||||
let rightmost = border_points.map(|(_left, right)| right).max().unwrap();
|
||||
|
||||
let count = Line {
|
||||
start: leftmost,
|
||||
end: rightmost,
|
||||
}
|
||||
.points()
|
||||
.unwrap()
|
||||
.filter(|&p| {
|
||||
sensors
|
||||
.iter()
|
||||
.any(|s| p != s.closest_beacon && s.position.manhattan_dist(p) <= s.beacon_distance())
|
||||
})
|
||||
.count();
|
||||
|
||||
println!("{:?}", count);
|
||||
}
|
7
Cargo.lock
generated
7
Cargo.lock
generated
|
@ -601,6 +601,13 @@ dependencies = [
|
|||
"itertools",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rust_2022_15"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"aoc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ryu"
|
||||
version = "1.0.11"
|
||||
|
|
|
@ -24,4 +24,5 @@ members = [
|
|||
"2022/day12/rust",
|
||||
"2022/day13/rust",
|
||||
"2022/day14/rust",
|
||||
"2022/day15/rust",
|
||||
]
|
||||
|
|
Loading…
Reference in a new issue