forked from sw0x2A/project-euler-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0011.go
71 lines (62 loc) · 1.15 KB
/
0011.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
// https://projecteuler.net/problem=11
package main
import (
"io/ioutil"
"strconv"
"strings"
)
func p11(file string) int {
fileBuf, err := ioutil.ReadFile(file)
if err != nil {
panic(err)
}
fileStr := string(fileBuf)
lines := strings.SplitN(fileStr, "\n", 20)
g := make([][]int, 20)
var columns []string
for i, v := range lines {
g[i] = make([]int, 20)
columns = strings.SplitN(v, " ", 20)
for j := range g[i] {
g[i][j], _ = strconv.Atoi(columns[j])
}
}
l, t, i, j := 0, 0, 0, 0
// top-left to bottom-right
for i = 0; i <= 16; i++ {
for j = 0; j <= 16; j++ {
t = g[i][j] * g[i+1][j+1] * g[i+2][j+2] * g[i+3][j+3]
if l < t {
l = t
}
}
}
// top-right to bottom-left
for i = 0; i <= 16; i++ {
for j = 3; j < 20; j++ {
t = g[i][j] * g[i+1][j-1] * g[i+2][j-2] * g[i+3][j-3]
if l < t {
l = t
}
}
}
// left
for i = 0; i <= 16; i++ {
for j = 0; j < 20; j++ {
t = g[i][j] * g[i+1][j] * g[i+2][j] * g[i+3][j]
if l < t {
l = t
}
}
}
// down
for i = 0; i < 20; i++ {
for j = 0; j <= 16; j++ {
t = g[i][j] * g[i][j+1] * g[i][j+2] * g[i][j+3]
if l < t {
l = t
}
}
}
return l
}