-
Notifications
You must be signed in to change notification settings - Fork 0
/
p82.go
62 lines (50 loc) · 990 Bytes
/
p82.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
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"time"
)
func main() {
start := time.Now()
f, _ := os.Open("./data/d82")
scanner := bufio.NewScanner(f)
scanner.Split(bufio.ScanWords)
const size = 80
var grid [size][size]int
var minRow [size]int
for i := 0; i < size; i++ {
for j := 0; j < size; j++ {
scanner.Scan()
text := scanner.Text()
grid[i][j], _ = strconv.Atoi(text)
}
minRow[i] = grid[i][size-1]
}
for j := size - 2; j > -1; j-- {
for i := 0; i < size; i++ {
minRow[i] += grid[i][j]
}
for i := 1; i < size; i++ {
if minRow[i] > minRow[i-1]+grid[i][j] {
minRow[i] = minRow[i-1] + grid[i][j]
}
}
for i := size - 2; i > -1; i-- {
if minRow[i] > minRow[i+1]+grid[i][j] {
minRow[i] = minRow[i+1] + grid[i][j]
}
}
}
min := minRow[0]
for i := 1; i < size; i++ {
if min > minRow[i] {
min = minRow[i]
}
}
fmt.Println(min)
f.Close()
elapsed := time.Since(start)
fmt.Println("Elasped:", elapsed)
}