Day 15, puzzle 1

This commit is contained in:
jazzpi
2022-12-16 10:47:17 +01:00
parent d05780e64a
commit 619c77aab7
4 changed files with 126 additions and 0 deletions

39
src/bin/d15p1.rs Normal file
View File

@ -0,0 +1,39 @@
use aoc22::{day15, util};
use itertools::Itertools;
const ROW: isize = 2000000;
pub fn main() {
let sensors = day15::parse_sensors(&util::parse_input());
let covered = sensors
.iter()
.map(|s| s.covered(ROW))
.filter(|r| !r.is_empty())
.sorted_by_key(|r| *r.start());
let mut max_x = isize::MIN;
let mut total_covered = 0;
for range in covered {
let (min, max) = range.into_inner();
if min > max_x {
total_covered += max - min + 1;
} else if max > max_x {
total_covered += max - max_x;
}
max_x = max_x.max(max);
}
let n_beacons = sensors
.iter()
.map(|s| s.loc_b)
.filter(|b| b.0 == ROW)
.unique()
.count() as isize;
println!(
"Impossible positions in row {}: {}",
ROW,
total_covered - n_beacons
);
}