-
Notifications
You must be signed in to change notification settings - Fork 0
/
giant_squid.go
316 lines (246 loc) · 6.58 KB
/
giant_squid.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package main
import (
"log"
"os"
"path/filepath"
"strconv"
"strings"
)
const BOARD_SIDE_LEN = 5
const pathInput = "input.txt"
const pathTestInput = "test.txt"
type splitMode int
const (
WHITESPACE splitMode = 0
COMMAS = 1
)
func logErr(e error) {
if e != nil {
log.Panicln(e)
}
}
func readFile(path string) (str string) {
fp, err := filepath.Abs(path)
logErr(err)
dat, err := os.ReadFile(fp)
logErr(err)
str = string(dat)
return str
}
type bingoBoardLine struct {
values []int
found int
}
type bingoBoard struct {
solved bool
lines []bingoBoardLine
}
func splitStringsToIntSlice(line string, mode splitMode) (bingoNums []int) {
var tempSpl []string
switch mode {
case WHITESPACE:
tempSpl = strings.Fields(line)
break
case COMMAS:
tempSpl = strings.Split(line, ",")
break
default:
log.Panicln("Unknown mode =>", mode)
}
for _, val := range tempSpl {
newNum, err := strconv.ParseInt(val, 10, 64)
logErr(err)
bingoNums = append(bingoNums, int(newNum))
}
return bingoNums
}
func parseBingoBoard(lines []string) (board bingoBoard) {
var rows [][]int
board.solved = false
for _, line := range lines {
newNums := splitStringsToIntSlice(line, WHITESPACE)
rows = append(rows, newNums)
}
for i := 0; i < len(rows); i++ {
newRowLine := bingoBoardLine{found: 0, values: rows[i]}
board.lines = append(board.lines, newRowLine)
}
for i := 0; i < len(rows); i++ {
colNums := make([]int, len(rows))
for j := 0; j < len(rows); j++ {
colNums[j] = rows[j][i]
}
newColLine := bingoBoardLine{found: 0, values: colNums}
board.lines = append(board.lines, newColLine)
}
if len(board.lines) != 10 {
log.Panicln("Not 10 lines in board =>", len(board.lines))
}
return board
}
func parseInput(spl []string) (bingoNums []int, boards []bingoBoard) {
var currentBoard []string
for i, line := range spl {
if i == 0 {
bingoNums = splitStringsToIntSlice(line, COMMAS)
} else if len(line) == 0 {
if len(currentBoard) != BOARD_SIDE_LEN && i != 1 {
log.Panicln("currentBoard has a len of =>", len(currentBoard))
} else if i != 1 {
newBoard := parseBingoBoard(currentBoard)
boards = append(boards, newBoard)
// reset current
currentBoard = make([]string, 0)
}
} else {
currentBoard = append(currentBoard, line)
}
}
// for last board
newBoard := parseBingoBoard(currentBoard)
boards = append(boards, newBoard)
return bingoNums, boards
}
func lineContainsNum(line []int, num int) bool {
for _, lineVal := range line {
if lineVal == num {
return true
}
}
return false
}
func getWinnerBoardIndex(bingoNums []int, boards []bingoBoard, debug bool) (boardIdx int, numIdx int) {
// for every drawn number ...
for i := 0; i < len(bingoNums); i++ {
num := bingoNums[i]
// ... check all boards ...
for j := 0; j < len(boards); j++ {
// ... and all lines in them
for k := 0; k < len(boards[j].lines); k++ {
line := boards[j].lines[k]
if lineContainsNum(line.values, num) {
// need to access over loop nums to not update copy
boards[j].lines[k].found = line.found + 1
if boards[j].lines[k].found == BOARD_SIDE_LEN {
if debug {
log.Println("Winner line in board", j, "=>", line.values)
}
return j, i
}
}
}
}
}
// debug output
for i, board := range boards {
log.Println("Lines for board", i)
for j, line := range board.lines {
log.Println(j, "found => ", line.found, " values => ", line.values)
}
}
log.Panicln("No winner found!")
return 0, 0
}
func calcResult(winnerBoard bingoBoard, drawnNums []int, debug bool) (res int) {
// only take rows as with cols values would be duplicated
relevantRows := winnerBoard.lines[:BOARD_SIDE_LEN]
if debug {
log.Println("drawnNumbers", drawnNums)
}
sumUnused := 0
for _, num := range drawnNums {
for i := 0; i < len(relevantRows); i++ {
for j := 0; j < len(relevantRows[i].values); j++ {
if relevantRows[i].values[j] == num {
relevantRows[i].values[j] = 0
}
}
}
}
for _, row := range relevantRows {
for _, val := range row.values {
sumUnused += val
}
}
lastDrawn := drawnNums[len(drawnNums)-1]
if debug {
log.Println("sumUnused =>", sumUnused)
log.Println("Last drawn =>", lastDrawn)
}
return sumUnused * lastDrawn
}
func part1(spl []string, debug bool) (res int, numBoards int) {
bingoNums, boards := parseInput(spl)
if debug {
log.Println("bingoNums =>", bingoNums)
log.Println("board count =>", len(boards))
}
boardIdx, numIdx := getWinnerBoardIndex(bingoNums, boards, debug)
res = calcResult(boards[boardIdx], bingoNums[:numIdx+1], debug)
return res, len(boards)
}
func getLoserBoardIndex(bingoNums []int, boards []bingoBoard, debug bool) (boardIdx int, numIdx int) {
winnerBoardsLen := 0
// for every drawn number ...
for i := 0; i < len(bingoNums); i++ {
num := bingoNums[i]
// ... check all boards ...
for j := 0; j < len(boards); j++ {
// ... and all lines in them
// skip already solved boards
if boards[j].solved == false {
for k := 0; k < len(boards[j].lines); k++ {
// check again because a board could be solved two times in the same drawing
if boards[j].solved == false {
line := boards[j].lines[k]
if lineContainsNum(line.values, num) {
// need to access over loop nums to not update copy
boards[j].lines[k].found = line.found + 1
if boards[j].lines[k].found == BOARD_SIDE_LEN {
winnerBoardsLen++
if debug {
log.Println("Winner line in board", j, "=>", line.values)
}
boards[j].solved = true
// if this is the last board
if winnerBoardsLen == len(boards) {
if debug {
log.Println("This was the last one!")
}
return j, i
}
}
}
}
}
}
}
}
// debug output
for i, board := range boards {
log.Println("Lines for board", i)
for j, line := range board.lines {
log.Println(j, "found => ", line.found, " values => ", line.values)
}
}
log.Panicln("No winner found!")
return 0, 0
}
func part2(spl []string, debug bool) (res int, numBoards int) {
bingoNums, boards := parseInput(spl)
if debug {
log.Println("bingoNums =>", bingoNums)
log.Println("board count =>", len(boards))
}
boardIdx, numIdx := getLoserBoardIndex(bingoNums, boards, debug)
res = calcResult(boards[boardIdx], bingoNums[:numIdx+1], debug)
return res, len(boards)
}
func main() {
str := readFile(pathInput)
splice := strings.Split(str, "\n")
prod1, _ := part1(splice, false)
log.Println("Part1 result =>", prod1)
prod2, _ := part2(splice, true)
log.Println("Part2 result =>", prod2)
}