advent-of-code/2022/src/bin/day15.rs

86 lines
2.8 KiB
Rust
Raw Normal View History

2022-12-15 12:19:28 +01:00
#![feature(test, result_option_inspect)]
extern crate test;
2022-12-15 12:19:28 +01:00
use aoc2022::{boilerplate, common::*};
2022-12-15 12:19:28 +01:00
use itertools::Itertools;
const DAY: usize = 15;
2022-12-15 13:19:33 +01:00
type Parsed = Vec<((i64, i64), u64)>;
fn parse_input(raw: &str) -> Parsed {
2022-12-15 12:19:28 +01:00
raw.lines()
.map(|line| {
<[&str; 4]>::try_from(line[12..].replace(", y=", ",").replace(": closest beacon is at x=", ",").split(',').collect_vec())
.unwrap()
.map(|s| s.parse().unwrap())
})
.map(|[x1, y1, x2, y2]| ((x1, y1), (x2, y2)))
2022-12-15 13:19:33 +01:00
.map(|(t1, t2)| (t1, manhattan(t1, t2)))
2022-12-15 12:19:28 +01:00
.collect()
}
2022-12-15 12:19:28 +01:00
fn manhattan((x1, y1): (i64, i64), (x2, y2): (i64, i64)) -> u64 {
x1.abs_diff(x2) + y1.abs_diff(y2)
}
2022-12-15 13:19:33 +01:00
fn part1(parsed: &Parsed, y: i64) -> i64 {
let (&min_x, &max_x) = parsed.iter().map(|((x1, _), _)| x1).minmax().into_option().unwrap();
let mut x = min_x - *parsed.iter().map(|(_, d)| d).max().unwrap() as i64;
2022-12-15 12:19:28 +01:00
let mut c = 0;
2022-12-15 13:19:33 +01:00
while x <= max_x {
match parsed.iter().find(|(p, d)| manhattan(*p, (x, y)) <= *d) {
Some(&((px, py), d)) => {
let new_x = px + d as i64 - (py.abs_diff(y) as i64) + 1;
c += new_x - x;
x = new_x;
2022-12-15 12:19:28 +01:00
}
2022-12-15 13:19:33 +01:00
None => x += 1,
};
2022-12-15 12:19:28 +01:00
}
2022-12-15 13:19:33 +01:00
c - 1
}
2022-12-15 13:08:15 +01:00
fn part2(parsed: &Parsed, bounds: i64) -> i64 {
for x in 0..=bounds {
let mut y = 0;
while y <= bounds {
2022-12-15 13:19:33 +01:00
match parsed.iter().find(|(p, d)| manhattan(*p, (x, y)) <= *d) {
Some(&((px, py), d)) => y = py + d as i64 - (px.abs_diff(x) as i64) + 1,
2022-12-15 13:08:15 +01:00
None => return x * 4_000_000 + y,
};
}
}
2022-12-15 13:19:33 +01:00
unreachable!("Did not find an eligible spot")
}
boilerplate! {
2022-12-15 12:19:28 +01:00
TEST_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",
tests: {
2022-12-15 12:19:28 +01:00
part1: { TEST_INPUT, 10 => 26 },
2022-12-15 13:08:15 +01:00
part2: { TEST_INPUT, 20 => 56000011 },
},
2022-12-15 12:19:28 +01:00
unittests: {
manhattan: {
(8,7), (2,10) => 9,
(8,7), (-1,7) => 9_,
}
},
2022-12-15 13:08:15 +01:00
bench1(2_000_000) == 4827924,
bench2(4_000_000) == 12977110973564,
2022-12-15 12:19:28 +01:00
bench_parse: Vec::len => 25,
}