@@ -54,7 +54,7 @@ fn get_guard_path_positions_assuming_no_loops(
54
54
direction : UP ,
55
55
} ;
56
56
let mut visited = FxHashSet :: with_capacity_and_hasher ( 10_000 , Default :: default ( ) ) ;
57
- let mut jump_table = JumpTable :: default ( ) ;
57
+ let mut jump_table = JumpTable :: with_capacity_and_hasher ( 10_000 , Default :: default ( ) ) ;
58
58
let mut old_guard = guard. clone ( ) ;
59
59
60
60
while let Some ( new_guard) = guard. turn ( grid) {
@@ -68,29 +68,6 @@ fn get_guard_path_positions_assuming_no_loops(
68
68
( visited, jump_table)
69
69
}
70
70
71
- #[ inline]
72
- fn has_loops ( grid : & Grid < char > , start_pos : Point , jump_table : & JumpTable ) -> bool {
73
- // remove entries from jump_table that contain pos between key and value
74
- let mut guard = Guard {
75
- position : start_pos,
76
- direction : UP ,
77
- } ;
78
-
79
- let mut visited = FxHashSet :: with_capacity_and_hasher ( 10_000 , Default :: default ( ) ) ;
80
- visited. insert ( guard. clone ( ) ) ;
81
-
82
- while let Some ( new_guard) = guard. turn ( & grid) {
83
- guard = new_guard;
84
- if !visited. insert ( guard. clone ( ) ) {
85
- return true ;
86
- }
87
- if let Some ( next_guard) = jump_table. get ( & guard) {
88
- guard = next_guard. clone ( ) ;
89
- }
90
- }
91
- false
92
- }
93
-
94
71
pub fn part_one ( input : & str ) -> Option < u32 > {
95
72
let ( grid, start_pos) = parse_grid_and_start_pos ( input) ;
96
73
let ( visited, _) = get_guard_path_positions_assuming_no_loops ( & grid, start_pos) ;
@@ -105,15 +82,33 @@ pub fn part_two(input: &str) -> Option<u32> {
105
82
. par_iter ( )
106
83
. filter ( |& & pos| {
107
84
let mut grid = grid. clone ( ) ;
108
- let jump_table: JumpTable = JumpTable :: from_iter (
109
- jump_table
110
- . iter ( )
111
- . filter ( |( key, val) | !pos. is_between_inclusive ( & key. position , & val. position ) )
112
- . map ( |( key, val) | ( key. clone ( ) , val. clone ( ) ) ) ,
113
- ) ;
114
85
grid[ pos] = 'O' ;
115
86
116
- has_loops ( & grid, pos, & jump_table)
87
+ let jump_table: JumpTable = jump_table
88
+ . iter ( )
89
+ . filter ( |( key, val) | !pos. is_between_inclusive ( & key. position , & val. position ) )
90
+ . map ( |( key, val) | ( key. clone ( ) , val. clone ( ) ) )
91
+ . collect ( ) ;
92
+
93
+ // remove entries from jump_table that contain pos between key and value
94
+ let mut guard = Guard {
95
+ position : start_pos,
96
+ direction : UP ,
97
+ } ;
98
+
99
+ let mut visited = FxHashSet :: with_capacity_and_hasher ( 10_000 , Default :: default ( ) ) ;
100
+ visited. insert ( guard. clone ( ) ) ;
101
+
102
+ while let Some ( new_guard) = guard. turn ( & grid) {
103
+ guard = new_guard;
104
+ if !visited. insert ( guard. clone ( ) ) {
105
+ return true ;
106
+ }
107
+ if let Some ( next_guard) = jump_table. get ( & guard) {
108
+ guard = next_guard. clone ( ) ;
109
+ }
110
+ }
111
+ false
117
112
} )
118
113
. count ( ) ;
119
114
0 commit comments