-
Notifications
You must be signed in to change notification settings - Fork 0
/
11.go
75 lines (67 loc) · 1.35 KB
/
11.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
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
func main() {
f, err := os.Open("11.txt")
if err != nil {
return
}
var data [20][20]int
reader := bufio.NewReader(f)
for i := 0; i < 20; i++ {
line, err := reader.ReadString('\n')
if err != nil {
break
}
line = strings.TrimSpace(line)
pieces := strings.Split(line, " ")
for key, value := range pieces {
data[i][key], _ = strconv.Atoi(value)
}
}
// Store largest product.
var max int
// Test horizontal.
for i := 0; i < 20; i++ {
for j := 0; j < 17; j++ {
product := data[i][j] * data[i][j+1] * data[i][j+2] * data[i][j+3]
if product > max {
max = product
}
}
}
// Test vertical.
for i := 0; i < 20; i++ {
for j := 0; j < 17; j++ {
product := data[j][i] * data[j+1][i] * data[j+2][i] * data[j+3][i]
if product > max {
max = product
}
}
}
// Test upper left to lower right diagonal.
for i := 0; i < 17; i++ {
for j := 0; j < 17; j++ {
product := data[i][j] * data[i+1][j+1] * data[i+2][j+2] * data[i+3][j+3]
if product > max {
max = product
}
}
}
// Test upper right to lower left diagonal.
for i := 0; i < 17; i++ {
for j := 3; j < 20; j++ {
product := data[i][j] * data[i+1][j-1] * data[i+2][j-2] * data[i+3][j-3]
if product > max {
max = product
}
}
}
// Print maximum.
fmt.Println(max)
}