@@ -6,19 +6,14 @@ import (
6
6
"strings"
7
7
)
8
8
9
- type Compartment struct {
10
- items string
11
- }
12
-
13
- func newCompartment (items string ) Compartment {
14
- return Compartment {items : items }
15
- }
16
-
17
- func (c Compartment ) findDuplicate (alt Compartment ) rune {
18
- for _ , char := range c .items {
19
- for _ , altChar := range alt .items {
20
- if char == altChar {
21
- return char
9
+ // I hate this function so much
10
+ func findCommonItem (sacks ... string ) rune {
11
+ for _ , aChar := range sacks [0 ] {
12
+ for _ , bChar := range sacks [1 ] {
13
+ for _ , cChar := range sacks [2 ] {
14
+ if aChar == bChar && bChar == cChar {
15
+ return aChar
16
+ }
22
17
}
23
18
}
24
19
}
@@ -35,20 +30,20 @@ func main() {
35
30
sacks := strings .Split (string (dat ), "\n " )
36
31
37
32
var sum int32
38
- // items in each compartment
33
+ grp := make ([]string , 0 )
34
+
39
35
for _ , sack := range sacks {
40
- c1 , c2 := splitItems (sack )
41
- dup := c1 .findDuplicate (c2 )
36
+ grp = append (grp , sack )
42
37
43
- sum += convert (dup )
38
+ if len (grp ) == 3 {
39
+ common := findCommonItem (grp ... )
40
+ sum += convert (common )
41
+ // new group
42
+ grp = make ([]string , 0 )
43
+ }
44
44
}
45
45
46
- fmt .Printf ("the sum of all duplicate items is %v" , sum )
47
- }
48
-
49
- func splitItems (input string ) (Compartment , Compartment ) {
50
- half := len (input ) / 2
51
- return newCompartment (input [:half ]), newCompartment (input [half :])
46
+ fmt .Printf ("the sum of all common items is %v" , sum )
52
47
}
53
48
54
49
func convert (charCode rune ) int32 {
0 commit comments