Skip to content

Instantly share code, notes, and snippets.

@HirbodBehnam
Created April 13, 2020 14:00
Show Gist options
  • Save HirbodBehnam/f4a17647e31e0e79a0450e1c04e854ea to your computer and use it in GitHub Desktop.
Save HirbodBehnam/f4a17647e31e0e79a0450e1c04e854ea to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"fmt"
"github.com/yourbasic/graph"
"log"
"os"
"strconv"
"strings"
"time"
)
func main() {
start := time.Now()
gC := make([][]int64,0)
file, err := os.Open("m81.txt")
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
split := strings.Split(scanner.Text(),",")
c := make([]int64,len(split))
for k,v := range split{
c[k] ,_ = strconv.ParseInt(v,10,64)
}
gC = append(gC,c)
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
inRow := len(gC)
g := graph.New(inRow * inRow + 1)
for row := range gC{
if row == inRow - 1{ // last row; Nothing is downer
for col := range gC[row]{
g.AddCost(row * inRow + col,row * inRow + col + 1,gC[row][col]) // also note that the very left and bottom nod is connected to exit nod
}
}else{
for col := range gC[row]{
g.AddCost(row * inRow + col,row * inRow + col + inRow,gC[row][col]) // connects the nod to the nod below it
if col != inRow - 1{ // not last column that has nothing lefter
g.AddCost(row * inRow + col,row * inRow + col + 1,gC[row][col])
}
}
}
}
_,min := graph.ShortestPath(g,0,inRow * inRow)
fmt.Println("Passed: ",time.Since(start))
fmt.Println(min)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment