Initial Commit
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/bin/**/*
|
||||
8
City.go
Normal file
8
City.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package main
|
||||
|
||||
type City struct {
|
||||
Name string
|
||||
Area int
|
||||
Population int
|
||||
Mayor Person
|
||||
}
|
||||
4
Makefile
Normal file
4
Makefile
Normal file
@@ -0,0 +1,4 @@
|
||||
build:
|
||||
go build -o bin/build/pathFinder *.go
|
||||
run:
|
||||
go run *.go
|
||||
8
Person.go
Normal file
8
Person.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package main
|
||||
|
||||
type Person struct {
|
||||
FirstName string
|
||||
LastName string
|
||||
Age int
|
||||
JobTitle string
|
||||
}
|
||||
18
README.md
Normal file
18
README.md
Normal file
@@ -0,0 +1,18 @@
|
||||
# Pathfinding
|
||||
|
||||
<img src="https://www.codingame.com/servlet/fileservlet?id=7766456941361" width="400"></img>
|
||||
|
||||
|
||||
## Which algorithms ?
|
||||
Dijkstra & A*
|
||||
|
||||
|
||||
## What type of graph I am using ?
|
||||
Directed graph
|
||||
|
||||
|
||||
## Why use Golang?
|
||||
<img src="https://miro.medium.com/max/920/1*CdjOgfolLt_GNJYBzI-1QQ.jpeg" width="200" >
|
||||
|
||||
I have wanted to play around with golang for a while but didn't have a good project for it. My hopes are to play with the ___ side by running the algorithm in ___
|
||||
|
||||
23
directedGraph.go
Normal file
23
directedGraph.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
const SIZE_GRAPH int = 10
|
||||
|
||||
type DirectedGraph struct {
|
||||
count int
|
||||
nodes [SIZE_GRAPH]Node
|
||||
Head *Node
|
||||
}
|
||||
|
||||
func (self *DirectedGraph) AddNode(node *Node) {
|
||||
|
||||
if self.count > SIZE_GRAPH {
|
||||
panic("Graph has gotten too big")
|
||||
}
|
||||
|
||||
if self.Head == nil && self.count == 0 {
|
||||
self.Head = node
|
||||
}
|
||||
|
||||
self.nodes[self.count] = *node
|
||||
self.count++
|
||||
}
|
||||
9
edge.go
Normal file
9
edge.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package main
|
||||
|
||||
type Edge struct {
|
||||
To *Node
|
||||
}
|
||||
|
||||
func (self *Edge) Cost() int {
|
||||
return (self.To.Data.Population / 100)
|
||||
}
|
||||
48
main.go
Normal file
48
main.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
MyGraph := DirectedGraph{}
|
||||
|
||||
// Instantiate Nodes
|
||||
FirstNode := Node{Data: City{Name: "Amsterdam", Population: 821_752}}
|
||||
SecondNode := Node{Data: City{Name: "Dublin", Population: 544_107}}
|
||||
thirdNode := Node{Data: City{Name: "Berlin", Population: 3_645_000}}
|
||||
fourthNode := Node{Data: City{Name: "Paris", Population: 2_161_000}}
|
||||
|
||||
// Setup relations
|
||||
FirstNode.Edges[0] = Edge{To: &thirdNode}
|
||||
SecondNode.Edges[0] = Edge{To: &thirdNode}
|
||||
FirstNode.Edges[1] = Edge{To: &SecondNode}
|
||||
thirdNode.Edges[0] = Edge{To: &fourthNode}
|
||||
|
||||
// Add Nodes to graph
|
||||
MyGraph.AddNode(&FirstNode)
|
||||
MyGraph.AddNode(&SecondNode)
|
||||
MyGraph.AddNode(&thirdNode)
|
||||
MyGraph.AddNode(&fourthNode)
|
||||
|
||||
fmt.Println("Whats inside the graph")
|
||||
for index, node := range MyGraph.nodes {
|
||||
if (node == Node{}) {
|
||||
continue
|
||||
}
|
||||
fmt.Printf("%d %s\t(Pops: %d)\n", index, node.Data.Name, node.Data.Population)
|
||||
for _, edge := range node.Edges {
|
||||
if (edge == Edge{}) {
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Printf(
|
||||
"\t- Has a connection to %s\t(Travel Cost: %d)\n",
|
||||
edge.To.Data.Name,
|
||||
edge.Cost())
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user