-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
104 lines (78 loc) · 1.67 KB
/
main.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"fmt"
"io"
"os"
"regexp"
"strconv"
"strings"
)
type Game struct {
id int
targetNumbers map[int]int
totalPoints int
}
func main() {
input := input()
games := createGames(input)
fmt.Println("part 1 =", partOne(games))
fmt.Println("part 2 =", partTwo(games))
}
func partOne(games []Game) int {
winningPoints := 0
for _, game := range games {
if game.totalPoints == 0 {
continue
}
winningPoints += 1 << (game.totalPoints - 1)
}
return winningPoints
}
var partTwoTotal = 0 // thanks I hate this :)
func partTwo(games []Game) int {
recurser(games)
return partTwoTotal
}
func recurser(games []Game) {
if len(games) == 0 {
return
}
for i, game := range games {
partTwoTotal++
recurser(games[i+1 : i+game.totalPoints+1])
}
}
func createGames(input []string) []Game {
games := []Game{}
for gameNumber, line := range input {
game := Game{id: gameNumber + 1, targetNumbers: map[int]int{}}
line = strings.Split(line, ":")[1]
gameLine := strings.Split(line, "|")
re := regexp.MustCompile(`\d+`)
targetNumbers := re.FindAllString(gameLine[0], -1)
winningNumbers := re.FindAllString(gameLine[1], -1)
for _, number := range targetNumbers {
num, _ := strconv.Atoi(number)
game.targetNumbers[num]++
}
for _, number := range winningNumbers {
num, _ := strconv.Atoi(number)
if _, ok := game.targetNumbers[num]; ok {
game.totalPoints++
}
}
games = append(games, game)
}
return games
}
func input() []string {
file, err := os.Open("input.txt")
if err != nil {
panic(err)
}
stream, err := io.ReadAll(file)
if err != nil {
panic(err)
}
return strings.Split(string(stream), "\n")
}