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

138 lines
4.3 KiB
Rust

use std::process::exit;
#[derive(Copy, Clone, PartialEq, Eq)]
struct Pos(i128, i128);
fn get_manhattan_distance(p1 :&Pos, p2 :&Pos) -> i128{
return (p1.0 - p2.0).abs() + (p1.1 - p2.1).abs()
}
fn position_is_empty(x :i128, y :i128, i :usize, sensors :&Vec<(Pos, Pos, i128)>) -> bool {
for i2 in 0..sensors.len() {
if i == i2 {
continue;
}
if get_manhattan_distance(&Pos(sensors[i].0.0 + x, sensors[i].0.1 + y), &sensors[i2].0) <= sensors[i2].2 {
return false;
}
}
return true;
}
pub fn run(inp :Vec<String>) {
let parsed_inp :Vec<(Pos, Pos, i128)> = 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::<i128>().unwrap()).collect::<Vec<i128>>();
let beacon = split1[1].replace(" y=", "").split(',').map(|s| s.parse::<i128>().unwrap()).collect::<Vec<i128>>();
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, i128)>>();
// 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 = i128::MAX;
for bp in &parsed_inp {
if bp.0.0 - bp.2 < min {
min = bp.0.0 - bp.2;
}
}
const REAL_C :(i128, i128) = (0, 4000000);
const TEST_C :(i128, i128) = (0, 20);
// go through each Sensor and then search its edge+1 ->
// Because there arent that many sensord this shoudnt take too long
// And the non-scanned-spot has to be just after one of those edges
for i in 0..parsed_inp.len() {
println!("{}/{}", i, parsed_inp.len());
let mut x_component :i128 = parsed_inp[i].2 + 1;
let mut y_component = 0;
let x = parsed_inp[i].0.0;
let y = parsed_inp[i].0.1;
// right to bottom
while x_component > 0 {
if position_is_empty(x_component, y_component, i, &parsed_inp) {
if (x+x_component >= 0 && x+x_component <= REAL_C.1 && y+y_component >= 0 && y+y_component <= REAL_C.1) {
println!("a2: ({}, {}), {}", x+x_component, y+y_component, (x+x_component) * 4000000 + y_component+y);
}
}
y_component += 1;
x_component -= 1;
}
// bottom to left
while y_component > 0 {
if position_is_empty(x_component, y_component, i, &parsed_inp) {
if (x+x_component >= 0 && x+x_component <= REAL_C.1 && y+y_component >= 0 && y+y_component <= REAL_C.1) {
println!("a2: ({}, {}), {}", x+x_component, y+y_component, (x+x_component) * 4000000 + y_component+y);
}
}
y_component -= 1;
x_component -= 1;
}
// left to top
while x_component < 0 {
if position_is_empty(x_component, y_component, i, &parsed_inp) {
if (x+x_component >= 0 && x+x_component <= REAL_C.1 && y+y_component >= 0 && y+y_component <= REAL_C.1) {
println!("a2: ({}, {}), {}", x+x_component, y+y_component, (x+x_component) * 4000000 + y_component+y);
}
}
y_component -= 1;
x_component += 1;
}
// top to right
while y_component < 0 {
if position_is_empty(x_component, y_component, i, &parsed_inp) {
if (x+x_component >= 0 && x+x_component <= REAL_C.1 && y+y_component >= 0 && y+y_component <= REAL_C.1) {
println!("a2: ({}, {}), {}", x+x_component, y+y_component, (x+x_component) * 4000000 + y_component+y);
}
}
y_component += 1;
x_component += 1;
}
}
}