Skip to content

Commit 3797b7e

Browse files
committed
3b
1 parent e8ac9b4 commit 3797b7e

File tree

1 file changed

+18
-23
lines changed

1 file changed

+18
-23
lines changed

3/main.go

+18-23
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,14 @@ import (
66
"strings"
77
)
88

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+
}
2217
}
2318
}
2419
}
@@ -35,20 +30,20 @@ func main() {
3530
sacks := strings.Split(string(dat), "\n")
3631

3732
var sum int32
38-
// items in each compartment
33+
grp := make([]string, 0)
34+
3935
for _, sack := range sacks {
40-
c1, c2 := splitItems(sack)
41-
dup := c1.findDuplicate(c2)
36+
grp = append(grp, sack)
4237

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+
}
4444
}
4545

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)
5247
}
5348

5449
func convert(charCode rune) int32 {

0 commit comments

Comments
 (0)