Day 15 - task 1

This commit is contained in:
Jonathan Flueren 2022-12-15 18:38:48 +01:00
parent 0f30709aa8
commit cb8d6e64ce
3 changed files with 145 additions and 2 deletions

36
res/15/input.txt Normal file
View file

@ -0,0 +1,36 @@
Sensor at x=489739, y=1144461: closest beacon is at x=-46516, y=554951
Sensor at x=2543342, y=3938: closest beacon is at x=2646619, y=229757
Sensor at x=3182359, y=3999986: closest beacon is at x=3142235, y=3956791
Sensor at x=3828004, y=1282262: closest beacon is at x=3199543, y=2310713
Sensor at x=871967, y=3962966: closest beacon is at x=-323662, y=4519876
Sensor at x=1323641, y=2986163: closest beacon is at x=2428372, y=3303736
Sensor at x=2911492, y=2576579: closest beacon is at x=3022758, y=2461675
Sensor at x=3030965, y=2469848: closest beacon is at x=3022758, y=2461675
Sensor at x=3299037, y=3402462: closest beacon is at x=3142235, y=3956791
Sensor at x=1975203, y=1672969: closest beacon is at x=1785046, y=2000000
Sensor at x=3048950, y=2452864: closest beacon is at x=3022758, y=2461675
Sensor at x=336773, y=2518242: closest beacon is at x=1785046, y=2000000
Sensor at x=1513936, y=574443: closest beacon is at x=2646619, y=229757
Sensor at x=3222440, y=2801189: closest beacon is at x=3199543, y=2310713
Sensor at x=2838327, y=2122421: closest beacon is at x=2630338, y=2304286
Sensor at x=2291940, y=2502068: closest beacon is at x=2630338, y=2304286
Sensor at x=2743173, y=3608337: closest beacon is at x=2428372, y=3303736
Sensor at x=3031202, y=2452943: closest beacon is at x=3022758, y=2461675
Sensor at x=3120226, y=3998439: closest beacon is at x=3142235, y=3956791
Sensor at x=2234247, y=3996367: closest beacon is at x=2428372, y=3303736
Sensor at x=593197, y=548: closest beacon is at x=-46516, y=554951
Sensor at x=2612034, y=2832157: closest beacon is at x=2630338, y=2304286
Sensor at x=3088807, y=3929947: closest beacon is at x=3142235, y=3956791
Sensor at x=2022834, y=2212455: closest beacon is at x=1785046, y=2000000
Sensor at x=3129783, y=3975610: closest beacon is at x=3142235, y=3956791
Sensor at x=3150025, y=2333166: closest beacon is at x=3199543, y=2310713
Sensor at x=3118715, y=2376161: closest beacon is at x=3199543, y=2310713
Sensor at x=3951193, y=3181929: closest beacon is at x=4344952, y=3106256
Sensor at x=2807831, y=2401551: closest beacon is at x=2630338, y=2304286
Sensor at x=3683864, y=2906786: closest beacon is at x=4344952, y=3106256
Sensor at x=2723234, y=3206978: closest beacon is at x=2428372, y=3303736
Sensor at x=3047123, y=3891244: closest beacon is at x=3142235, y=3956791
Sensor at x=3621967, y=3793314: closest beacon is at x=3142235, y=3956791
Sensor at x=2384506, y=1814055: closest beacon is at x=2630338, y=2304286
Sensor at x=83227, y=330275: closest beacon is at x=-46516, y=554951
Sensor at x=3343176, y=75114: closest beacon is at x=2646619, y=229757

14
res/15/test-input.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

View file

@ -1,9 +1,102 @@
use std::fs;
use std::{collections::HashSet, fs};
const ROW: i32 = 2000000;
pub fn solve() {
let path = "res/15/input.txt";
let contents = fs::read_to_string(path).expect("File read error");
let sensors: Vec<((i32, i32), (i32, i32), i32)> = contents
.lines()
.filter(|line| *line != "")
.map(|line| {
let line = line
.replace("x", "")
.replace("y", "")
.replace("=", "")
.replace(":", "")
.replace(",", "");
let words: Vec<&str> = line.split_whitespace().collect();
let x1 = words[2].parse::<i32>().unwrap();
let y1 = words[3].parse::<i32>().unwrap();
let x2 = words[8].parse::<i32>().unwrap();
let y2 = words[9].parse::<i32>().unwrap();
let rad = x1.abs_diff(x2) as i32 + y1.abs_diff(y2) as i32;
((x1, y1), (x2, y2), rad)
})
.collect();
let mut row_map = HashSet::<i32>::new();
sensors.iter().for_each(|sensor| {
let ((x1, y1), (x2, y2), rad) = sensor;
let y_diff = y1.abs_diff(ROW) as i32;
let x_diff = rad - y_diff;
if x_diff < 0 {
return;
}
(x1 - x_diff..=x1 + x_diff).for_each(|i| {
row_map.insert(i);
})
});
sensors.iter().for_each(|sensor| {
let ((_, _), (x2, y2), _) = sensor;
if *y2 == ROW && row_map.contains(&x2) {
row_map.remove(&x2);
}
});
let res = row_map.len();
println!("Result 1: {res}");
let mut sen_tup = Vec::<((i32, i32, i32), (i32, i32, i32))>::new();
let mut up_outlines = Vec::<i32>::new();
let mut down_outlines = Vec::<i32>::new();
sensors.iter().for_each(|s1| {
sensors.iter().for_each(|s2| {
if s1 != s2 {
let ((x1, y1), (_, _), rad1) = *s1;
let ((x2, y2), (_, _), rad2) = *s2;
let dist = x1.abs_diff(x2) as i32 + y1.abs_diff(y2) as i32;
if dist == rad1 + rad2 + 1 {
sen_tup.push(((x1, y1, rad1), (x2, y2, rad2)));
// s2 in quadrant 1
if x1 - x2 < 0 && y1 - y2 > 0 {
down_outlines.push(y1 - (x1 + rad1) - 1)
}
// s2 in quadrant 3
else if x1 - x2 > 0 && y1 - y2 < 0 {
down_outlines.push(y2 - (x2 + rad2) - 1)
}
// s2 in quadrant 2
else if x1 - x2 > 0 && y1 - y2 > 0 {
up_outlines.push(y2 + (x2 + rad2) + 1)
}
// s2 in quadrant 4
else if x1 - x2 < 0 && y1 - y2 < 0 {
up_outlines.push(y1 + (x1 + rad1) + 1)
}
}
}
})
});
let mut intersections = Vec::<(i32, i32)>::new();
up_outlines.iter().for_each(|up_line| {
down_outlines.iter().for_each(|down_line| {
if up_line > down_line && up_line % 2 == down_line % 2 {
let intersection = down_line + (up_line - down_line) / 2;
// TODO continue
}
})
});
sen_tup.iter().for_each(|tup| {});
}