From a6c11ad68d419cf2b472665ae972d2bb77b39470 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Max=20K=C3=A4nner?= Date: Thu, 15 Dec 2022 10:09:18 +0100 Subject: [PATCH] Day 15 --- day15/Cargo.lock | 7 ++++ day15/input.txt | 32 ++++++++++++++++ day15/src/main.rs | 96 ++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 day15/Cargo.lock create mode 100644 day15/input.txt diff --git a/day15/Cargo.lock b/day15/Cargo.lock new file mode 100644 index 0000000..2473428 --- /dev/null +++ b/day15/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day15" +version = "0.1.0" diff --git a/day15/input.txt b/day15/input.txt new file mode 100644 index 0000000..fe5ff9c --- /dev/null +++ b/day15/input.txt @@ -0,0 +1,32 @@ +Sensor at x=2832148, y=322979: closest beacon is at x=3015667, y=-141020 +Sensor at x=1449180, y=3883502: closest beacon is at x=2656952, y=4188971 +Sensor at x=2808169, y=1194666: closest beacon is at x=3015667, y=-141020 +Sensor at x=1863363, y=2435968: closest beacon is at x=2166084, y=2883057 +Sensor at x=3558230, y=2190936: closest beacon is at x=3244164, y=2592191 +Sensor at x=711491, y=2444705: closest beacon is at x=617239, y=2988377 +Sensor at x=2727148, y=2766272: closest beacon is at x=2166084, y=2883057 +Sensor at x=2857938, y=3988086: closest beacon is at x=2968511, y=4098658 +Sensor at x=1242410, y=2270153: closest beacon is at x=214592, y=2000000 +Sensor at x=3171784, y=2523127: closest beacon is at x=3244164, y=2592191 +Sensor at x=2293378, y=71434: closest beacon is at x=3015667, y=-141020 +Sensor at x=399711, y=73420: closest beacon is at x=1152251, y=-158441 +Sensor at x=3677529, y=415283: closest beacon is at x=3015667, y=-141020 +Sensor at x=207809, y=2348497: closest beacon is at x=214592, y=2000000 +Sensor at x=60607, y=3403420: closest beacon is at x=617239, y=2988377 +Sensor at x=3767729, y=3136725: closest beacon is at x=4171278, y=3348370 +Sensor at x=3899632, y=3998969: closest beacon is at x=4171278, y=3348370 +Sensor at x=394783, y=1541278: closest beacon is at x=214592, y=2000000 +Sensor at x=1193642, y=642631: closest beacon is at x=1152251, y=-158441 +Sensor at x=122867, y=2661904: closest beacon is at x=214592, y=2000000 +Sensor at x=551012, y=3787568: closest beacon is at x=617239, y=2988377 +Sensor at x=3175715, y=2975144: closest beacon is at x=3244164, y=2592191 +Sensor at x=402217, y=2812449: closest beacon is at x=617239, y=2988377 +Sensor at x=879648, y=1177329: closest beacon is at x=214592, y=2000000 +Sensor at x=1317218, y=2978309: closest beacon is at x=617239, y=2988377 +Sensor at x=3965126, y=1743931: closest beacon is at x=3244164, y=2592191 +Sensor at x=2304348, y=3140055: closest beacon is at x=2166084, y=2883057 +Sensor at x=3380135, y=3650709: closest beacon is at x=2968511, y=4098658 +Sensor at x=49224, y=1914296: closest beacon is at x=214592, y=2000000 +Sensor at x=3096228, y=2457233: closest beacon is at x=3244164, y=2592191 +Sensor at x=1415660, y=6715: closest beacon is at x=1152251, y=-158441 +Sensor at x=2616280, y=3548378: closest beacon is at x=2656952, y=4188971 diff --git a/day15/src/main.rs b/day15/src/main.rs index e7a11a9..39e0a36 100644 --- a/day15/src/main.rs +++ b/day15/src/main.rs @@ -1,3 +1,95 @@ -fn main() { - println!("Hello, world!"); +use std::{collections::HashSet, fs::read_to_string, io::Write, ops::RangeInclusive}; + +struct Sensor { + x: i64, + y: i64, + x_beacon: i64, + y_beacon: i64, + distance: u64, +} + +impl From<&str> for Sensor { + fn from(value: &str) -> Self { + let [x_sensor, y_sensor, x_beacon, y_beacon] = value + .chars() + .filter(|c| c.is_ascii_digit() || c.is_whitespace() || *c == '-') + .collect::() + .split_whitespace() + .map(|n| n.parse::().unwrap()) + .collect::>()[..] else {panic!()}; + Self { + x: x_sensor, + y: y_sensor, + x_beacon, + y_beacon, + distance: x_sensor.abs_diff(x_beacon) + y_sensor.abs_diff(y_beacon), + } + } +} + +fn main() { + let input = ( + "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" + .to_owned(), + 10, + 20, + ); + let (input, line, xy_max): (_, i64, i64) = read_to_string("input.txt") + .map(|input| (input, 2_000_000, 4_000_000)) + .unwrap_or(input); + let sensors = input.lines().map(Sensor::from).collect::>(); + let mut covered: HashSet<_> = sensors + .iter() + .flat_map(|s| { + let diff = line.abs_diff(s.y); + if diff > s.distance { + return RangeInclusive::new(1, 0); + } + let d = s.distance - diff; + (s.x - d as i64)..=(s.x + d as i64) + }) + .collect(); + for sensor in &sensors { + if sensor.y_beacon == line { + covered.remove(&sensor.x_beacon); + } + } + println!("{} positions can't contain a beacon", covered.len()); + 'outer: for y in 0..=xy_max { + let mut ranges = vec![]; + for sensor in &sensors { + let diff = y.abs_diff(sensor.y); + if diff > sensor.distance { + continue; + } + let d = sensor.distance - diff; + ranges.push((sensor.x - d as i64)..=(sensor.x + d as i64)); + } + ranges.sort_unstable_by(|r1, r2| r1.start().cmp(r2.start())); + let mut max_x = 0; + for range in ranges { + if max_x + 1 < *range.start() { + let x = max_x + 1; + println!( + "found beacon at {x},{y} with frequency {}", + x * 4_000_000 + y + ); + break 'outer; + } + max_x = max_x.max(*range.end()); + } + } }