mirror of
https://github.com/LeMoonStar/AoC24.git
synced 2025-07-08 01:19:59 +02:00
🚧 Day 6: WIP on Part 2
This commit is contained in:
parent
de37fe9390
commit
2241f69905
1 changed files with 44 additions and 5 deletions
|
@ -1,9 +1,13 @@
|
||||||
use super::{Answer, Day, DayImpl};
|
use super::{Answer, Day, DayImpl};
|
||||||
use std::{collections::HashSet, hash::Hash, path::Iter};
|
use std::{
|
||||||
|
collections::{HashMap, HashSet},
|
||||||
|
hash::Hash,
|
||||||
|
path::Iter,
|
||||||
|
};
|
||||||
|
|
||||||
const CURRENT_DAY: u8 = 6;
|
const CURRENT_DAY: u8 = 6;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
pub enum Direction {
|
pub enum Direction {
|
||||||
North,
|
North,
|
||||||
East,
|
East,
|
||||||
|
@ -83,6 +87,21 @@ pub struct PatrolPathIterator<'a> {
|
||||||
map: &'a PatrollingMap,
|
map: &'a PatrollingMap,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'a> PatrolPathIterator<'a> {
|
||||||
|
fn is_loop(&mut self) -> bool {
|
||||||
|
let mut visited = HashSet::new();
|
||||||
|
|
||||||
|
for current in self {
|
||||||
|
if visited.contains(¤t) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
visited.insert(current);
|
||||||
|
}
|
||||||
|
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a> Iterator for PatrolPathIterator<'a> {
|
impl<'a> Iterator for PatrolPathIterator<'a> {
|
||||||
type Item = (Position, Direction);
|
type Item = (Position, Direction);
|
||||||
|
|
||||||
|
@ -124,6 +143,10 @@ impl PatrollingMap {
|
||||||
self.obstacles.contains(&pos)
|
self.obstacles.contains(&pos)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn add_obstruction(&mut self, pos: Position) {
|
||||||
|
self.obstacles.insert(pos);
|
||||||
|
}
|
||||||
|
|
||||||
pub fn iter(&self) -> PatrolPathIterator {
|
pub fn iter(&self) -> PatrolPathIterator {
|
||||||
PatrolPathIterator {
|
PatrolPathIterator {
|
||||||
position: Some(self.start_position),
|
position: Some(self.start_position),
|
||||||
|
@ -193,7 +216,7 @@ impl DayImpl<Data> for Day<CURRENT_DAY> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn expected_results() -> (Answer, Answer) {
|
fn expected_results() -> (Answer, Answer) {
|
||||||
(Answer::Number(41), Answer::Number(0))
|
(Answer::Number(41), Answer::Number(6))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn init(input: &str) -> (Self, Data) {
|
fn init(input: &str) -> (Self, Data) {
|
||||||
|
@ -201,7 +224,6 @@ impl DayImpl<Data> for Day<CURRENT_DAY> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn one(&self, data: &mut Data) -> Answer {
|
fn one(&self, data: &mut Data) -> Answer {
|
||||||
println!("{:?}", data);
|
|
||||||
let visited: HashSet<Position> = data
|
let visited: HashSet<Position> = data
|
||||||
.iter()
|
.iter()
|
||||||
//.inspect(|v| println!("{:?}", v))
|
//.inspect(|v| println!("{:?}", v))
|
||||||
|
@ -211,6 +233,23 @@ impl DayImpl<Data> for Day<CURRENT_DAY> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn two(&self, data: &mut Data) -> Answer {
|
fn two(&self, data: &mut Data) -> Answer {
|
||||||
Answer::Number(0)
|
let main_path_visited: HashSet<Position> = data
|
||||||
|
.iter()
|
||||||
|
//.inspect(|v| println!("{:?}", v))
|
||||||
|
.map(|v| v.0)
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
|
||||||
|
// NOTE: More than 1812
|
||||||
|
Answer::Number(
|
||||||
|
main_path_visited
|
||||||
|
.iter()
|
||||||
|
.filter(|pos| {
|
||||||
|
let mut map = data.clone();
|
||||||
|
map.add_obstruction(**pos);
|
||||||
|
map.iter().is_loop()
|
||||||
|
})
|
||||||
|
.count() as u64
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue