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::{
IResult,
character::complete::{self,multispace1},
bytes::streaming::tag,
character::complete::{self, multispace1},
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))>> {
let (input, coords) = separated_list1(
@@ -12,19 +13,12 @@ fn parse_input(input: &str) -> IResult<&str, Vec<((i32, i32), (i32, i32))>> {
preceded(
tag("Sensor at x="),
separated_pair(
separated_pair(
complete::i32,
tag(", y="),
complete::i32,
),
separated_pair(complete::i32, tag(", y="), complete::i32),
tag(": closest beacon is at x="),
separated_pair(
complete::i32,
tag(", y="),
complete::i32,
))
)
)(input)?;
separated_pair(complete::i32, tag(", y="), complete::i32),
),
),
)(input)?;
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();
}
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 (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 (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() {
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}");
// }
// Naive method
// let mut beacon_map = HashSet::new();
@@ -85,14 +99,25 @@ fn main() {
// println!("Number of positions with y={test_y}: {positions}");
// A slightly less naive approach
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);
}
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;
}