@@ -19,7 +19,7 @@ pub fn input_generator(input: &str) -> Map {
1919 }
2020}
2121
22- #[ derive( PartialEq , Copy , Clone ) ]
22+ #[ derive( PartialEq , Eq , Copy , Clone ) ]
2323enum Direction {
2424 Up ,
2525 Down ,
@@ -46,7 +46,8 @@ pub fn part1(input: &Map) -> usize {
4646 let ( mut row, mut col) = input. current ;
4747 let row_limit = input. map . len ( ) ;
4848 let col_limit = input. map [ 0 ] . len ( ) ;
49- let mut total: usize = 0 ;
49+ let mut total: usize = 1 ;
50+ visited[ row] [ col] = 'X' ;
5051 loop {
5152 let ( next_row, next_col) = match direction {
5253 Direction :: Up if row > 0 => ( row - 1 , col) ,
@@ -68,6 +69,14 @@ pub fn part1(input: &Map) -> usize {
6869 col = next_col;
6970 }
7071 }
72+ // for i in visited {
73+ // for j in i {
74+ // print!("{j}");
75+ // }
76+ // println!();
77+ // }
78+ // println!();
79+ // println!();
7180 total
7281}
7382
@@ -80,7 +89,9 @@ pub fn part2(input: &Map) -> usize {
8089 let col_limit = input. map [ 0 ] . len ( ) ;
8190
8291 let ( mut row, mut col) = input. current ;
92+ let mut visited = input. map . clone ( ) ;
8393
94+ visited[ row] [ col] = 'X' ;
8495 loop {
8596 let ( next_row, next_col) = match direction {
8697 Direction :: Up if row > 0 => ( row - 1 , col) ,
@@ -92,7 +103,11 @@ pub fn part2(input: &Map) -> usize {
92103
93104 if input. map [ next_row] [ next_col] == '#' {
94105 direction = direction. rotate ( ) ;
106+ } else if visited[ next_row] [ next_col] == 'X' {
107+ row = next_row;
108+ col = next_col;
95109 } else {
110+ visited[ next_row] [ next_col] = 'X' ;
96111 let mut loop_visited = input. map . clone ( ) ;
97112 let mut loop_direction = direction;
98113 loop_visited[ next_row] [ next_col] = 'O' ;
@@ -101,6 +116,8 @@ pub fn part2(input: &Map) -> usize {
101116
102117 let ( mut loop_row, mut loop_col) = ( row, col) ;
103118
119+ row = next_row;
120+ col = next_col;
104121 loop {
105122 let ( next_loop_row, next_loop_col) = match loop_direction {
106123 Direction :: Up if loop_row > 0 => ( loop_row - 1 , loop_col) ,
@@ -123,26 +140,22 @@ pub fn part2(input: &Map) -> usize {
123140 }
124141 std:: collections:: hash_map:: Entry :: Occupied ( entry) => {
125142 if * entry. get ( ) == loop_direction {
126- total += 1 ;
127143 loop_visited[ loop_row] [ loop_col] = 'T' ;
128- for i in loop_visited {
129- for j in i {
130- print ! ( "{j}" ) ;
131- }
132- println ! ( ) ;
133- }
134- println ! ( ) ;
135- println ! ( ) ;
144+ // for i in loop_visited {
145+ // for j in i {
146+ // print!("{j}");
147+ // }
148+ // println!();
149+ // }
150+ // println!();
151+ // println!();
152+ total += 1 ;
136153 break ;
137154 }
138155 }
139156 }
140157 }
141158 }
142-
143- // total += 1;
144- row = next_row;
145- col = next_col;
146159 }
147160 }
148161 total
0 commit comments