-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
51 lines (43 loc) · 1.09 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"fmt"
. "github.com/baspar/adventofcode2022/internal"
"github.com/baspar/adventofcode2022/internal/set"
)
type x [2]string
type DayImpl struct {
rucksacks []string
}
func badgeToScore(badge rune) int {
if 'a' <= badge && badge <= 'z' {
return int(badge - 'a' + 1)
} else {
return int(badge - 'A' + 27)
}
}
func (d *DayImpl) Init(lines []string) error {
d.rucksacks = lines
return nil
}
func (d *DayImpl) Part1() (string, error) {
score := 0
for _, rucksacks := range d.rucksacks {
n := len(rucksacks)
rucksack1, rucksack2 := rucksacks[:n/2], rucksacks[n/2:]
badge := set.Intersect([]rune(rucksack1), []rune(rucksack2))
score += badgeToScore(badge[0])
}
return fmt.Sprint(score), nil
}
func (d *DayImpl) Part2() (string, error) {
score := 0
for i := 0; i < len(d.rucksacks); i += 3 {
rucksack1, rucksack2, rucksack3 := d.rucksacks[i], d.rucksacks[i+1], d.rucksacks[i+2]
badge := set.Intersect([]rune(rucksack1), []rune(rucksack2), []rune(rucksack3))[0]
score += badgeToScore(badge)
}
return fmt.Sprint(score), nil
}
func main() {
Run(&DayImpl{}, false)
}