day 15
This commit is contained in:
34
inputs/day15.txt
Normal file
34
inputs/day15.txt
Normal file
@@ -0,0 +1,34 @@
|
||||
Sensor at x=1363026, y=2928920: closest beacon is at x=1571469, y=3023534
|
||||
Sensor at x=2744178, y=3005943: closest beacon is at x=3091714, y=3106683
|
||||
Sensor at x=223983, y=2437431: closest beacon is at x=-278961, y=3326224
|
||||
Sensor at x=2454616, y=2576344: closest beacon is at x=2885998, y=2387754
|
||||
Sensor at x=1551436, y=29248: closest beacon is at x=1865296, y=-1279130
|
||||
Sensor at x=2997120, y=2493979: closest beacon is at x=2885998, y=2387754
|
||||
Sensor at x=1588355, y=3153332: closest beacon is at x=1571469, y=3023534
|
||||
Sensor at x=3539081, y=3302128: closest beacon is at x=3309042, y=3583067
|
||||
Sensor at x=3973905, y=60392: closest beacon is at x=3515381, y=-806927
|
||||
Sensor at x=3305001, y=3120691: closest beacon is at x=3091714, y=3106683
|
||||
Sensor at x=3859262, y=2668840: closest beacon is at x=3574747, y=2000000
|
||||
Sensor at x=2475557, y=3997856: closest beacon is at x=2364210, y=4052453
|
||||
Sensor at x=2775306, y=3668540: closest beacon is at x=3309042, y=3583067
|
||||
Sensor at x=3018235, y=2285225: closest beacon is at x=2885998, y=2387754
|
||||
Sensor at x=3033163, y=3294719: closest beacon is at x=3091714, y=3106683
|
||||
Sensor at x=3079956, y=3215569: closest beacon is at x=3091714, y=3106683
|
||||
Sensor at x=3994355, y=1831842: closest beacon is at x=3574747, y=2000000
|
||||
Sensor at x=1741021, y=3231978: closest beacon is at x=1571469, y=3023534
|
||||
Sensor at x=1873455, y=3917294: closest beacon is at x=2364210, y=4052453
|
||||
Sensor at x=3128140, y=2938277: closest beacon is at x=3091714, y=3106683
|
||||
Sensor at x=732217, y=3603298: closest beacon is at x=-278961, y=3326224
|
||||
Sensor at x=3884431, y=3834735: closest beacon is at x=3309042, y=3583067
|
||||
Sensor at x=3679358, y=1029949: closest beacon is at x=3574747, y=2000000
|
||||
Sensor at x=2260133, y=3563353: closest beacon is at x=2364210, y=4052453
|
||||
Sensor at x=60149, y=3320681: closest beacon is at x=-278961, y=3326224
|
||||
Sensor at x=3132535, y=2405693: closest beacon is at x=2885998, y=2387754
|
||||
Sensor at x=3028313, y=2829410: closest beacon is at x=3091714, y=3106683
|
||||
Sensor at x=3142423, y=3921417: closest beacon is at x=3309042, y=3583067
|
||||
Sensor at x=2636416, y=939525: closest beacon is at x=2885998, y=2387754
|
||||
Sensor at x=524530, y=681397: closest beacon is at x=-1031499, y=681463
|
||||
Sensor at x=3155000, y=1666362: closest beacon is at x=3574747, y=2000000
|
||||
Sensor at x=2169350, y=3040469: closest beacon is at x=1571469, y=3023534
|
||||
Sensor at x=1663350, y=1595182: closest beacon is at x=1571469, y=3023534
|
||||
Sensor at x=3311582, y=3386773: closest beacon is at x=3309042, y=3583067
|
||||
8
src/bin/day15_1.rs
Normal file
8
src/bin/day15_1.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
use std::fs;
|
||||
|
||||
use aoc2022::day15::process_part_1;
|
||||
|
||||
fn main() {
|
||||
let file = fs::read_to_string("./inputs/day15.txt").unwrap();
|
||||
println!("{}", process_part_1(&file, 2_000_000));
|
||||
}
|
||||
8
src/bin/day15_2.rs
Normal file
8
src/bin/day15_2.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
use std::fs;
|
||||
|
||||
use aoc2022::day15::process_part_2;
|
||||
|
||||
fn main() {
|
||||
let file = fs::read_to_string("./inputs/day15.txt").unwrap();
|
||||
println!("{}", process_part_2(&file, 4_000_000));
|
||||
}
|
||||
126
src/day15.rs
Normal file
126
src/day15.rs
Normal file
@@ -0,0 +1,126 @@
|
||||
use itertools::{Itertools, MinMaxResult};
|
||||
use nom::bytes::complete::tag;
|
||||
use nom::character::complete;
|
||||
use nom::character::complete::newline;
|
||||
use nom::multi::separated_list1;
|
||||
use nom::sequence::{preceded, separated_pair};
|
||||
use nom::IResult;
|
||||
|
||||
pub fn process_part_1(input: &str, row: isize) -> usize {
|
||||
let sensors = parse_input(input).unwrap().1;
|
||||
let (min_x, max_x) = match sensors
|
||||
.iter()
|
||||
.flat_map(|sensor| {
|
||||
[
|
||||
sensor.pos.0 + sensor.distance as isize,
|
||||
sensor.pos.0 - sensor.distance as isize,
|
||||
]
|
||||
})
|
||||
.minmax()
|
||||
{
|
||||
MinMaxResult::NoElements => panic!("invalid sensors!"),
|
||||
MinMaxResult::OneElement(e) => (e, e),
|
||||
MinMaxResult::MinMax(min, max) => (min, max),
|
||||
};
|
||||
(min_x..=max_x)
|
||||
.filter(|&i| sensors.iter().any(|s| s.is_in_range((i, row))))
|
||||
.count()
|
||||
}
|
||||
|
||||
pub fn process_part_2(input: &str, max_n: isize) -> isize {
|
||||
let sensors = parse_input(input).unwrap().1;
|
||||
sensors
|
||||
.iter()
|
||||
.find_map(|sensor| {
|
||||
// just outside the leftmost side of the sensor
|
||||
((sensor.pos.0 - (sensor.distance as isize) - 1).max(0)
|
||||
..=(sensor.pos.0 + (sensor.distance as isize) + 1).min(max_n))
|
||||
.zip(sensor.pos.1..=max_n)
|
||||
.find_map(|pos| {
|
||||
sensors
|
||||
.iter()
|
||||
.all(|s| !s.is_in_range(pos))
|
||||
.then_some(pos.0 * 4_000_000 + pos.1)
|
||||
})
|
||||
})
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
type Pos = (isize, isize);
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
struct Sensor {
|
||||
pos: Pos,
|
||||
closest_beacon: Pos,
|
||||
distance: usize,
|
||||
}
|
||||
|
||||
impl Sensor {
|
||||
fn new(sensor_pos: Pos, beacon_pos: Pos) -> Self {
|
||||
Self {
|
||||
pos: sensor_pos,
|
||||
closest_beacon: beacon_pos,
|
||||
distance: manhattan(sensor_pos, beacon_pos),
|
||||
}
|
||||
}
|
||||
|
||||
fn is_in_range(&self, p: Pos) -> bool {
|
||||
if self.closest_beacon == p {
|
||||
return false;
|
||||
}
|
||||
|
||||
self.distance >= manhattan(self.pos, p)
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_input(input: &str) -> IResult<&str, Vec<Sensor>> {
|
||||
separated_list1(newline, parse_sensor)(input)
|
||||
}
|
||||
|
||||
fn parse_sensor(input: &str) -> IResult<&str, Sensor> {
|
||||
let (input, sensor_pos) = preceded(tag("Sensor at "), parse_pos)(input)?;
|
||||
let (input, beacon_pos) = preceded(tag(": closest beacon is at "), parse_pos)(input)?;
|
||||
Ok((input, Sensor::new(sensor_pos, beacon_pos)))
|
||||
}
|
||||
|
||||
fn parse_pos(input: &str) -> IResult<&str, Pos> {
|
||||
let (input, (x, y)) = separated_pair(
|
||||
preceded(tag("x="), complete::i32),
|
||||
tag(", "),
|
||||
preceded(tag("y="), complete::i32),
|
||||
)(input)?;
|
||||
Ok((input, (x as isize, y as isize)))
|
||||
}
|
||||
|
||||
fn manhattan(a: Pos, b: Pos) -> usize {
|
||||
a.0.abs_diff(b.0) + a.1.abs_diff(b.1)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const INPUT: &str = "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";
|
||||
|
||||
#[test]
|
||||
fn day1() {
|
||||
assert_eq!(process_part_1(INPUT, 10), 26);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn day2() {
|
||||
assert_eq!(process_part_2(INPUT, 20), 56000011);
|
||||
}
|
||||
}
|
||||
@@ -15,3 +15,4 @@ pub mod day11;
|
||||
pub mod day12;
|
||||
pub mod day13;
|
||||
pub mod day14;
|
||||
pub mod day15;
|
||||
|
||||
Reference in New Issue
Block a user