[rs] Solve 2022_15 part 1
This commit is contained in:
parent
faec68e1fb
commit
a8291375ba
5 changed files with 75 additions and 0 deletions
19
inputs/2022/2022_15.input
Normal file
19
inputs/2022/2022_15.input
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
Sensor at x=3482210, y=422224: closest beacon is at x=2273934, y=-202439
|
||||||
|
Sensor at x=3679395, y=2737332: closest beacon is at x=4104213, y=2980736
|
||||||
|
Sensor at x=3173475, y=3948494: closest beacon is at x=3494250, y=3554521
|
||||||
|
Sensor at x=27235, y=3642190: closest beacon is at x=-190885, y=3635525
|
||||||
|
Sensor at x=3851721, y=1754784: closest beacon is at x=3145586, y=2167751
|
||||||
|
Sensor at x=327074, y=3250656: closest beacon is at x=-190885, y=3635525
|
||||||
|
Sensor at x=3499970, y=3186179: closest beacon is at x=3494250, y=3554521
|
||||||
|
Sensor at x=150736, y=2522778: closest beacon is at x=-85806, y=2000000
|
||||||
|
Sensor at x=3000768, y=3333983: closest beacon is at x=2564067, y=3163630
|
||||||
|
Sensor at x=1751302, y=1660540: closest beacon is at x=3145586, y=2167751
|
||||||
|
Sensor at x=2591068, y=2923079: closest beacon is at x=2564067, y=3163630
|
||||||
|
Sensor at x=48946, y=3999178: closest beacon is at x=-190885, y=3635525
|
||||||
|
Sensor at x=3695475, y=3863101: closest beacon is at x=3494250, y=3554521
|
||||||
|
Sensor at x=1504031, y=2760: closest beacon is at x=2273934, y=-202439
|
||||||
|
Sensor at x=3021186, y=2667125: closest beacon is at x=3145586, y=2167751
|
||||||
|
Sensor at x=1514629, y=3771171: closest beacon is at x=2564067, y=3163630
|
||||||
|
Sensor at x=234064, y=616106: closest beacon is at x=-85806, y=2000000
|
||||||
|
Sensor at x=3990843, y=3393575: closest beacon is at x=4104213, y=2980736
|
||||||
|
Sensor at x=768875, y=2665271: closest beacon is at x=-85806, y=2000000
|
||||||
2
inputs/2022/2022_15.solution
Normal file
2
inputs/2022/2022_15.solution
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
Part 1: 4811413
|
||||||
|
Part 2: ???
|
||||||
|
|
@ -65,6 +65,7 @@ days! {
|
||||||
Y2022D12: "2022_12" => y2022::d12::solve,
|
Y2022D12: "2022_12" => y2022::d12::solve,
|
||||||
Y2022D13: "2022_13" => y2022::d13::solve,
|
Y2022D13: "2022_13" => y2022::d13::solve,
|
||||||
Y2022D14: "2022_14" => y2022::d14::solve,
|
Y2022D14: "2022_14" => y2022::d14::solve,
|
||||||
|
Y2022D15: "2022_15" => y2022::d15::solve,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
|
|
|
||||||
|
|
@ -12,3 +12,4 @@ pub mod d11;
|
||||||
pub mod d12;
|
pub mod d12;
|
||||||
pub mod d13;
|
pub mod d13;
|
||||||
pub mod d14;
|
pub mod d14;
|
||||||
|
pub mod d15;
|
||||||
|
|
|
||||||
52
rs/src/y2022/d15.rs
Normal file
52
rs/src/y2022/d15.rs
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
use std::collections::HashSet;
|
||||||
|
|
||||||
|
struct Sensor {
|
||||||
|
pos: (i32, i32),
|
||||||
|
beac: (i32, i32),
|
||||||
|
dist: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_coord(coord: &str) -> i32 {
|
||||||
|
let coord = coord.replace(|c| "xy=,:".contains(c), "");
|
||||||
|
coord.parse().unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn solve(input: String) {
|
||||||
|
let sensors = input
|
||||||
|
.lines()
|
||||||
|
.map(|l| {
|
||||||
|
let parts = l.split_whitespace().collect::<Vec<_>>();
|
||||||
|
let pos = (parse_coord(parts[2]), parse_coord(parts[3]));
|
||||||
|
let beac = (parse_coord(parts[8]), parse_coord(parts[9]));
|
||||||
|
let dist = (pos.0 - beac.0).abs() + (pos.1 - beac.1).abs();
|
||||||
|
Sensor { pos, beac, dist }
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
// A beacon exactly dist away has 1 field, i. e. x±0
|
||||||
|
// A beacon exactly dist-1 away has 3 fields, i. e. x±1
|
||||||
|
// ...
|
||||||
|
// A beacon exactly 0 away has 2dist+1 fields, i. e. x±dist
|
||||||
|
//
|
||||||
|
// Formula: Go from x-(dist-dy) to x+(dist-dy)
|
||||||
|
let line_at_y = 2000000;
|
||||||
|
let mut part1 = HashSet::new();
|
||||||
|
for (i, sensor) in sensors.iter().enumerate() {
|
||||||
|
eprintln!("Sensor {}", i + 1);
|
||||||
|
let dy = (line_at_y - sensor.pos.1).abs();
|
||||||
|
if dy > sensor.dist {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let dx = sensor.dist - dy;
|
||||||
|
for x in (sensor.pos.0 - dx)..=(sensor.pos.0 + dx) {
|
||||||
|
part1.insert(x);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for sensor in &sensors {
|
||||||
|
if sensor.beac.1 == line_at_y {
|
||||||
|
part1.remove(&sensor.beac.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
println!("Part 1: {}", part1.len());
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue