-
Notifications
You must be signed in to change notification settings - Fork 0
/
p11.go
77 lines (67 loc) · 1.12 KB
/
p11.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
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"time"
)
func main() {
start := time.Now()
f, _ := os.Open("./data/d11")
scanner := bufio.NewScanner(f)
scanner.Split(bufio.ScanWords)
max := 0
var grid [20][20]int
for i := 0; i < 20; i++ {
for j := 0; j < 20; j++ {
scanner.Scan()
text := scanner.Text()
grid[i][j], _ = strconv.Atoi(text)
}
}
for i := 0; i < 16; i++ {
for j := 0; j < 16; j++ {
// vertical
curr := grid[i][j]
for k := 1; k < 4; k++ {
curr *= grid[i][j+k]
}
if curr > max {
max = curr
}
// diagonal \
curr = grid[i][j]
for k := 1; k < 4; k++ {
curr *= grid[i+k][j+k]
}
if curr > max {
max = curr
}
// horizontal
curr = grid[i][j]
for k := 1; k < 4; k++ {
curr *= grid[i+k][j]
}
if curr > max {
max = curr
}
}
}
for i := 3; i < 20; i++ {
for j := 0; j < 16; j++ {
// diagonal /
curr := grid[i][j]
for k := 1; k < 4; k++ {
curr *= grid[i-k][j+k]
}
if curr > max {
max = curr
}
}
}
fmt.Println(max)
f.Close()
elapsed := time.Since(start)
fmt.Println("Elasped:", elapsed)
}