Skip to content

Commit 27e292f

Browse files
committed
6th day: only use positions from part_1
1 parent 238d887 commit 27e292f

File tree

1 file changed

+16
-23
lines changed

1 file changed

+16
-23
lines changed

src/bin/06.rs

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ fn parse_data(input: &str) -> DataType {
6363
}
6464
}
6565

66-
pub fn part_one(input: &str) -> Option<usize> {
67-
let data: DataType = parse_data(input);
68-
66+
fn visited_positions(data: &DataType) -> FastSet<Point> {
6967
let mut my_position = data.position;
7068
let mut my_direction = data.direction;
7169

@@ -76,7 +74,7 @@ pub fn part_one(input: &str) -> Option<usize> {
7674

7775
let next_position = my_position + my_direction;
7876
if !data.map_size.contains(&next_position) {
79-
break;
77+
return visit;
8078
}
8179

8280
if data.obstructions.contains(&next_position) {
@@ -85,31 +83,30 @@ pub fn part_one(input: &str) -> Option<usize> {
8583
my_position = next_position;
8684
}
8785
}
86+
}
8887

89-
let result = visit.len();
88+
pub fn part_one(input: &str) -> Option<u32> {
89+
let data: DataType = parse_data(input);
90+
91+
let result = visited_positions(&data).len() as u32;
9092

9193
Some(result)
9294
}
9395

9496
pub fn part_two(input: &str) -> Option<u32> {
9597
let data: DataType = parse_data(input);
9698

97-
let mut result = 0;
98-
99-
for y in data.map_size.min_y..=data.map_size.max_y {
100-
for x in data.map_size.min_x..=data.map_size.max_x {
101-
let new_obstruction = Point::new(x, y);
102-
103-
if data.position == new_obstruction || data.obstructions.contains(&new_obstruction) {
104-
continue;
105-
}
106-
107-
let mut visit: FastSet<(Point, Point)> = FastSet::new();
99+
let result = visited_positions(&data)
100+
.into_iter()
101+
.filter(|new_obstruction| new_obstruction != &data.position)
102+
.filter(|new_obstruction| !data.obstructions.contains(new_obstruction))
103+
.filter(|&new_obstruction| {
104+
let mut visit = FastSet::new();
108105

109106
let mut my_position = data.position;
110107
let mut my_direction = data.direction;
111108

112-
let stuck = loop {
109+
loop {
113110
if !visit.insert((my_position, my_direction)) {
114111
break true;
115112
}
@@ -124,13 +121,9 @@ pub fn part_two(input: &str) -> Option<u32> {
124121
} else {
125122
my_position = next_position;
126123
}
127-
};
128-
129-
if stuck {
130-
result += 1;
131124
}
132-
}
133-
}
125+
})
126+
.count() as u32;
134127

135128
Some(result)
136129
}

0 commit comments

Comments
 (0)