11use crate :: solutions:: Solution ;
2+ use crate :: utils:: point:: Point ;
3+ use std:: collections:: HashMap ;
24
35pub struct Day03 ;
46
57impl Solution for Day03 {
6- fn part_one ( & self , _input : & str ) -> String {
7- String :: from ( "0" )
8+ fn part_one ( & self , input : & str ) -> String {
9+ let mut visited_houses: HashMap < Point , u64 > = HashMap :: new ( ) ;
10+ let mut current = Point :: new ( 0 , 0 ) ;
11+
12+ visited_houses. insert ( current, 1 ) ;
13+
14+ for b in input. bytes ( ) {
15+ current = match b {
16+ b'>' => current. east ( ) ,
17+ b'<' => current. west ( ) ,
18+ b'^' => current. north ( ) ,
19+ b'v' => current. south ( ) ,
20+ _ => unreachable ! ( ) ,
21+ } ;
22+ visited_houses
23+ . entry ( current)
24+ . and_modify ( |e| * e += 1 )
25+ . or_insert ( 1 ) ;
26+ }
27+
28+ visited_houses. keys ( ) . count ( ) . to_string ( )
829 }
930
1031 fn part_two ( & self , _input : & str ) -> String {
@@ -18,6 +39,8 @@ mod tests {
1839
1940 #[ test]
2041 fn part_one_example_test ( ) {
21- assert_eq ! ( "0" , Day03 . part_one( "0" ) ) ;
42+ assert_eq ! ( "2" , Day03 . part_one( ">" ) ) ;
43+ assert_eq ! ( "4" , Day03 . part_one( "^>v<" ) ) ;
44+ assert_eq ! ( "2" , Day03 . part_one( "^v^v^v^v^v" ) ) ;
2245 }
2346}
0 commit comments