/
main.go
93 lines (72 loc) · 1.9 KB
/
main.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"log"
"os"
"strconv"
"strings"
"github.com/TomasCruz/projecteuler"
)
/*
Problem 83; Path Sum: Four Ways
NOTE: This problem is a significantly more challenging version of Problem 81.
In the 5 by 5 matrix below, the minimal path sum from the top left to the bottom right, by moving left, right, up, and down, is indicated with asterisk
and is equal to 2297.
*131 673 *234 *103 *18
*201 *96 *342 965 *150
630 803 746 *422 *111
537 699 497 *121 956
805 732 524 *37 *331
Find the minimal path sum from the top left to the bottom right by moving left, right, up, and down in
matrix.txt (right click and "Save Link/Target As..."), a 31K text file containing an 80 by 80 matrix.
*/
func main() {
var limit int
if len(os.Args) > 1 {
limit64, err := strconv.ParseInt(os.Args[1], 10, 64)
if err != nil {
log.Fatal("bad argument")
}
limit = int(limit64)
} else {
limit = 80
}
projecteuler.Timed(calc, limit)
}
func calc(args ...interface{}) (result string, err error) {
limit := args[0].(int)
var fileName string
if limit == 5 {
fileName = "example.txt"
} else {
fileName = "0083_matrix.txt"
}
var rowStrings []string
if rowStrings, err = projecteuler.FileToStrings(fileName); err != nil {
return
}
input := makeMatrix(limit)
for i := 0; i < limit; i++ {
readToRow(limit, rowStrings[i], input[i])
}
// min path matrix
minPath := newMinPathMatrix(limit, input)
minPath.buildMinPathMatrix()
result = strconv.FormatInt(minPath.m[limit-1][limit-1], 10)
return
}
func makeMatrix(limit int) [][]int64 {
m := make([][]int64, limit)
for i := 0; i < limit; i++ {
m[i] = make([]int64, limit)
}
return m
}
func readToRow(limit int, rowString string, row []int64) (err error) {
numStrings := strings.Split(rowString, ",")
for i := 0; i < limit; i++ {
if row[i], err = strconv.ParseInt(numStrings[i], 10, 64); err != nil {
return
}
}
return
}