-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
54 lines (42 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
52
53
54
package main
import (
"github.com/danvolchek/AdventOfCode/lib"
)
// common returns the items that are common between first and second
func common(first, second []byte) []byte {
var result lib.Set[byte]
for _, f := range first {
for _, s := range second {
if f == s {
result.Add(f)
}
}
}
return result.Items()
}
// priority returns the priority of an item.
func priority(item byte) int {
if item <= 'Z' {
return int(item-'A') + 27
}
return int(item-'a') + 1
}
func solve(rucksacks [][]byte) int {
sum := 0
// sum up the priority of the common item between every three elves
for i := 0; i < len(rucksacks); i += 3 {
first := rucksacks[i]
second := rucksacks[i+1]
third := rucksacks[i+2]
sum += priority(common(first, common(second, third))[0])
}
return sum
}
func main() {
solver := lib.Solver[[][]byte, int]{
ParseF: lib.ParseLine(lib.ToByteSlice),
SolveF: solve,
}
solver.Expect("vJrwpWtwJgWrhcsFMMfFFhFp\njqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL\nPmmdzqPrVvPwwTWBwg\nwMqvLMZHhHMvwLHjbvcjnnSBnvTQFn\nttgJtRGJQctTZtZT\nCrZsJsPPZsGzwwsLwLmpwMDw", 70)
solver.Verify(2567)
}