51 lines
1.3 KiB
Rust
51 lines
1.3 KiB
Rust
use std::ops::RangeInclusive;
|
|
|
|
use regex::Regex;
|
|
|
|
use crate::util::{Coordinate, SignedCoord};
|
|
|
|
#[derive(Debug)]
|
|
pub struct Sensor {
|
|
pub loc_s: SignedCoord,
|
|
pub loc_b: SignedCoord,
|
|
pub range: usize,
|
|
}
|
|
|
|
impl Sensor {
|
|
pub fn covered(&self, y: isize) -> RangeInclusive<isize> {
|
|
let dy = self.loc_s.0.abs_diff(y);
|
|
if dy > self.range {
|
|
1..=0
|
|
} else {
|
|
let range_x = (self.range - dy) as isize;
|
|
(self.loc_s.1 - range_x)..=(self.loc_s.1 + range_x)
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn parse_sensors(input: &String) -> Vec<Sensor> {
|
|
let mut result = Vec::new();
|
|
|
|
let re =
|
|
Regex::new(r"^Sensor at x=(-?\d+), y=(-?\d+): closest beacon is at x=(-?\d+), y=(-?\d+)$")
|
|
.unwrap();
|
|
for line in input.lines() {
|
|
let captures = re.captures(line).unwrap();
|
|
let loc_s: SignedCoord = (
|
|
captures.get(2).unwrap().as_str().parse().unwrap(),
|
|
captures.get(1).unwrap().as_str().parse().unwrap(),
|
|
);
|
|
let loc_b: SignedCoord = (
|
|
captures.get(4).unwrap().as_str().parse().unwrap(),
|
|
captures.get(3).unwrap().as_str().parse().unwrap(),
|
|
);
|
|
result.push(Sensor {
|
|
loc_s,
|
|
loc_b,
|
|
range: loc_s.manhattan(&loc_b),
|
|
});
|
|
}
|
|
|
|
result
|
|
}
|