128 lines
2 KiB
Go
128 lines
2 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
const boardSize = 5
|
|
|
|
type Board [][]int
|
|
|
|
func (b Board) Mark(num int) {
|
|
for x := 0; x < boardSize; x++ {
|
|
for y := 0; y < boardSize; y++ {
|
|
if b[x][y] == num {
|
|
b[x][y] = -1
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func (b Board) CheckWin() bool {
|
|
for x := 0; x < boardSize; x++ {
|
|
match := true
|
|
|
|
for y := 0; y < boardSize; y++ {
|
|
if b[x][y] != -1 {
|
|
match = false
|
|
}
|
|
}
|
|
|
|
if match {
|
|
return true
|
|
}
|
|
}
|
|
|
|
for y := 0; y < boardSize; y++ {
|
|
match := true
|
|
|
|
for x := 0; x < boardSize; x++ {
|
|
if b[x][y] != -1 {
|
|
match = false
|
|
}
|
|
}
|
|
|
|
if match {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func (b Board) Sum() int {
|
|
sum := 0
|
|
|
|
for x := 0; x < boardSize; x++ {
|
|
for y := 0; y < boardSize; y++ {
|
|
if b[x][y] != -1 {
|
|
sum += b[x][y]
|
|
}
|
|
}
|
|
}
|
|
|
|
return sum
|
|
}
|
|
|
|
func NewBoard(lines []string) Board {
|
|
var board Board
|
|
|
|
for _, line := range lines {
|
|
var numbers []int
|
|
for i := 0; i < boardSize; i++ {
|
|
val, _ := strconv.Atoi(strings.TrimSpace(line[(i*3):(i*3)+2]))
|
|
numbers = append(numbers, val)
|
|
}
|
|
board = append(board, numbers)
|
|
}
|
|
|
|
return board
|
|
}
|
|
|
|
func main() {
|
|
var numbers []int
|
|
var boards []Board
|
|
var boardLines []string
|
|
|
|
s := bufio.NewScanner(os.Stdin)
|
|
for s.Scan() {
|
|
// first line should be the numbers
|
|
if numbers == nil {
|
|
for _, n := range strings.Split(s.Text(), ",") {
|
|
val, _ := strconv.Atoi(n)
|
|
numbers = append(numbers, val)
|
|
}
|
|
s.Scan()
|
|
continue
|
|
}
|
|
|
|
// if the line is empty, create new board
|
|
// else, collect more lines
|
|
if len(s.Bytes()) == 0 {
|
|
boards = append(boards, NewBoard(boardLines))
|
|
boardLines = []string{}
|
|
} else {
|
|
boardLines = append(boardLines, s.Text())
|
|
}
|
|
}
|
|
// if we have any lines left, convert them into board
|
|
if len(boardLines) > 0 {
|
|
boards = append(boards, NewBoard(boardLines))
|
|
}
|
|
|
|
for _, num := range numbers {
|
|
for _, board := range boards {
|
|
board.Mark(num)
|
|
if board.CheckWin() {
|
|
fmt.Println(board.Sum() * num)
|
|
os.Exit(0)
|
|
}
|
|
}
|
|
}
|
|
|
|
fmt.Println(":(")
|
|
}
|