-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
61 lines (48 loc) · 1.14 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
55
56
57
58
59
60
61
package main
import (
"github.com/danvolchek/AdventOfCode/lib"
)
type Rucksack struct {
first, second []byte
}
func parse(line string) Rucksack {
half := len(line) / 2
return Rucksack{
first: []byte(line[:half]),
second: []byte(line[half:]),
}
}
// common returns the item that is common between first and second.
func common(first, second []byte) byte {
for _, f := range first {
for _, s := range second {
if f == s {
return f
}
}
}
panic("no common item")
}
// 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 []Rucksack) int {
sum := 0
// sum up the priority of all the common items
for _, rucksack := range rucksacks {
sum += priority(common(rucksack.first, rucksack.second))
}
return sum
}
func main() {
solver := lib.Solver[[]Rucksack, int]{
ParseF: lib.ParseLine(parse),
SolveF: solve,
}
solver.Expect("vJrwpWtwJgWrhcsFMMfFFhFp\njqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL\nPmmdzqPrVvPwwTWBwg\nwMqvLMZHhHMvwLHjbvcjnnSBnvTQFn\nttgJtRGJQctTZtZT\nCrZsJsPPZsGzwwsLwLmpwMDw", 157)
solver.Verify(8072)
}