generated from devries/aoc_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.go
79 lines (59 loc) · 1.33 KB
/
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
70
71
72
73
74
75
76
77
78
79
package day04p2
import (
"fmt"
"io"
"strconv"
"strings"
"aoc/utils"
)
func Solve(r io.Reader) any {
lines := utils.ReadLines(r)
// this will start at 0 and we will add to it, so the multiplier is this plus 1
multiplier := make(map[int]int)
max := 0
for i, 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++
}
}
for j := i + 1; j <= i+matches; j++ {
// Add cards equal to the number of copies of this card you have
multiplier[j] = multiplier[j] + multiplier[i] + 1
}
max = i
}
sum := 0
for i := 0; i <= max; i++ {
if utils.Verbose {
fmt.Printf("Copies of Card %d: %d\n", i+1, multiplier[i]+1)
}
sum += multiplier[i] + 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
}