11use aoc_runner_derive:: { aoc, aoc_generator} ;
2+ use std:: collections:: HashMap ;
23
34#[ derive( Debug ) ]
45pub struct Map {
@@ -18,6 +19,7 @@ pub fn input_generator(input: &str) -> Map {
1819 }
1920}
2021
22+ #[ derive( PartialEq , Copy , Clone ) ]
2123enum Direction {
2224 Up ,
2325 Down ,
@@ -40,31 +42,110 @@ impl Direction {
4042#[ aoc( day6, part1) ]
4143pub fn part1 ( input : & Map ) -> usize {
4244 let mut direction: Direction = Direction :: Up ;
43- let ( mut y, mut x) = input. current ;
44- let mut new_map = input. map . clone ( ) ;
45- let mut count = 0 ;
46- while y < input. map . len ( ) && x < input. map [ y] . len ( ) {
47- if input. map [ y] [ x] == '#' {
48- // put back
49- match direction {
50- Direction :: Up => y += 1 ,
51- Direction :: Down => y -= 1 ,
52- Direction :: Left => x += 1 ,
53- Direction :: Right => x -= 1 ,
54- }
45+ let mut visited = input. map . clone ( ) ;
46+ let ( mut row, mut col) = input. current ;
47+ let row_limit = input. map . len ( ) ;
48+ let col_limit = input. map [ 0 ] . len ( ) ;
49+ let mut total: usize = 0 ;
50+ loop {
51+ let ( next_row, next_col) = match direction {
52+ Direction :: Up if row > 0 => ( row - 1 , col) ,
53+ Direction :: Down if row < row_limit - 1 => ( row + 1 , col) ,
54+ Direction :: Left if col > 0 => ( row, col - 1 ) ,
55+ Direction :: Right if col < col_limit - 1 => ( row, col + 1 ) ,
56+ _ => break ,
57+ } ;
58+
59+ if visited[ next_row] [ next_col] == '#' {
5560 direction = direction. rotate ( ) ;
56- } else if new_map[ y] [ x] != 'X' {
57- count += 1 ;
58- new_map[ y] [ x] = 'X' ;
61+ } else if visited[ next_row] [ next_col] == 'X' {
62+ row = next_row;
63+ col = next_col;
64+ } else {
65+ visited[ next_row] [ next_col] = 'X' ;
66+ total += 1 ;
67+ row = next_row;
68+ col = next_col;
5969 }
60- match direction {
61- Direction :: Up => y -= 1 ,
62- Direction :: Down => y += 1 ,
63- Direction :: Left => x -= 1 ,
64- Direction :: Right => x += 1 ,
70+ }
71+ total
72+ }
73+
74+ #[ aoc( day6, part2) ]
75+ pub fn part2 ( input : & Map ) -> usize {
76+ let mut total: usize = 0 ;
77+
78+ let mut direction: Direction = Direction :: Up ;
79+ let row_limit = input. map . len ( ) ;
80+ let col_limit = input. map [ 0 ] . len ( ) ;
81+
82+ let ( mut row, mut col) = input. current ;
83+
84+ loop {
85+ let ( next_row, next_col) = match direction {
86+ Direction :: Up if row > 0 => ( row - 1 , col) ,
87+ Direction :: Down if row < row_limit - 1 => ( row + 1 , col) ,
88+ Direction :: Left if col > 0 => ( row, col - 1 ) ,
89+ Direction :: Right if col < col_limit - 1 => ( row, col + 1 ) ,
90+ _ => break ,
91+ } ;
92+
93+ if input. map [ next_row] [ next_col] == '#' {
94+ direction = direction. rotate ( ) ;
95+ } else {
96+ let mut loop_visited = input. map . clone ( ) ;
97+ let mut loop_direction = direction;
98+ loop_visited[ next_row] [ next_col] = 'O' ;
99+
100+ let mut visited: HashMap < ( usize , usize ) , Direction > = HashMap :: new ( ) ;
101+
102+ let ( mut loop_row, mut loop_col) = ( row, col) ;
103+
104+ loop {
105+ let ( next_loop_row, next_loop_col) = match loop_direction {
106+ Direction :: Up if loop_row > 0 => ( loop_row - 1 , loop_col) ,
107+ Direction :: Down if loop_row < row_limit - 1 => ( loop_row + 1 , loop_col) ,
108+ Direction :: Left if loop_col > 0 => ( loop_row, loop_col - 1 ) ,
109+ Direction :: Right if loop_col < col_limit - 1 => ( loop_row, loop_col + 1 ) ,
110+ _ => break ,
111+ } ;
112+ if loop_visited[ next_loop_row] [ next_loop_col] == '#'
113+ || loop_visited[ next_loop_row] [ next_loop_col] == 'O'
114+ {
115+ loop_direction = loop_direction. rotate ( ) ;
116+ } else {
117+ loop_row = next_loop_row;
118+ loop_col = next_loop_col;
119+ loop_visited[ next_loop_row] [ next_loop_col] = 'X' ;
120+ match visited. entry ( ( loop_row, loop_col) ) {
121+ std:: collections:: hash_map:: Entry :: Vacant ( entry) => {
122+ entry. insert ( loop_direction) ;
123+ }
124+ std:: collections:: hash_map:: Entry :: Occupied ( entry) => {
125+ if * entry. get ( ) == loop_direction {
126+ total += 1 ;
127+ 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 ! ( ) ;
136+ break ;
137+ }
138+ }
139+ }
140+ }
141+ }
142+
143+ // total += 1;
144+ row = next_row;
145+ col = next_col;
65146 }
66147 }
67- count
148+ total
68149}
69150
70151#[ cfg( test) ]
@@ -87,8 +168,8 @@ mod tests {
87168 assert_eq ! ( 41 , part1( & input_generator( INPUT ) ) )
88169 }
89170
90- // #[test]
91- // fn test_part2() {
92- // assert_eq!(123 , part2(&input_generator(INPUT)))
93- // }
171+ #[ test]
172+ fn test_part2 ( ) {
173+ assert_eq ! ( 6 , part2( & input_generator( INPUT ) ) )
174+ }
94175}
0 commit comments