generated from devries/aoc_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.go
69 lines (50 loc) · 997 Bytes
/
solution.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
62
63
64
65
66
67
68
69
package day04p1
import (
"io"
"strconv"
"strings"
"aoc/utils"
)
func Solve(r io.Reader) any {
lines := utils.ReadLines(r)
sum := 0
for _, ln := range lines {
card := parseLine(ln)
winners := make(map[int]bool)
for _, v := range card.Winners {
winners[v] = true
}
matches := 0
for _, v := range card.Selected {
if winners[v] {
matches++
}
}
switch matches {
case 0:
// do nothing
default:
sum += 1 << (matches - 1)
}
}
return sum
}
type Card struct {
Winners []int
Selected []int
}
func parseLine(ln string) Card {
titleSplit := strings.Split(ln, ": ")
contents := strings.Split(titleSplit[1], " | ")
return Card{stringToNumbers(contents[0]), stringToNumbers(contents[1])}
}
func stringToNumbers(s string) []int {
groups := strings.Fields(s)
var result []int
for _, v := range groups {
n, err := strconv.Atoi(v)
utils.Check(err, "unable to convert %s to integer", v)
result = append(result, n)
}
return result
}