-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathb.go
61 lines (48 loc) · 1.17 KB
/
b.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
package day04
import (
"strconv"
"strings"
)
type PuzzleB struct{}
func (p PuzzleB) String() string {
return "04b"
}
func (p PuzzleB) Run() int {
index := strings.Index(input, "\n\n")
// Parse the input, splitting the numbers from the boards
nums := strings.Split(input[:index], ",")
brds := strings.Split(input[index+2:], "\n\n")
// Parse boards from input and convert into required structure
boards := make([]*Board, len(brds))
for i, b := range brds {
boards[i] = NewBoard(b)
}
// An the grids are square. It isn't possible to win without until after 'n'
// numbers are drawn
for i := 0; i < BoardDim; i++ {
for _, b := range boards {
b.Mark(nums[i])
}
}
// Continue marking, but check for a winning board
var winNum int
var winner *Board
nsize := len(nums)
for i := BoardDim; i < nsize; i++ {
for n := 0; n < len(boards); n++ {
board := boards[n]
pos := board.Mark(nums[i])
if board.Check(pos) {
winNum, _ = strconv.Atoi(nums[i])
winner = board
// Remove from the board from the slice
boards = append(boards[:n], boards[n+1:]...)
n--
}
}
if len(boards) == 0 {
break
}
}
return winNum * winner.SumUnmarked()
}