-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday4.go
57 lines (48 loc) · 1.35 KB
/
day4.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
package main
import (
"fmt"
"slices"
"strings"
"github.com/Alex-Waring/AoC/utils"
)
func main() {
defer utils.Timer("main")()
lines := utils.ReadInput("Input.txt")
card_points := []int{}
line_matches := map[int]int{}
// Part 1
for game_no, line := range lines {
round_points := 0
line = strings.Split(line, ": ")[1]
winning_numbers := utils.RemoveSliceSpaces(strings.Split(strings.Split(line, " | ")[0], " "))
card_numbers := utils.RemoveSliceSpaces(strings.Split(strings.Split(line, " | ")[1], " "))
count_wins := 0
for _, number := range card_numbers {
if slices.Contains(winning_numbers, number) {
count_wins++
if round_points == 0 {
round_points++
} else {
round_points = round_points * 2
}
}
}
// Storing the number of matches so we can use the result in part 2
line_matches[game_no+1] = count_wins
card_points = append(card_points, round_points)
}
fmt.Println(utils.Sum(card_points))
// Part 2, queue based but used the cached results from part 1
queue := utils.MakeRange(1, len(lines))
won_rounds := []int{}
for len(queue) > 0 {
processed_game := queue[0]
games_won := line_matches[processed_game]
for i := 1; i <= games_won; i++ {
queue = append(queue, (i + processed_game))
}
won_rounds = append(won_rounds, processed_game)
queue = queue[1:]
}
fmt.Println(len(won_rounds))
}