Solutions for 2022, as well as 2015-2018 and 2019 up to day 11
This commit is contained in:
commit
1895197c49
722 changed files with 375457 additions and 0 deletions
8
2022/day15-beacon_exclusive_zone/Cargo.toml
Normal file
8
2022/day15-beacon_exclusive_zone/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "day15-beacon_exclusive_zone"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
111
2022/day15-beacon_exclusive_zone/challenge.md
Normal file
111
2022/day15-beacon_exclusive_zone/challenge.md
Normal file
|
@ -0,0 +1,111 @@
|
|||
--- Day 15: Beacon Exclusion Zone ---
|
||||
|
||||
You feel the ground rumble again as the distress signal leads you to a large network of subterranean tunnels. You don't have time to search them all, but you don't need to: your pack contains a set of deployable sensors that you imagine were originally built to locate lost Elves.
|
||||
|
||||
The sensors aren't very powerful, but that's okay; your handheld device indicates that you're close enough to the source of the distress signal to use them. You pull the emergency sensor system out of your pack, hit the big button on top, and the sensors zoom off down the tunnels.
|
||||
|
||||
Once a sensor finds a spot it thinks will give it a good reading, it attaches itself to a hard surface and begins monitoring for the nearest signal source beacon. Sensors and beacons always exist at integer coordinates. Each sensor knows its own position and can determine the position of a beacon precisely; however, sensors can only lock on to the one beacon closest to the sensor as measured by the Manhattan distance. (There is never a tie where two beacons are the same distance to a sensor.)
|
||||
|
||||
It doesn't take long for the sensors to report back their positions and closest beacons (your puzzle input). For example:
|
||||
|
||||
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
|
||||
|
||||
So, consider the sensor at 2,18; the closest beacon to it is at -2,15. For the sensor at 9,16, the closest beacon to it is at 10,16.
|
||||
|
||||
Drawing sensors as S and beacons as B, the above arrangement of sensors and beacons looks like this:
|
||||
|
||||
1 1 2 2
|
||||
0 5 0 5 0 5
|
||||
0 ....S.......................
|
||||
1 ......................S.....
|
||||
2 ...............S............
|
||||
3 ................SB..........
|
||||
4 ............................
|
||||
5 ............................
|
||||
6 ............................
|
||||
7 ..........S.......S.........
|
||||
8 ............................
|
||||
9 ............................
|
||||
10 ....B.......................
|
||||
11 ..S.........................
|
||||
12 ............................
|
||||
13 ............................
|
||||
14 ..............S.......S.....
|
||||
15 B...........................
|
||||
16 ...........SB...............
|
||||
17 ................S..........B
|
||||
18 ....S.......................
|
||||
19 ............................
|
||||
20 ............S......S........
|
||||
21 ............................
|
||||
22 .......................B....
|
||||
|
||||
This isn't necessarily a comprehensive map of all beacons in the area, though. Because each sensor only identifies its closest beacon, if a sensor detects a beacon, you know there are no other beacons that close or closer to that sensor. There could still be beacons that just happen to not be the closest beacon to any sensor. Consider the sensor at 8,7:
|
||||
|
||||
1 1 2 2
|
||||
0 5 0 5 0 5
|
||||
-2 ..........#.................
|
||||
-1 .........###................
|
||||
0 ....S...#####...............
|
||||
1 .......#######........S.....
|
||||
2 ......#########S............
|
||||
3 .....###########SB..........
|
||||
4 ....#############...........
|
||||
5 ...###############..........
|
||||
6 ..#################.........
|
||||
7 .#########S#######S#........
|
||||
8 ..#################.........
|
||||
9 ...###############..........
|
||||
10 ....B############...........
|
||||
11 ..S..###########............
|
||||
12 ......#########.............
|
||||
13 .......#######..............
|
||||
14 ........#####.S.......S.....
|
||||
15 B........###................
|
||||
16 ..........#SB...............
|
||||
17 ................S..........B
|
||||
18 ....S.......................
|
||||
19 ............................
|
||||
20 ............S......S........
|
||||
21 ............................
|
||||
22 .......................B....
|
||||
|
||||
This sensor's closest beacon is at 2,10, and so you know there are no beacons that close or closer (in any positions marked #).
|
||||
|
||||
None of the detected beacons seem to be producing the distress signal, so you'll need to work out where the distress beacon is by working out where it isn't. For now, keep things simple by counting the positions where a beacon cannot possibly be along just a single row.
|
||||
|
||||
So, suppose you have an arrangement of beacons and sensors like in the example above and, just in the row where y=10, you'd like to count the number of positions a beacon cannot possibly exist. The coverage from all sensors near that row looks like this:
|
||||
|
||||
1 1 2 2
|
||||
0 5 0 5 0 5
|
||||
9 ...#########################...
|
||||
10 ..####B######################..
|
||||
11 .###S#############.###########.
|
||||
|
||||
In this example, in the row where y=10, there are 26 positions where a beacon cannot be present.
|
||||
|
||||
Consult the report from the sensors you just deployed. In the row where y=2000000, how many positions cannot contain a beacon?
|
||||
|
||||
|
||||
--- Part Two ---
|
||||
|
||||
Your handheld device indicates that the distress signal is coming from a beacon nearby. The distress beacon is not detected by any sensor, but the distress beacon must have x and y coordinates each no lower than 0 and no larger than 4000000.
|
||||
|
||||
To isolate the distress beacon's signal, you need to determine its tuning frequency, which can be found by multiplying its x coordinate by 4000000 and then adding its y coordinate.
|
||||
|
||||
In the example above, the search space is smaller: instead, the x and y coordinates can each be at most 20. With this reduced search area, there is only a single position that could have a beacon: x=14, y=11. The tuning frequency for this distress beacon is 56000011.
|
||||
|
||||
Find the only possible position for the distress beacon. What is its tuning frequency?
|
35
2022/day15-beacon_exclusive_zone/input
Normal file
35
2022/day15-beacon_exclusive_zone/input
Normal file
|
@ -0,0 +1,35 @@
|
|||
Sensor at x=545406, y=2945484: closest beacon is at x=772918, y=2626448
|
||||
Sensor at x=80179, y=3385522: closest beacon is at x=772918, y=2626448
|
||||
Sensor at x=2381966, y=3154542: closest beacon is at x=2475123, y=3089709
|
||||
Sensor at x=2607868, y=1728571: closest beacon is at x=2715626, y=2000000
|
||||
Sensor at x=746476, y=2796469: closest beacon is at x=772918, y=2626448
|
||||
Sensor at x=911114, y=2487289: closest beacon is at x=772918, y=2626448
|
||||
Sensor at x=2806673, y=3051666: closest beacon is at x=2475123, y=3089709
|
||||
Sensor at x=1335361, y=3887240: closest beacon is at x=2505629, y=4282497
|
||||
Sensor at x=2432913, y=3069935: closest beacon is at x=2475123, y=3089709
|
||||
Sensor at x=1333433, y=35725: closest beacon is at x=1929144, y=529341
|
||||
Sensor at x=2289207, y=1556729: closest beacon is at x=2715626, y=2000000
|
||||
Sensor at x=2455525, y=3113066: closest beacon is at x=2475123, y=3089709
|
||||
Sensor at x=3546858, y=3085529: closest beacon is at x=3629407, y=2984857
|
||||
Sensor at x=3542939, y=2742086: closest beacon is at x=3629407, y=2984857
|
||||
Sensor at x=2010918, y=2389107: closest beacon is at x=2715626, y=2000000
|
||||
Sensor at x=3734968, y=3024964: closest beacon is at x=3629407, y=2984857
|
||||
Sensor at x=2219206, y=337159: closest beacon is at x=1929144, y=529341
|
||||
Sensor at x=1969253, y=890542: closest beacon is at x=1929144, y=529341
|
||||
Sensor at x=3522991, y=3257032: closest beacon is at x=3629407, y=2984857
|
||||
Sensor at x=2303155, y=3239124: closest beacon is at x=2475123, y=3089709
|
||||
Sensor at x=2574308, y=111701: closest beacon is at x=1929144, y=529341
|
||||
Sensor at x=14826, y=2490395: closest beacon is at x=772918, y=2626448
|
||||
Sensor at x=3050752, y=2366125: closest beacon is at x=2715626, y=2000000
|
||||
Sensor at x=3171811, y=2935106: closest beacon is at x=3629407, y=2984857
|
||||
Sensor at x=3909938, y=1033557: closest beacon is at x=3493189, y=-546524
|
||||
Sensor at x=1955751, y=452168: closest beacon is at x=1929144, y=529341
|
||||
Sensor at x=2159272, y=614653: closest beacon is at x=1929144, y=529341
|
||||
Sensor at x=3700981, y=2930103: closest beacon is at x=3629407, y=2984857
|
||||
Sensor at x=3236266, y=3676457: closest beacon is at x=3373823, y=4223689
|
||||
Sensor at x=3980003, y=3819278: closest beacon is at x=3373823, y=4223689
|
||||
Sensor at x=1914391, y=723058: closest beacon is at x=1929144, y=529341
|
||||
Sensor at x=474503, y=1200604: closest beacon is at x=-802154, y=776650
|
||||
Sensor at x=2650714, y=3674470: closest beacon is at x=2505629, y=4282497
|
||||
Sensor at x=1696740, y=586715: closest beacon is at x=1929144, y=529341
|
||||
Sensor at x=3818789, y=2961752: closest beacon is at x=3629407, y=2984857
|
183
2022/day15-beacon_exclusive_zone/src/main.rs
Normal file
183
2022/day15-beacon_exclusive_zone/src/main.rs
Normal file
|
@ -0,0 +1,183 @@
|
|||
use std::{fs, collections::{HashSet, HashMap}};
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
|
||||
struct Position {
|
||||
x: isize,
|
||||
y: isize,
|
||||
}
|
||||
|
||||
impl Position {
|
||||
fn distance_to(&self, other: &Position) -> isize {
|
||||
(self.x - other.x).abs() + (self.y - other.y).abs()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
struct Sensor {
|
||||
position: Position,
|
||||
beacon_distance: isize,
|
||||
}
|
||||
|
||||
impl Sensor {
|
||||
fn from(reading: &str) -> Self {
|
||||
let components = reading.split(' ').collect::<Vec<&str>>();
|
||||
if components.len() != 10 {
|
||||
panic!("{components:?} does not have 10 items.");
|
||||
}
|
||||
|
||||
let sensor_x_str = &components[2][2..];
|
||||
let sensor_y_str = &components[3][2..];
|
||||
let beacon_x_str = &components[8][2..];
|
||||
let beacon_y_str = &components[9][2..];
|
||||
|
||||
let sensor_x = sensor_x_str[0..sensor_x_str.len()-1].parse::<isize>().unwrap();
|
||||
let sensor_y = sensor_y_str[0..sensor_y_str.len()-1].parse::<isize>().unwrap();
|
||||
let beacon_x = beacon_x_str[0..beacon_x_str.len()-1].parse::<isize>().unwrap();
|
||||
let beacon_y = beacon_y_str[0..].parse::<isize>().unwrap();
|
||||
|
||||
let position = Position {
|
||||
x: sensor_x,
|
||||
y: sensor_y,
|
||||
};
|
||||
let beacon_position = Position {
|
||||
x: beacon_x,
|
||||
y: beacon_y,
|
||||
};
|
||||
|
||||
Self {
|
||||
position: position.clone(),
|
||||
beacon_distance: position.distance_to(&beacon_position),
|
||||
}
|
||||
}
|
||||
|
||||
fn beacon_from(reading: &str) -> Position {
|
||||
let components = reading.split(' ').collect::<Vec<&str>>();
|
||||
if components.len() != 10 {
|
||||
panic!("{components:?} does not have 10 items.");
|
||||
}
|
||||
|
||||
let beacon_x_str = &components[8][2..];
|
||||
let beacon_y_str = &components[9][2..];
|
||||
|
||||
let beacon_x = beacon_x_str[0..beacon_x_str.len()-1].parse::<isize>().unwrap();
|
||||
let beacon_y = beacon_y_str[0..].parse::<isize>().unwrap();
|
||||
|
||||
Position {
|
||||
x: beacon_x,
|
||||
y: beacon_y,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn at_row(&self, row: isize) -> HashSet<isize> {
|
||||
let slice_depth = self.beacon_distance - (row-self.position.y).abs();
|
||||
match slice_depth {
|
||||
nope if nope <= 0 => HashSet::new(),
|
||||
_ => (self.position.x-slice_depth..=self.position.x+slice_depth).collect(),
|
||||
}
|
||||
}
|
||||
|
||||
fn first_non_reachables(&self, unreachables: &mut HashMap<Position, bool>, min: isize, max: isize) {
|
||||
// top right
|
||||
for i in 0..=self.beacon_distance {
|
||||
let x = self.position.x+i;
|
||||
let y = self.position.y-(self.beacon_distance+1)+i;
|
||||
if (min..=max).contains(&x) && (min..=max).contains(&y) {
|
||||
unreachables.entry(Position{ x, y }).and_modify(|repeat| *repeat = true).or_insert(false);
|
||||
}
|
||||
}
|
||||
|
||||
// bottom right
|
||||
for i in 0..=self.beacon_distance {
|
||||
let x = self.position.x+self.beacon_distance+1-i;
|
||||
let y = self.position.y+i;
|
||||
if (min..=max).contains(&x) && (min..=max).contains(&y) {
|
||||
unreachables.entry(Position{ x, y }).and_modify(|repeat| *repeat = true).or_insert(false);
|
||||
}
|
||||
}
|
||||
|
||||
// bottom left
|
||||
for i in 0..=self.beacon_distance {
|
||||
let x = self.position.x-i;
|
||||
let y = self.position.y+self.beacon_distance+1+i;
|
||||
if (min..=max).contains(&x) && (min..=max).contains(&y) {
|
||||
unreachables.entry(Position{ x, y }).and_modify(|repeat| *repeat = true).or_insert(false);
|
||||
}
|
||||
}
|
||||
|
||||
// top left
|
||||
for i in 0..=self.beacon_distance {
|
||||
let x = self.position.x-(self.beacon_distance+1)+i;
|
||||
let y = self.position.y-i;
|
||||
if (min..=max).contains(&x) && (min..=max).contains(&y) {
|
||||
unreachables.entry(Position{ x, y }).and_modify(|repeat| *repeat = true).or_insert(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn read_file(path: &str) -> String {
|
||||
fs::read_to_string(path)
|
||||
.expect("File not Found")
|
||||
}
|
||||
|
||||
fn beacon_free_positions(row: isize, sensors: &[Sensor], beacons: &HashSet<Position>) -> usize {
|
||||
sensors.iter()
|
||||
.map(|s| s.at_row(row))
|
||||
.reduce(|a, b| a.union(&b).cloned().collect())
|
||||
.unwrap()
|
||||
.len() - beacons.iter().filter(|b| b.y == row).count()
|
||||
}
|
||||
|
||||
fn is_reachable_by(position: &Position, sensors: &[Sensor]) -> bool {
|
||||
for sensor in sensors {
|
||||
if position.distance_to(&sensor.position) <= sensor.beacon_distance {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
fn get_non_reachable(sensors: &[Sensor], beacons: &HashSet<Position>, max: isize) -> isize {
|
||||
let mut first_non_reachables = HashMap::new();
|
||||
sensors.iter()
|
||||
.for_each(|s| s.first_non_reachables(&mut first_non_reachables, 0, max));
|
||||
|
||||
if let Some((&pos, _)) = &first_non_reachables.iter()
|
||||
.find(|(pos, &repeat)| repeat && !beacons.contains(pos) && !is_reachable_by(pos, sensors)) {
|
||||
pos.x * 4_000_000 + pos.y
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let readings = read_file("input");
|
||||
|
||||
let sensors = readings.lines().map(Sensor::from).collect::<Vec<_>>();
|
||||
let beacons = readings.lines().map(Sensor::beacon_from).collect::<HashSet<_>>();
|
||||
|
||||
println!("Not in Line 2_000_000: {}", beacon_free_positions(2_000_000, &sensors, &beacons));
|
||||
println!("Non-Reachable Position found with frequency {}.", get_non_reachable(&sensors, &beacons, 4_000_000));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sample_input() {
|
||||
let readings = read_file("tests/sample_input");
|
||||
let sensors = readings.lines().map(Sensor::from).collect::<Vec<_>>();
|
||||
let beacons = readings.lines().map(Sensor::beacon_from).collect::<HashSet<_>>();
|
||||
|
||||
assert_eq!(beacon_free_positions(10, &sensors, &beacons), 26);
|
||||
assert_eq!(get_non_reachable(&sensors, &beacons, 20), 56000011);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn challenge_input() {
|
||||
let readings = read_file("tests/input");
|
||||
let sensors = readings.lines().map(Sensor::from).collect::<Vec<_>>();
|
||||
let beacons = readings.lines().map(Sensor::beacon_from).collect::<HashSet<_>>();
|
||||
|
||||
assert_eq!(beacon_free_positions(2_000_000, &sensors, &beacons), 5367037);
|
||||
assert_eq!(get_non_reachable(&sensors, &beacons, 4_000_000), 11914583249288);
|
||||
}
|
35
2022/day15-beacon_exclusive_zone/tests/input
Normal file
35
2022/day15-beacon_exclusive_zone/tests/input
Normal file
|
@ -0,0 +1,35 @@
|
|||
Sensor at x=545406, y=2945484: closest beacon is at x=772918, y=2626448
|
||||
Sensor at x=80179, y=3385522: closest beacon is at x=772918, y=2626448
|
||||
Sensor at x=2381966, y=3154542: closest beacon is at x=2475123, y=3089709
|
||||
Sensor at x=2607868, y=1728571: closest beacon is at x=2715626, y=2000000
|
||||
Sensor at x=746476, y=2796469: closest beacon is at x=772918, y=2626448
|
||||
Sensor at x=911114, y=2487289: closest beacon is at x=772918, y=2626448
|
||||
Sensor at x=2806673, y=3051666: closest beacon is at x=2475123, y=3089709
|
||||
Sensor at x=1335361, y=3887240: closest beacon is at x=2505629, y=4282497
|
||||
Sensor at x=2432913, y=3069935: closest beacon is at x=2475123, y=3089709
|
||||
Sensor at x=1333433, y=35725: closest beacon is at x=1929144, y=529341
|
||||
Sensor at x=2289207, y=1556729: closest beacon is at x=2715626, y=2000000
|
||||
Sensor at x=2455525, y=3113066: closest beacon is at x=2475123, y=3089709
|
||||
Sensor at x=3546858, y=3085529: closest beacon is at x=3629407, y=2984857
|
||||
Sensor at x=3542939, y=2742086: closest beacon is at x=3629407, y=2984857
|
||||
Sensor at x=2010918, y=2389107: closest beacon is at x=2715626, y=2000000
|
||||
Sensor at x=3734968, y=3024964: closest beacon is at x=3629407, y=2984857
|
||||
Sensor at x=2219206, y=337159: closest beacon is at x=1929144, y=529341
|
||||
Sensor at x=1969253, y=890542: closest beacon is at x=1929144, y=529341
|
||||
Sensor at x=3522991, y=3257032: closest beacon is at x=3629407, y=2984857
|
||||
Sensor at x=2303155, y=3239124: closest beacon is at x=2475123, y=3089709
|
||||
Sensor at x=2574308, y=111701: closest beacon is at x=1929144, y=529341
|
||||
Sensor at x=14826, y=2490395: closest beacon is at x=772918, y=2626448
|
||||
Sensor at x=3050752, y=2366125: closest beacon is at x=2715626, y=2000000
|
||||
Sensor at x=3171811, y=2935106: closest beacon is at x=3629407, y=2984857
|
||||
Sensor at x=3909938, y=1033557: closest beacon is at x=3493189, y=-546524
|
||||
Sensor at x=1955751, y=452168: closest beacon is at x=1929144, y=529341
|
||||
Sensor at x=2159272, y=614653: closest beacon is at x=1929144, y=529341
|
||||
Sensor at x=3700981, y=2930103: closest beacon is at x=3629407, y=2984857
|
||||
Sensor at x=3236266, y=3676457: closest beacon is at x=3373823, y=4223689
|
||||
Sensor at x=3980003, y=3819278: closest beacon is at x=3373823, y=4223689
|
||||
Sensor at x=1914391, y=723058: closest beacon is at x=1929144, y=529341
|
||||
Sensor at x=474503, y=1200604: closest beacon is at x=-802154, y=776650
|
||||
Sensor at x=2650714, y=3674470: closest beacon is at x=2505629, y=4282497
|
||||
Sensor at x=1696740, y=586715: closest beacon is at x=1929144, y=529341
|
||||
Sensor at x=3818789, y=2961752: closest beacon is at x=3629407, y=2984857
|
14
2022/day15-beacon_exclusive_zone/tests/sample_input
Normal file
14
2022/day15-beacon_exclusive_zone/tests/sample_input
Normal 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
|
Loading…
Add table
Add a link
Reference in a new issue