-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday04.go
81 lines (65 loc) · 1.8 KB
/
day04.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
80
81
package day04
import (
"2023/day04/card"
"log"
"strconv"
"strings"
)
type Day struct{}
func getLineCard(line string) card.Card {
parts := strings.Split(line, ": ")
sId := strings.ReplaceAll(parts[0], "Card", "")
id, err := strconv.Atoi(strings.TrimSpace(sId))
if err != nil {
log.Fatalf("There was an error parsing the integer '%s'", sId)
}
numberParts := strings.Split(parts[1], " | ")
sWinningNumbers := strings.Fields(numberParts[0])
sNumbers := strings.Fields(numberParts[1])
stringSliceToInt := func(sSlice []string) []int {
integerSlice := make([]int, len(sSlice))
for i, sElement := range sSlice {
element, err := strconv.Atoi(sElement)
if err != nil {
log.Fatalf("There was an error parsing a number of the slice to integer '%s'", sElement)
}
integerSlice[i] = element
}
return integerSlice
}
return card.Card{
CardID: id,
Numbers: stringSliceToInt(sWinningNumbers),
WinningNumbers: stringSliceToInt(sNumbers),
}
}
func (d Day) GetInput(lines []string) interface{} {
cards := make([]card.Card, len(lines))
for i, line := range lines {
card := getLineCard(line)
cards[i] = card
}
return cards
}
func (d Day) SolvePart1(cardsI interface{}) int {
cards := cardsI.([]card.Card)
sum := 0
for _, card := range cards {
sum += card.GetWorth()
}
return sum
}
func (d Day) SolvePart2(cardsI interface{}) int {
cards := cardsI.([]card.Card)
wonCardAmounts := make([]int, len(cards))
sum := 0
for i := len(cards) - 1; i >= 0; i-- {
wonCards := cards[i].GetWonCards()
wonCardAmounts[i] = len(wonCards) // Save how many cards this card wins
for _, j := range wonCards {
wonCardAmounts[i] += wonCardAmounts[j-1] // Add how many cards each won card wins
}
sum += wonCardAmounts[i]
}
return sum + len(cards) // Also sum the original cards
}