File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ use std::fs;
2
2
3
3
fn main ( ) {
4
4
part_1 ( ) ;
5
+ part_2 ( ) ;
5
6
}
6
7
7
8
fn part_1 ( ) {
@@ -28,3 +29,37 @@ fn part_1() {
28
29
29
30
println ! ( "Part 1 result: {} unique houses" , unique_houses) ;
30
31
}
32
+
33
+ fn part_2 ( ) {
34
+ let input = fs:: read_to_string ( "./day-3/input.txt" ) . unwrap ( ) ;
35
+ let mut santa_history = vec ! [ ( 0 , 0 ) ] ;
36
+ let mut robot_santa_history = vec ! [ ( 0 , 0 ) ] ;
37
+
38
+ for ( i, c) in input. chars ( ) . enumerate ( ) {
39
+ let history = if i % 2 == 0 {
40
+ & mut santa_history
41
+ } else {
42
+ & mut robot_santa_history
43
+ } ;
44
+
45
+ let ( x, y) = history. last ( ) . unwrap ( ) . clone ( ) ;
46
+ let ( new_x, new_y) = match c {
47
+ '^' => ( x, y + 1 ) ,
48
+ 'v' => ( x, y - 1 ) ,
49
+ '>' => ( x + 1 , y) ,
50
+ '<' => ( x - 1 , y) ,
51
+ _ => ( x, y) ,
52
+ } ;
53
+
54
+ history. push ( ( new_x, new_y) ) ;
55
+ }
56
+
57
+ santa_history. extend ( robot_santa_history) ;
58
+
59
+ let unique_houses = santa_history
60
+ . iter ( )
61
+ . collect :: < std:: collections:: HashSet < _ > > ( )
62
+ . len ( ) ;
63
+
64
+ println ! ( "Part 2 result: {} unique houses" , unique_houses) ;
65
+ }
You can’t perform that action at this time.
0 commit comments