File tree Expand file tree Collapse file tree 1 file changed +23
-40
lines changed Expand file tree Collapse file tree 1 file changed +23
-40
lines changed Original file line number Diff line number Diff line change @@ -20,50 +20,33 @@ pub fn parse(input: &str) -> Input<'_> {
20
20
input. lines ( ) . collect ( )
21
21
}
22
22
23
- pub fn part1 ( input : & Input < ' _ > ) -> u32 {
24
- let mut result = 0 ;
23
+ pub fn part1 ( input : & Input < ' _ > ) -> usize {
25
24
let mut seen = FastSet :: new ( ) ;
26
-
27
- for line in input {
28
- result += 1 ;
29
-
30
- for token in line. split_ascii_whitespace ( ) {
31
- // Insert returns `false` if the value is already in the set.
32
- if !seen. insert ( token. as_bytes ( ) ) {
33
- result -= 1 ;
34
- break ;
35
- }
36
- }
37
-
38
- seen. clear ( ) ;
39
- }
40
-
41
- result
25
+ input
26
+ . iter ( )
27
+ . filter ( |line| {
28
+ seen. clear ( ) ;
29
+ line. split_ascii_whitespace ( ) . all ( |token| seen. insert ( token. as_bytes ( ) ) )
30
+ } )
31
+ . count ( )
42
32
}
43
33
44
- pub fn part2 ( input : & Input < ' _ > ) -> u32 {
45
- let mut result = 0 ;
46
- let mut seen = FastSet :: new ( ) ;
47
-
48
- for line in input {
49
- result += 1 ;
50
-
51
- for token in line. split_ascii_whitespace ( ) {
52
- // Calculate the frequency of each letter, as anagrams will have the same values.
53
- let mut freq = [ 0_u8 ; 26 ] ;
54
-
55
- for b in token. bytes ( ) {
56
- freq[ ( b - b'a' ) as usize ] += 1 ;
57
- }
58
-
59
- if !seen. insert ( freq) {
60
- result -= 1 ;
61
- break ;
62
- }
34
+ pub fn part2 ( input : & Input < ' _ > ) -> usize {
35
+ // Calculate the frequency of each letter as anagrams will have the same values.
36
+ fn convert ( token : & str ) -> [ u8 ; 26 ] {
37
+ let mut freq = [ 0 ; 26 ] ;
38
+ for b in token. bytes ( ) {
39
+ freq[ ( b - b'a' ) as usize ] += 1 ;
63
40
}
64
-
65
- seen. clear ( ) ;
41
+ freq
66
42
}
67
43
68
- result
44
+ let mut seen = FastSet :: new ( ) ;
45
+ input
46
+ . iter ( )
47
+ . filter ( |line| {
48
+ seen. clear ( ) ;
49
+ line. split_ascii_whitespace ( ) . all ( |token| seen. insert ( convert ( token) ) )
50
+ } )
51
+ . count ( )
69
52
}
You can’t perform that action at this time.
0 commit comments