Day 15, puzzle 1
This commit is contained in:
39
src/bin/d15p1.rs
Normal file
39
src/bin/d15p1.rs
Normal 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
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user