-
Notifications
You must be signed in to change notification settings - Fork 0
/
04p1.go
86 lines (72 loc) · 2.07 KB
/
04p1.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
82
83
84
85
86
package main
import (
"fmt"
"strings"
"aoc2023/utils"
)
type scratchCard struct {
cardId int
matchingNumbers int
cardPoints int
cardCopies int
}
func D04P1() {
lines := utils.ReadLines("inputs/04.txt")
cards := []scratchCard{}
for _, line := range lines {
currentCard := parseScratchCard(line)
for i := 0; i < currentCard.matchingNumbers; i++ {
if currentCard.cardPoints == 0 {
currentCard.cardPoints = 1
continue
}
// Double the points if the number is a winning number
currentCard.cardPoints *= 2
}
cards = append(cards, currentCard)
}
// Sum the points of all the cards
totalPoints := 0
for _, card := range cards {
totalPoints += card.cardPoints
}
fmt.Printf("Total points: %d\n", totalPoints)
}
func parseScratchCard(line string) scratchCard {
card := scratchCard{}
fmt.Sscanf(line, "Card %d:", &card.cardId)
// Break the line into the winning numbers and the card numbers
cardNumbersString := strings.Split(line, ":")[1]
winningNumbersString := strings.Split(cardNumbersString, "|")[0]
actualNumbersString := strings.Split(cardNumbersString, "|")[1]
numberOfWinningNumbers := len(winningNumbersString) / 3
numberOfActualNumbers := len(actualNumbersString) / 3
winningNumbers := []int{}
actualNumbers := []int{}
// Parse the winning numbers
for i := 0; i < numberOfWinningNumbers; i++ {
winningNumber := 0
fmt.Sscanf(winningNumbersString[i*3+1:i*3+3], "%d", &winningNumber)
winningNumbers = append(winningNumbers, winningNumber)
}
// Repeat for the actual numbers
for i := 0; i < numberOfActualNumbers; i++ {
actualNumber := 0
fmt.Sscanf(actualNumbersString[i*3+1:i*3+3], "%d", &actualNumber)
actualNumbers = append(actualNumbers, actualNumber)
}
card.matchingNumbers = countWinningMatches(actualNumbers, winningNumbers)
card.cardCopies = 1
return card
}
func countWinningMatches(numbers []int, winningNumbers []int) int {
matches := 0
for _, actualNumber := range numbers {
for _, winningNumber := range winningNumbers {
if actualNumber == winningNumber {
matches++
}
}
}
return matches
}