-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4.go
115 lines (105 loc) · 1.98 KB
/
4.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
package main
import (
"bufio"
"fmt"
"os"
"regexp"
"strconv"
)
type board struct {
size int
numbers []int
}
var re = regexp.MustCompile("\\d+")
const MARK = -1
func read(scanner *bufio.Scanner) board {
scanner.Scan()
first := re.FindAllString(scanner.Text(), -1)
result := board{size: len(first), numbers: make([]int, len(first)*len(first))}
for c, s := range first {
n, _ := strconv.ParseInt(s, 0, 0)
result.numbers[c] = int(n)
}
for r := 1; r < result.size; r++ {
scanner.Scan()
for c, s := range re.FindAllString(scanner.Text(), -1) {
n, _ := strconv.ParseInt(s, 0, 0)
result.numbers[r*result.size+c] = int(n)
}
}
return result
}
func (b board) String() string {
return fmt.Sprintf("%v: %v", b.size, b.numbers)
}
func (b board) mark(n int) {
for i, e := range b.numbers {
if e == n {
b.numbers[i] = MARK
}
}
}
func (b board) score(n int) int {
sum := 0
for _, el := range b.numbers {
if el != MARK {
sum += el
}
}
return sum * n
}
func (b board) win() bool {
for i := 0; i < b.size; i++ {
marked := true
for r := 0; (r < b.size) && marked; r++ {
if b.numbers[r*b.size+i] != MARK {
marked = false
}
}
if marked {
return true
}
marked = true
for c := 0; (c < b.size) && marked; c++ {
if b.numbers[i*b.size+c] != MARK {
marked = false
}
}
if marked {
return true
}
}
return false
}
func main() {
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
numbers := make([]int, 0, 100)
for _, s := range re.FindAllString(scanner.Text(), -1) {
n, _ := strconv.ParseInt(s, 0, 0)
numbers = append(numbers, int(n))
}
boards := make([]board, 0, 100)
for scanner.Scan() {
boards = append(boards, read(scanner))
}
var first, last, wins int
for _, n := range numbers {
for _, b := range boards {
if b.win() {
continue
}
b.mark(n)
if b.win() {
wins++
if wins == 1 {
first = b.score(n)
} else {
last = b.score(n)
}
}
}
}
fmt.Println(first)
fmt.Println(last)
}