-
Notifications
You must be signed in to change notification settings - Fork 0
/
15.go
63 lines (60 loc) · 1.25 KB
/
15.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
package main
import (
"bufio"
"fmt"
"math"
"os"
)
func path(size int, risk func(x int, y int) int) int {
var field [][]int
field = make([][]int, size+2)
for i := 0; i < len(field); i++ {
field[i] = make([]int, size+2)
for j := 0; j < len(field[i]); j++ {
field[i][j] = math.MaxInt / 2
}
}
field[1][1] = 0
adjust := func(x int, y int, dx int, dy int) bool {
_risk := risk(x-1, y-1)
if field[y+dy][x+dx] != -1 && field[y+dy][x+dx]+_risk < field[y][x] {
field[y][x] = field[y+dy][x+dx] + _risk
return true
}
return false
}
found:
for y := 1; y <= size; y++ {
for x := 1; x <= size; x++ {
if adjust(x, y, -1, 0) {
goto found
}
if adjust(x, y, +1, 0) {
goto found
}
if adjust(x, y, 0, -1) {
goto found
}
if adjust(x, y, 0, +1) {
goto found
}
}
}
return field[size][size]
}
func main() {
scanner := bufio.NewScanner(os.Stdin)
risks := make([]string, 0, 100)
for scanner.Scan() {
risks = append(risks, scanner.Text())
}
size := len(risks)
fmt.Println(path(size, func(x int, y int) int { return int(risks[y][x]) - int('0') }))
fmt.Println(path(size*5, func(x int, y int) int {
r := int(risks[y%size][x%size]) - int('0') + x/size + y/size
if r > 9 {
return r - 9
}
return r
}))
}