AoC2022/d15/src/a1.rs
2022-12-15 15:46:55 +01:00

75 lines
2.0 KiB
Rust

#[derive(Copy, Clone, PartialEq, Eq)]
struct Pos(i32, i32);
fn get_manhattan_distance(p1 :&Pos, p2 :&Pos) -> i32{
return (p1.0 - p2.0).abs() + (p1.1 - p2.1).abs()
}
pub fn run(inp :Vec<String>) {
let parsed_inp :Vec<(Pos, Pos, i32)> = inp.iter().map(|str| {
let split1 = str.split(": closest beacon is at x=").collect::<Vec<&str>>();
let sensor = split1[0].replace("Sensor at x=", "").replace(" y=", "").split(',').map(|s| s.parse::<i32>().unwrap()).collect::<Vec<i32>>();
let beacon = split1[1].replace(" y=", "").split(',').map(|s| s.parse::<i32>().unwrap()).collect::<Vec<i32>>();
println!("{:?}", sensor);
println!("{:?}", beacon);
let tmp = (Pos(sensor[0], sensor[1]), Pos(beacon[0], beacon[1]));
(tmp.0, tmp.1, get_manhattan_distance(&tmp.0, &tmp.1))
}).collect::<Vec<(Pos, Pos, i32)>>();
// check for how many pixels in row Y, the manhatten distance is STRUCTLY GREATER than the distance from any sensor to its beacon
// go from 0 to the furthest possible right pixel (x_pos + manhattan_distance are max)
// find max x
let mut max = 0;
for bp in &parsed_inp {
if bp.0.0 + bp.2 > max {
max = bp.0.0 + bp.2;
}
}
// find min x
let mut min = i32::MAX;
for bp in &parsed_inp {
if bp.0.0 - bp.2 < min {
min = bp.0.0 - bp.2;
}
}
const TEST_Y :i32 = 10;
const REAL_Y :i32 = 2000000;
let mut counter = 0;
'outer: for i in min..=max {
// check if there is a beacon at the current position
for b in &parsed_inp {
if b.1.eq(&Pos(i, REAL_Y)) {
continue 'outer;
}
}
for bp in &parsed_inp {
if get_manhattan_distance(&Pos(i, REAL_Y), &bp.0) <= bp.2 {
counter += 1;
//println!("({}, {}); ({}, {})", i, TEST_Y, bp.0.0, bp.0.1);
break;
}
}
}
println!("a1: {}", counter);
}