Idee op papier voor aoc 15 dat zou moeten werken

This commit is contained in:
2023-09-18 23:51:58 +02:00
parent fdf044144f
commit 7a0bc59bd2

View File

@@ -1,10 +1,11 @@
use std::{collections::HashSet, hash::Hash};
use nom::{ use nom::{
IResult, bytes::streaming::tag,
character::complete::{self,multispace1}, character::complete::{self, multispace1},
multi::separated_list1, multi::separated_list1,
sequence::{preceded, separated_pair}, bytes::streaming::tag, sequence::{preceded, separated_pair},
IResult,
}; };
use std::collections::HashSet;
fn parse_input(input: &str) -> IResult<&str, Vec<((i32, i32), (i32, i32))>> { fn parse_input(input: &str) -> IResult<&str, Vec<((i32, i32), (i32, i32))>> {
let (input, coords) = separated_list1( let (input, coords) = separated_list1(
@@ -12,18 +13,11 @@ fn parse_input(input: &str) -> IResult<&str, Vec<((i32, i32), (i32, i32))>> {
preceded( preceded(
tag("Sensor at x="), tag("Sensor at x="),
separated_pair( separated_pair(
separated_pair( separated_pair(complete::i32, tag(", y="), complete::i32),
complete::i32,
tag(", y="),
complete::i32,
),
tag(": closest beacon is at x="), tag(": closest beacon is at x="),
separated_pair( separated_pair(complete::i32, tag(", y="), complete::i32),
complete::i32, ),
tag(", y="), ),
complete::i32,
))
)
)(input)?; )(input)?;
Ok((input, coords)) Ok((input, coords))
@@ -33,7 +27,11 @@ fn manhattan_dist(a: (i32, i32), b: (i32, i32)) -> i32 {
return (a.0 - b.0).abs() + (a.1 - b.1).abs(); return (a.0 - b.0).abs() + (a.1 - b.1).abs();
} }
fn fill_map(beacon_map: &mut HashSet<(i32, i32)>, sensor_coord: (i32, i32), beacon_coord: (i32, i32)) { fn _fill_map(
beacon_map: &mut HashSet<(i32, i32)>,
sensor_coord: (i32, i32),
beacon_coord: (i32, i32),
) {
let dist = manhattan_dist(sensor_coord, beacon_coord); let dist = manhattan_dist(sensor_coord, beacon_coord);
let (sx, sy) = sensor_coord; let (sx, sy) = sensor_coord;
@@ -47,7 +45,12 @@ fn fill_map(beacon_map: &mut HashSet<(i32, i32)>, sensor_coord: (i32, i32), beac
} }
} }
fn fill_line(beacon_line: &mut HashSet<i32>, sensor_coord: (i32, i32), beacon_coord: (i32, i32), test_line: i32) { fn fill_line(
beacon_line: &mut HashSet<i32>,
sensor_coord: (i32, i32),
beacon_coord: (i32, i32),
test_line: i32,
) {
let dist = manhattan_dist(sensor_coord, beacon_coord); let dist = manhattan_dist(sensor_coord, beacon_coord);
let (sx, sy) = sensor_coord; let (sx, sy) = sensor_coord;
@@ -60,6 +63,18 @@ fn fill_line(beacon_line: &mut HashSet<i32>, sensor_coord: (i32, i32), beacon_co
} }
} }
enum EventType {
Start,
Intersection,
End,
}
struct Event {
x: i32,
y: i32,
t: EventType,
}
fn main() { fn main() {
let test_y = 2000000; let test_y = 2000000;
@@ -72,7 +87,6 @@ fn main() {
// println!("Sensor at x={sx}, y={sy}: closest beacon is at x={bx}, y={by}"); // println!("Sensor at x={sx}, y={sy}: closest beacon is at x={bx}, y={by}");
// } // }
// Naive method // Naive method
// let mut beacon_map = HashSet::new(); // let mut beacon_map = HashSet::new();
@@ -85,14 +99,25 @@ fn main() {
// println!("Number of positions with y={test_y}: {positions}"); // println!("Number of positions with y={test_y}: {positions}");
// A slightly less naive approach // A slightly less naive approach
let mut beacon_line = HashSet::new(); let mut beacon_line = HashSet::new();
for (sensor, beacon) in coord_list { for (sensor, beacon) in coord_list.clone() {
fill_line(&mut beacon_line, sensor, beacon, test_y); fill_line(&mut beacon_line, sensor, beacon, test_y);
} }
println!("Number of positions with y={test_y}: {}", beacon_line.len() - 1); println!(
"Number of positions with y={test_y}: {}",
beacon_line.len() - 1
);
// Part 2
//
// Ik denk dat dit misschien wel met een sweep line zou moeten kunnen.
// Je weet immers waar alle ruiten liggen en je kan die hele mik sorteren.
// Je krijgt dan 3 event-types: start van een lijnstuk, een kruising en het einde van een lijnstuk.
// Enige is dat je alleen in de daadwerkelijke coordinaat-range zoals in de opgave dingen wilt
// mee laten tellen.
const max_coord: i32 = 4000000;
} }