-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
202 lines (158 loc) · 3.27 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main
import (
"bufio"
"fmt"
"io"
"os"
"path"
"strconv"
"strings"
)
func input() *os.File {
input, err := os.Open(path.Join("2021", "4", "input.txt"))
if err != nil {
panic(err)
}
return input
}
type board struct {
grid [][]*number
winningNumber string
}
func (b *board) Call(number string) bool {
if b.isWinner() {
panic("can't call number on an already won board")
}
b.mark(number)
won := b.isWinner()
if won {
b.winningNumber = number
}
return won
}
func (b *board) Score() int {
if b.winningNumber == "" {
panic("can't get score of a board that hasn't won")
}
winningNumberInt, err := strconv.Atoi(b.winningNumber)
if err != nil {
panic(err)
}
return winningNumberInt * b.sumUnmarked()
}
func (b *board) sumUnmarked() int {
score := 0
for _, row := range b.grid {
for _, number := range row {
if !number.marked {
intValue, err := strconv.Atoi(number.value)
if err != nil {
panic(err)
}
score += intValue
}
}
}
return score
}
func (b *board) mark(num string) {
for _, row := range b.grid {
for _, pos := range row {
if pos.value == num {
pos.marked = true
}
}
}
}
func (b board) isWinner() bool {
for i := 0; i < len(b.grid); i++ {
if areAllNumbersMarked(b.grid[i]) {
return true
}
var col []*number
for _, row := range b.grid {
col = append(col, row[i])
}
if areAllNumbersMarked(col) {
return true
}
}
return false
}
func areAllNumbersMarked(numbers []*number) bool {
for _, p := range numbers {
if !p.marked {
return false
}
}
return true
}
type number struct {
value string
marked bool
}
func parse(r io.Reader) ([]string, []*board) {
scanner := bufio.NewScanner(r)
var numbers []string
var boards []*board
var currentBoard *board
for scanner.Scan() {
row := scanner.Text()
if numbers == nil {
numbers = strings.Split(row, ",")
continue
}
if len(row) == 0 {
if currentBoard != nil {
boards = append(boards, currentBoard)
currentBoard = nil
}
continue
}
if currentBoard == nil {
currentBoard = &board{}
}
currentBoard.grid = append(currentBoard.grid, makeUnmarkedNumbers(strings.Split(row, " ")))
}
if scanner.Err() != nil {
panic(scanner.Err())
}
if currentBoard != nil {
boards = append(boards, currentBoard)
}
return numbers, boards
}
func makeUnmarkedNumbers(numbers []string) []*number {
var result []*number
for _, value := range numbers {
if value == "" {
continue
}
result = append(result, &number{
value: value,
marked: false,
})
}
return result
}
func solve(r io.Reader) {
numbers, boards := parse(r)
var lastWinner *board
for _, number := range numbers {
for i := 0; i < len(boards); i++ {
b := boards[i]
won := b.Call(number)
if won {
lastWinner = b
boards[i] = boards[len(boards)-1]
boards = boards[:len(boards)-1]
i--
}
}
}
fmt.Println(lastWinner.Score())
}
func main() {
solve(strings.NewReader("7,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1\n\n22 13 17 11 0\n 8 2 23 4 24\n21 9 14 16 7\n 6 10 3 18 5\n 1 12 20 15 19\n\n 3 15 0 2 22\n 9 18 13 17 5\n19 8 7 25 23\n20 11 10 24 4\n14 21 16 12 6\n\n14 21 17 24 4\n10 16 15 9 19\n18 8 23 26 20\n22 11 13 6 5\n 2 0 12 3 7"))
solve(input())
}