@@ -54,7 +54,7 @@ fn get_guard_path_positions_assuming_no_loops(
5454 direction : UP ,
5555 } ;
5656 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 ( ) ) ;
5858 let mut old_guard = guard. clone ( ) ;
5959
6060 while let Some ( new_guard) = guard. turn ( grid) {
@@ -68,29 +68,6 @@ fn get_guard_path_positions_assuming_no_loops(
6868 ( visited, jump_table)
6969}
7070
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-
9471pub fn part_one ( input : & str ) -> Option < u32 > {
9572 let ( grid, start_pos) = parse_grid_and_start_pos ( input) ;
9673 let ( visited, _) = get_guard_path_positions_assuming_no_loops ( & grid, start_pos) ;
@@ -105,15 +82,33 @@ pub fn part_two(input: &str) -> Option<u32> {
10582 . par_iter ( )
10683 . filter ( |& & pos| {
10784 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- ) ;
11485 grid[ pos] = 'O' ;
11586
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
117112 } )
118113 . count ( ) ;
119114
0 commit comments