-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
206 lines (179 loc) · 3.88 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
203
204
205
206
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
)
const MATRIX_SIZE = 5
const MARK = -1
type Orders []int
type Board [MATRIX_SIZE * MATRIX_SIZE]int
func toMatrixCoordinate(x int, y int) int {
return x*MATRIX_SIZE + y
}
func getValue(board Board) func(int, int) int {
return func(x int, y int) int {
return board[toMatrixCoordinate(x, y)]
}
}
func getRow(board Board) func(int) []int {
return func(y int) []int {
return board[y*MATRIX_SIZE : (y+1)*MATRIX_SIZE]
}
}
func getCol(board Board) func(int) []int {
return func(x int) []int {
res := []int{}
for i, v := range board {
if i%MATRIX_SIZE == x {
res = append(res, v)
}
}
return res
}
}
func setValue(board Board) func(int, int) func(int) Board {
return func(x int, y int) func(int) Board {
return func(newValue int) Board {
board[toMatrixCoordinate(x, y)] = newValue
return board
}
}
}
func getFile() *os.File {
if len(os.Args) <= 1 {
return os.Stdin
}
f, err := os.Open(os.Args[1])
if err != nil {
log.Fatal(err)
}
return f
}
func transform[T, U any](arr []T, fn func(T) U) []U {
var res []U
for _, v := range arr {
res = append(res, fn(v))
}
return res
}
func readOrders(s *bufio.Scanner) Orders {
return transform(strings.Split(s.Text(), ","), func(order string) int {
res, err := strconv.Atoi(order)
if err != nil {
log.Fatalf("Order '%s' cannot be converted to integer!", order)
}
return res
})
}
func readEmptyLine(s *bufio.Scanner) bool {
res := s.Scan()
if s.Text() != "" {
log.Fatalf("Expected empty line but got '%s'!", s.Text())
}
return res
}
func readBoard(s *bufio.Scanner) Board {
var board Board
for x := 0; x < MATRIX_SIZE; x++ {
row := strings.Fields(s.Text())
for y := 0; y < MATRIX_SIZE; y++ {
num, err := strconv.Atoi(row[y])
if err != nil {
log.Fatalf("Cannot convert '%s' to integer!", row[y])
}
board = setValue(board)(x, y)(num)
}
s.Scan()
}
return board
}
func readBoards(s *bufio.Scanner) []Board {
var boards []Board
for s.Scan() {
if s.Text() == "" {
break
}
boards = append(boards, readBoard(s))
}
return boards
}
func isBingoArr(arr []int) bool {
for _, val := range arr {
if val != MARK {
return false
}
}
return true
}
func isBingo(board Board) bool {
for i := 0; i < MATRIX_SIZE; i++ {
row := getRow(board)(i)
col := getCol(board)(i)
if isBingoArr(row) || isBingoArr(col) {
return true
}
}
return false
}
func mark(board Board, order int) Board {
for i, val := range board {
if val == order {
board[i] = MARK
}
}
return board
}
func playBingo(orders Orders, boards []Board) (int, Board, error) {
for _, order := range orders {
bingoBoards := []Board{}
noBingoBoards := []Board{}
for _, board := range boards {
newBoard := mark(board, order)
if isBingo(newBoard) {
bingoBoards = append(bingoBoards, newBoard)
} else {
noBingoBoards = append(noBingoBoards, newBoard)
}
}
if len(noBingoBoards) <= 0 && len(bingoBoards) == 1 {
return order, bingoBoards[0], nil
}
// Keep boards that are not bingo.
boards = noBingoBoards
}
return 0, Board{}, fmt.Errorf("no bingo found")
}
func sumUnmarked(board Board) int {
sum := 0
for _, val := range board {
if val != MARK {
sum += val
}
}
return sum
}
func score(order int, board Board) int {
sum := sumUnmarked(board)
return order * sum
}
func main() {
f := getFile()
defer f.Close()
s := bufio.NewScanner(f)
if err := s.Err(); err != nil {
log.Fatal(err)
}
s.Scan()
orders := readOrders(s)
readEmptyLine(s)
boards := readBoards(s)
order, board, err := playBingo(orders, boards)
if err != nil {
log.Fatal(err)
}
fmt.Println(score(order, board))
}