2022-12-16 23:55:32 +00:00
|
|
|
use std::cmp;
|
2022-12-16 23:40:44 +00:00
|
|
|
use std::collections::HashMap;
|
2022-12-16 23:55:32 +00:00
|
|
|
use std::convert::TryFrom;
|
|
|
|
use std::fs;
|
|
|
|
use std::ops::Deref;
|
2022-12-16 23:40:44 +00:00
|
|
|
use std::ops::RangeInclusive;
|
|
|
|
use std::ops::Sub;
|
2022-12-16 23:55:32 +00:00
|
|
|
use std::panic;
|
2022-12-16 23:40:44 +00:00
|
|
|
use std::process;
|
2022-12-16 23:55:32 +00:00
|
|
|
use std::str;
|
2022-12-16 23:40:44 +00:00
|
|
|
use std::sync::Arc;
|
2022-12-16 23:55:32 +00:00
|
|
|
use std::thread;
|
2022-12-16 23:40:44 +00:00
|
|
|
|
2022-12-16 23:55:32 +00:00
|
|
|
#[derive(Default, Debug, PartialEq)]
|
2022-12-16 23:40:44 +00:00
|
|
|
struct Location {
|
|
|
|
x: isize,
|
2022-12-16 23:55:32 +00:00
|
|
|
y: isize,
|
2022-12-16 23:40:44 +00:00
|
|
|
}
|
|
|
|
impl<'a> Sub for &'a Location {
|
|
|
|
type Output = isize;
|
|
|
|
|
|
|
|
fn sub(self, other: &'a Location) -> isize {
|
|
|
|
(self.x - other.x).abs() + (self.y - other.y).abs()
|
|
|
|
}
|
|
|
|
}
|
2022-12-16 23:55:32 +00:00
|
|
|
#[derive(Default, PartialEq)]
|
2022-12-16 23:40:44 +00:00
|
|
|
struct Sensor {
|
|
|
|
loc: Location,
|
|
|
|
visibility: usize,
|
|
|
|
}
|
2022-12-16 23:55:32 +00:00
|
|
|
#[derive(Default, Debug, PartialEq)]
|
2022-12-16 23:40:44 +00:00
|
|
|
struct Beacon {
|
2022-12-16 23:55:32 +00:00
|
|
|
loc: Location,
|
2022-12-16 23:40:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ComparableRange(RangeInclusive<isize>);
|
|
|
|
|
|
|
|
impl ComparableRange {
|
2022-12-16 23:55:32 +00:00
|
|
|
pub fn intersects(&self, other: &ComparableRange) -> bool {
|
|
|
|
self.contains(other.start()) || other.contains(self.start())
|
2022-12-16 23:40:44 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 23:55:32 +00:00
|
|
|
pub fn intersection(&self, other: &ComparableRange) -> ComparableRange {
|
2022-12-16 23:40:44 +00:00
|
|
|
match self.intersects(other) {
|
|
|
|
false => panic!("Ranges do not intersect"),
|
2022-12-16 23:55:32 +00:00
|
|
|
true => ComparableRange(
|
|
|
|
cmp::min(*self.start(), *other.start())..=cmp::max(*self.end(), *other.end()),
|
|
|
|
),
|
2022-12-16 23:40:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for ComparableRange {
|
|
|
|
type Target = RangeInclusive<isize>;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-16 23:55:32 +00:00
|
|
|
const PART_1_CONST: isize = 2000000;
|
|
|
|
const PART_2_CONST: isize = 4000000;
|
2022-12-16 23:40:44 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
run();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run() {
|
|
|
|
let contents = fs::read_to_string("15").unwrap();
|
|
|
|
process_part_1(contents.lines());
|
|
|
|
process_part_2(contents.lines());
|
|
|
|
}
|
|
|
|
|
2022-12-16 23:55:32 +00:00
|
|
|
fn process_part_2<'a, T>(lines: T)
|
|
|
|
where
|
|
|
|
T: IntoIterator<Item = &'a str>,
|
|
|
|
{
|
2022-12-16 23:52:37 +00:00
|
|
|
let (sensors, worker_bundles) = prep_work(lines);
|
2022-12-16 23:40:44 +00:00
|
|
|
|
|
|
|
let mut handles = Vec::new();
|
|
|
|
let bundle_storage = Arc::new(worker_bundles);
|
|
|
|
let sensor_storage = Arc::new(sensors);
|
|
|
|
for worker in 0..=99 {
|
|
|
|
let worker_bundles = Arc::clone(&bundle_storage);
|
|
|
|
let sensors = Arc::clone(&sensor_storage);
|
|
|
|
let bounds = 0..=PART_2_CONST;
|
2022-12-16 23:55:32 +00:00
|
|
|
let handle = thread::spawn(move || do_part2_work(worker_bundles, worker, &bounds, sensors));
|
2022-12-16 23:40:44 +00:00
|
|
|
handles.push(handle);
|
|
|
|
}
|
2022-12-16 23:55:32 +00:00
|
|
|
handles.into_iter().for_each(|handle| {
|
|
|
|
handle.join();
|
|
|
|
});
|
2022-12-16 23:40:44 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 23:55:32 +00:00
|
|
|
fn prep_work<'a, T>(lines: T) -> (Vec<Sensor>, HashMap<isize, Vec<usize>>)
|
|
|
|
where
|
|
|
|
T: IntoIterator<Item = &'a str>,
|
|
|
|
{
|
2022-12-16 23:52:37 +00:00
|
|
|
let (sensors, _) = parse(lines);
|
|
|
|
let mut worker_bundles = HashMap::new();
|
|
|
|
for worker in 0..99 {
|
|
|
|
let mut work_item = Vec::new();
|
|
|
|
for sensor in &sensors {
|
|
|
|
let chunk_s = sensor.visibility / 100;
|
|
|
|
work_item.push(chunk_s);
|
|
|
|
}
|
|
|
|
worker_bundles.insert(worker, work_item);
|
2022-12-16 23:55:32 +00:00
|
|
|
}
|
2022-12-16 23:52:37 +00:00
|
|
|
// remainder worker
|
|
|
|
let worker = 99;
|
|
|
|
let mut work_item = Vec::new();
|
|
|
|
for sensor in &sensors {
|
|
|
|
let chunk_s = sensor.visibility / 100;
|
2022-12-16 23:55:32 +00:00
|
|
|
let remainder = sensor.visibility - (99 * chunk_s);
|
2022-12-16 23:52:37 +00:00
|
|
|
work_item.push(remainder);
|
|
|
|
}
|
|
|
|
worker_bundles.insert(worker, work_item);
|
|
|
|
(sensors, worker_bundles)
|
|
|
|
}
|
|
|
|
|
2022-12-16 23:55:32 +00:00
|
|
|
fn do_part2_work(
|
|
|
|
worker_bundles: Arc<HashMap<isize, Vec<usize>>>,
|
|
|
|
worker: isize,
|
|
|
|
bounds: &RangeInclusive<isize>,
|
|
|
|
sensors: Arc<Vec<Sensor>>,
|
|
|
|
) {
|
|
|
|
worker_bundles.get(&worker).and_then(|x| {
|
|
|
|
for (work_idx, work_item) in x.into_iter().enumerate() {
|
2022-12-16 23:40:44 +00:00
|
|
|
for idx in 0..*work_item {
|
2022-12-16 23:55:32 +00:00
|
|
|
let dist =
|
|
|
|
isize::try_from(idx).unwrap() + worker * isize::try_from(*work_item).unwrap();
|
2022-12-16 23:40:44 +00:00
|
|
|
let sensor = &sensors[work_idx];
|
|
|
|
let visibility = isize::try_from(sensor.visibility).unwrap();
|
2022-12-16 23:55:32 +00:00
|
|
|
let x_left = sensor.loc.x - dist;
|
|
|
|
let x_right = sensor.loc.x + dist;
|
|
|
|
let y_lower = sensor.loc.y + (visibility - dist) + 1;
|
|
|
|
let y_upper = sensor.loc.y - (visibility - dist) - 1;
|
|
|
|
[
|
|
|
|
(x_left, y_upper),
|
|
|
|
(x_right, y_upper),
|
|
|
|
(x_left, y_lower),
|
|
|
|
(x_right, y_lower),
|
|
|
|
]
|
|
|
|
.into_iter()
|
|
|
|
.for_each(|coord| {
|
|
|
|
if !(bounds).contains(&coord.0) || !(bounds).contains(&coord.1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if sensors.iter().all(|tgt_sensor| {
|
|
|
|
&tgt_sensor.loc
|
|
|
|
- &Location {
|
|
|
|
x: coord.0,
|
|
|
|
y: coord.1,
|
|
|
|
}
|
|
|
|
> isize::try_from(tgt_sensor.visibility).unwrap()
|
2022-12-16 23:40:44 +00:00
|
|
|
}) {
|
2022-12-16 23:55:32 +00:00
|
|
|
println!("{:?}", coord.0 * 4000000 + coord.1);
|
2022-12-16 23:40:44 +00:00
|
|
|
process::exit(0);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-12-16 23:55:32 +00:00
|
|
|
fn process_part_1<'a, T>(lines: T)
|
|
|
|
where
|
|
|
|
T: IntoIterator<Item = &'a str>,
|
|
|
|
{
|
2022-12-16 23:40:44 +00:00
|
|
|
let (sensors, beacons) = parse(lines);
|
|
|
|
let target_row = PART_1_CONST;
|
2022-12-16 23:55:32 +00:00
|
|
|
let mut impossible_ranges: Vec<ComparableRange> = Vec::new();
|
2022-12-16 23:40:44 +00:00
|
|
|
for sensor in (&sensors).into_iter() {
|
|
|
|
let visibility = isize::try_from(sensor.visibility).unwrap();
|
2022-12-16 23:55:32 +00:00
|
|
|
let dist_to_target = &sensor.loc
|
|
|
|
- &(Location {
|
|
|
|
x: sensor.loc.x,
|
|
|
|
y: target_row,
|
|
|
|
});
|
|
|
|
if dist_to_target > visibility {
|
|
|
|
continue;
|
|
|
|
}
|
2022-12-16 23:40:44 +00:00
|
|
|
|
|
|
|
let impossible_width = visibility - dist_to_target;
|
2022-12-16 23:55:32 +00:00
|
|
|
let impossible_range = sensor.loc.x - impossible_width..=sensor.loc.x + impossible_width;
|
2022-12-16 23:40:44 +00:00
|
|
|
impossible_ranges.push(ComparableRange(impossible_range));
|
2022-12-16 23:55:32 +00:00
|
|
|
}
|
|
|
|
impossible_ranges.sort_by(|a, b| a.start().cmp(b.start()));
|
2022-12-16 23:40:44 +00:00
|
|
|
|
|
|
|
merge(&mut impossible_ranges);
|
|
|
|
|
2022-12-16 23:55:32 +00:00
|
|
|
let beacons_in_row = beacons
|
|
|
|
.into_iter()
|
|
|
|
.filter(|b| {
|
|
|
|
if b.loc.y != target_row {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
(&impossible_ranges)
|
|
|
|
.into_iter()
|
|
|
|
.any(|r| r.contains(&b.loc.x))
|
|
|
|
})
|
|
|
|
.count();
|
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
impossible_ranges
|
|
|
|
.into_iter()
|
|
|
|
.map(|r| r.end() - r.start() + 1)
|
|
|
|
.sum::<isize>()
|
|
|
|
- isize::try_from(beacons_in_row).unwrap()
|
|
|
|
);
|
2022-12-16 23:40:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn merge(impossible_ranges: &mut Vec<ComparableRange>) {
|
|
|
|
let mut merge_idx = 0;
|
2022-12-16 23:55:32 +00:00
|
|
|
while merge_idx < impossible_ranges.len() - 1 {
|
|
|
|
if (*impossible_ranges)[merge_idx].intersects(&impossible_ranges[merge_idx + 1]) {
|
2022-12-16 23:40:44 +00:00
|
|
|
let r1 = &impossible_ranges[merge_idx];
|
2022-12-16 23:55:32 +00:00
|
|
|
let r2 = &impossible_ranges[merge_idx + 1];
|
|
|
|
impossible_ranges.insert(merge_idx + 2, r1.intersection(&r2));
|
|
|
|
impossible_ranges.remove(merge_idx + 1);
|
2022-12-16 23:40:44 +00:00
|
|
|
impossible_ranges.remove(merge_idx);
|
|
|
|
} else {
|
|
|
|
merge_idx += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse<'a, T>(lines: T) -> (Vec<Sensor>, Vec<Beacon>)
|
2022-12-16 23:55:32 +00:00
|
|
|
where
|
|
|
|
T: IntoIterator<Item = &'a str>,
|
|
|
|
{
|
2022-12-16 23:40:44 +00:00
|
|
|
let mut sensors = Vec::new();
|
|
|
|
let mut beacons = Vec::new();
|
|
|
|
let extract_ints = |s: &str| {
|
2022-12-16 23:55:32 +00:00
|
|
|
s.split(&['=', ',', ':'][..])
|
2022-12-16 23:40:44 +00:00
|
|
|
.filter_map(|s| s.parse::<isize>().ok())
|
|
|
|
.collect::<Vec<isize>>()
|
|
|
|
};
|
|
|
|
let loc_sets = lines.into_iter().map(extract_ints);
|
|
|
|
loc_sets.for_each(|loc_set| {
|
2022-12-16 23:55:32 +00:00
|
|
|
let sensor_loc = Location {
|
|
|
|
x: loc_set[0],
|
|
|
|
y: loc_set[1],
|
|
|
|
};
|
|
|
|
let beacon_loc = Location {
|
|
|
|
x: loc_set[2],
|
|
|
|
y: loc_set[3],
|
|
|
|
};
|
|
|
|
let visibility = usize::try_from(&sensor_loc - &beacon_loc).unwrap();
|
|
|
|
sensors.push(Sensor {
|
2022-12-16 23:40:44 +00:00
|
|
|
loc: sensor_loc,
|
2022-12-16 23:55:32 +00:00
|
|
|
visibility: visibility,
|
2022-12-16 23:40:44 +00:00
|
|
|
});
|
2022-12-16 23:55:32 +00:00
|
|
|
let beacon = Beacon { loc: beacon_loc };
|
2022-12-16 23:40:44 +00:00
|
|
|
if !beacons.contains(&beacon) {
|
|
|
|
beacons.push(beacon);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
(sensors, beacons)
|
2022-12-16 23:55:32 +00:00
|
|
|
}
|