@@ -32,8 +32,8 @@ fn part1(input: &str) -> usize {
32
32
. count ( )
33
33
}
34
34
35
- // try to do it without bruteforce
36
- fn _part2_clever ( input : & str ) -> usize {
35
+ // try to do it without bruteforce, but is wrong. gives 2019 instead of 2008
36
+ fn part2_clever ( input : & str ) -> usize {
37
37
let directions = [ ( N , '^' ) , ( E , '>' ) , ( S , 'v' ) , ( W , '<' ) ] ;
38
38
let get_direction = |char : char | directions. iter ( ) . find ( |( _, c) | char == * c) . unwrap ( ) . 0 ;
39
39
let get_next_direction =
@@ -56,32 +56,24 @@ fn _part2_clever(input: &str) -> usize {
56
56
} else {
57
57
m[ newpos] = m[ pos] ;
58
58
pos = newpos;
59
- // if putting a obstruction at the new tile will make the guard join a path he already took
60
59
let next_next_direction = get_next_direction ( next_direction. 1 ) ;
61
60
for tile in m. in_direction ( pos, get_next_direction ( m[ pos] ) . 0 ) {
62
- if m[ tile] == next_direction. 1
63
- || ( m[ tile] == next_next_direction. 1
64
- && m[ m. move_in_direction ( tile, next_direction. 0 ) . unwrap ( ) ] == '#' )
61
+ if m[ tile] == next_next_direction. 1
62
+ && m[ m. move_in_direction ( tile, next_direction. 0 ) . unwrap ( ) ] == '#'
65
63
{
66
64
let move_in_direction = m. move_in_direction ( pos, direction) . unwrap ( ) ;
67
- if move_in_direction != start {
65
+ if move_in_direction != start && m [ move_in_direction ] != '#' {
68
66
possible_obstructions. insert ( move_in_direction) ;
69
67
}
70
68
break ;
71
69
}
72
70
}
73
71
}
74
72
}
75
- for pos in possible_obstructions. iter ( ) {
76
- m[ * pos] = 'O'
77
- }
78
- for row in m. iter ( ) {
79
- println ! ( "{}" , row. iter( ) . collect:: <String >( ) ) ;
80
- }
81
73
possible_obstructions. len ( )
82
74
}
83
75
84
- // bruteforce but thanks to rayon its still ~250ms to run
76
+ // bruteforce but thanks to rayon its still really fast
85
77
fn part2 ( input : & str ) -> usize {
86
78
let directions = [ ( N , '^' ) , ( E , '>' ) , ( S , 'v' ) , ( W , '<' ) ] ;
87
79
let get_direction = |char : char | directions. iter ( ) . find ( |( _, c) | char == * c) . unwrap ( ) . 0 ;
@@ -129,7 +121,10 @@ fn part2(input: &str) -> usize {
129
121
fn main ( ) {
130
122
let input = include_str ! ( "../input.txt" ) ;
131
123
println ! ( "Part 1: {}" , part1( input) ) ;
132
- println ! ( "Part 2: {}" , part2( input) ) ;
124
+ let p2 = part2 ( input) ;
125
+ println ! ( "Part 2: {p2}" ) ;
126
+ assert_eq ! ( p2, 2008 ) ;
127
+ assert_eq ! ( p2, part2_clever( input) ) ;
133
128
}
134
129
135
130
#[ cfg( test) ]
@@ -153,4 +148,8 @@ mod tests {
153
148
fn part2 ( ) {
154
149
assert_eq ! ( super :: part2( INPUT ) , 6 ) ;
155
150
}
151
+ #[ test]
152
+ fn part2_clever ( ) {
153
+ assert_eq ! ( super :: part2( INPUT ) , 6 ) ;
154
+ }
156
155
}
0 commit comments