1
0
Fork 0

day 15 part 1 solution

main
Andrew Coleman 2022-12-18 15:44:54 -05:00
parent 5113eba2a9
commit 8f46feaf69
5 changed files with 132 additions and 0 deletions

31
2022/day15.txt Normal file
View File

@ -0,0 +1,31 @@
Sensor at x=3923513, y=2770279: closest beacon is at x=3866712, y=2438950
Sensor at x=675683, y=3223762: closest beacon is at x=-224297, y=2997209
Sensor at x=129453, y=2652332: closest beacon is at x=92656, y=2629486
Sensor at x=3906125, y=2154618: closest beacon is at x=3866712, y=2438950
Sensor at x=65723, y=902062: closest beacon is at x=92656, y=2629486
Sensor at x=3137156, y=2876347: closest beacon is at x=2907507, y=3100765
Sensor at x=32848, y=2676435: closest beacon is at x=92656, y=2629486
Sensor at x=3272472, y=3445147: closest beacon is at x=2907507, y=3100765
Sensor at x=2926008, y=128948: closest beacon is at x=3089364, y=-501737
Sensor at x=2975, y=2769838: closest beacon is at x=92656, y=2629486
Sensor at x=3540455, y=2469135: closest beacon is at x=3866712, y=2438950
Sensor at x=3674809, y=2062166: closest beacon is at x=3719980, y=2000000
Sensor at x=3693706, y=2027384: closest beacon is at x=3719980, y=2000000
Sensor at x=3869683, y=2291983: closest beacon is at x=3866712, y=2438950
Sensor at x=2666499, y=2796436: closest beacon is at x=2650643, y=2489479
Sensor at x=492, y=2601991: closest beacon is at x=92656, y=2629486
Sensor at x=2710282, y=3892347: closest beacon is at x=2907507, y=3100765
Sensor at x=28974, y=3971342: closest beacon is at x=-224297, y=2997209
Sensor at x=3990214, y=2399722: closest beacon is at x=3866712, y=2438950
Sensor at x=3853352, y=1009020: closest beacon is at x=3719980, y=2000000
Sensor at x=1231833, y=3999338: closest beacon is at x=1313797, y=4674300
Sensor at x=2083669, y=875035: closest beacon is at x=1369276, y=-160751
Sensor at x=1317274, y=2146819: closest beacon is at x=2650643, y=2489479
Sensor at x=3712875, y=2018770: closest beacon is at x=3719980, y=2000000
Sensor at x=963055, y=23644: closest beacon is at x=1369276, y=-160751
Sensor at x=3671967, y=64054: closest beacon is at x=3089364, y=-501737
Sensor at x=3109065, y=2222392: closest beacon is at x=2650643, y=2489479
Sensor at x=3218890, y=1517419: closest beacon is at x=3719980, y=2000000
Sensor at x=3856777, y=3987650: closest beacon is at x=4166706, y=3171774
Sensor at x=1912696, y=3392788: closest beacon is at x=2907507, y=3100765
Sensor at x=3597620, y=3100104: closest beacon is at x=4166706, y=3171774

83
2022/src/days/day15.rs Normal file
View File

@ -0,0 +1,83 @@
use anyhow::Result;
use std::fs;
use std::collections::HashSet;
#[derive(Debug)]
struct Pair {
sensor: Coord,
beacon: Coord,
}
#[derive(Debug, Eq, Hash, PartialEq)]
struct Coord {
x: i32,
y: i32,
}
impl Pair {
fn distance(self: &Self) -> i32 {
(self.sensor.x - self.beacon.x).abs() + (self.sensor.y - self.beacon.y).abs()
}
}
pub fn run() -> Result<()> {
#[cfg(not(feature = "test_input"))]
let file_contents = fs::read_to_string("day15.txt")?;
#[cfg(feature = "test_input")]
let file_contents = fs::read_to_string("tests/day15.txt")?;
let mut pairs: Vec<Pair> = Vec::with_capacity(25);
for line in file_contents.lines() {
let mut parts = line.split(": ");
let spart = parts.next().unwrap().strip_prefix("Sensor at ").unwrap();
let bpart = parts.next().unwrap().strip_prefix("closest beacon is at ").unwrap();
let mut scoords = spart.split(", ");
let sx = scoords.next().unwrap().strip_prefix("x=").unwrap().parse::<i32>().unwrap();
let sy = scoords.next().unwrap().strip_prefix("y=").unwrap().parse::<i32>().unwrap();
let mut bcoords = bpart.split(", ");
let bx = bcoords.next().unwrap().strip_prefix("x=").unwrap().parse::<i32>().unwrap();
let by = bcoords.next().unwrap().strip_prefix("y=").unwrap().parse::<i32>().unwrap();
pairs.push(Pair {
sensor: Coord { x: sx, y: sy },
beacon: Coord { x: bx, y: by },
});
}
#[cfg(not(feature = "test_input"))]
spots_taken_on_row(&pairs, 2000000);
#[cfg(feature = "test_input")]
spots_taken_on_row(&pairs, 10);
// println!("pairs {:?}", pairs);
Ok(())
}
fn spots_taken_on_row(pairs: &Vec<Pair>, row: i32) {
let mut c: HashSet<i32> = HashSet::new();
for p in pairs.iter() {
let d = p.distance();
if row > p.sensor.y && row - p.sensor.y > d {
continue;
} else if row < p.sensor.y && p.sensor.y - row > d {
continue;
}
let remaining_distance = d - (row - p.sensor.y).abs();
let minx = p.sensor.x - remaining_distance;
let maxx = p.sensor.x + remaining_distance;
// println!("{:?} distance {} from {}..={}", p.sensor, d, minx, maxx);
for x in minx..=maxx {
c.insert(x);
}
}
for p in pairs.iter() {
if p.beacon.y == row {
c.remove(&p.beacon.x);
}
}
println!("spots taken on row {} = {}", row, c.len());
}

View File

@ -4,6 +4,7 @@ pub mod day11;
pub mod day12;
pub mod day13;
pub mod day14;
pub mod day15;
pub mod day2;
pub mod day3;
pub mod day4;

View File

@ -54,6 +54,9 @@ fn run_day(number: i32) -> Result<()> {
14 => {
days::day14::run()?;
}
15 => {
days::day15::run()?;
}
_ => return Err(anyhow!("Invalid day provided")),
}
Ok(())

14
2022/tests/day15.txt Normal file
View File

@ -0,0 +1,14 @@
Sensor at x=2, y=18: closest beacon is at x=-2, y=15
Sensor at x=9, y=16: closest beacon is at x=10, y=16
Sensor at x=13, y=2: closest beacon is at x=15, y=3
Sensor at x=12, y=14: closest beacon is at x=10, y=16
Sensor at x=10, y=20: closest beacon is at x=10, y=16
Sensor at x=14, y=17: closest beacon is at x=10, y=16
Sensor at x=8, y=7: closest beacon is at x=2, y=10
Sensor at x=2, y=0: closest beacon is at x=2, y=10
Sensor at x=0, y=11: closest beacon is at x=2, y=10
Sensor at x=20, y=14: closest beacon is at x=25, y=17
Sensor at x=17, y=20: closest beacon is at x=21, y=22
Sensor at x=16, y=7: closest beacon is at x=15, y=3
Sensor at x=14, y=3: closest beacon is at x=15, y=3
Sensor at x=20, y=1: closest beacon is at x=15, y=3