day 15
This commit is contained in:
@ -1,4 +1,75 @@
|
||||
|
||||
#[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);
|
||||
|
||||
}
|
133
d15/src/a2.rs
133
d15/src/a2.rs
@ -1,5 +1,138 @@
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -34,7 +34,7 @@ fn main() {
|
||||
|
||||
let inp :Vec<String> = read_file("input.txt");
|
||||
|
||||
a1::run(inp.clone());
|
||||
// a1::run(inp.clone());
|
||||
|
||||
a2::run(inp);
|
||||
|
||||
|
Reference in New Issue
Block a user