From 633591acf8c10d0a19e7769ac4e8e56214aa0851 Mon Sep 17 00:00:00 2001 From: Xiretza Date: Thu, 15 Dec 2022 20:11:55 +0100 Subject: [PATCH] 2022 day15/rust: add bad part1 solution --- 2022/day15/rust/Cargo.toml | 9 ++++ 2022/day15/rust/src/main.rs | 86 +++++++++++++++++++++++++++++++++++++ Cargo.lock | 7 +++ Cargo.toml | 1 + 4 files changed, 103 insertions(+) create mode 100644 2022/day15/rust/Cargo.toml create mode 100644 2022/day15/rust/src/main.rs diff --git a/2022/day15/rust/Cargo.toml b/2022/day15/rust/Cargo.toml new file mode 100644 index 0000000..f977f84 --- /dev/null +++ b/2022/day15/rust/Cargo.toml @@ -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" } diff --git a/2022/day15/rust/src/main.rs b/2022/day15/rust/src/main.rs new file mode 100644 index 0000000..6111147 --- /dev/null +++ b/2022/day15/rust/src/main.rs @@ -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); +} diff --git a/Cargo.lock b/Cargo.lock index 606b8a9..3819068 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -601,6 +601,13 @@ dependencies = [ "itertools", ] +[[package]] +name = "rust_2022_15" +version = "0.1.0" +dependencies = [ + "aoc", +] + [[package]] name = "ryu" version = "1.0.11" diff --git a/Cargo.toml b/Cargo.toml index 7132ee8..ea046e5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,4 +24,5 @@ members = [ "2022/day12/rust", "2022/day13/rust", "2022/day14/rust", + "2022/day15/rust", ]