From 7a0bc59bd20dff0cab651684375a49b455f0c9fc Mon Sep 17 00:00:00 2001 From: Philippe Zwietering Date: Mon, 18 Sep 2023 23:51:58 +0200 Subject: [PATCH] Idee op papier voor aoc 15 dat zou moeten werken --- advent_of_code/2022/15/src/main.rs | 73 ++++++++++++++++++++---------- 1 file changed, 49 insertions(+), 24 deletions(-) diff --git a/advent_of_code/2022/15/src/main.rs b/advent_of_code/2022/15/src/main.rs index 323ddf4..1c646b2 100644 --- a/advent_of_code/2022/15/src/main.rs +++ b/advent_of_code/2022/15/src/main.rs @@ -1,30 +1,24 @@ -use std::{collections::HashSet, hash::Hash}; use nom::{ + bytes::streaming::tag, + character::complete::{self, multispace1}, + multi::separated_list1, + sequence::{preceded, separated_pair}, IResult, - character::complete::{self,multispace1}, - multi::separated_list1, - sequence::{preceded, separated_pair}, bytes::streaming::tag, }; +use std::collections::HashSet; fn parse_input(input: &str) -> IResult<&str, Vec<((i32, i32), (i32, i32))>> { let (input, coords) = separated_list1( - multispace1, + multispace1, preceded( tag("Sensor at x="), separated_pair( - 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), + tag(": closest beacon is at x="), + 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, sensor_coord: (i32, i32), beacon_coord: (i32, i32), test_line: i32) { +fn fill_line( + beacon_line: &mut HashSet, + 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, 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; }