-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
136 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
Faerun to Norrath = 129 | ||
Faerun to Tristram = 58 | ||
Faerun to AlphaCentauri = 13 | ||
Faerun to Arbre = 24 | ||
Faerun to Snowdin = 60 | ||
Faerun to Tambi = 71 | ||
Faerun to Straylight = 67 | ||
Norrath to Tristram = 142 | ||
Norrath to AlphaCentauri = 15 | ||
Norrath to Arbre = 135 | ||
Norrath to Snowdin = 75 | ||
Norrath to Tambi = 82 | ||
Norrath to Straylight = 54 | ||
Tristram to AlphaCentauri = 118 | ||
Tristram to Arbre = 122 | ||
Tristram to Snowdin = 103 | ||
Tristram to Tambi = 49 | ||
Tristram to Straylight = 97 | ||
AlphaCentauri to Arbre = 116 | ||
AlphaCentauri to Snowdin = 12 | ||
AlphaCentauri to Tambi = 18 | ||
AlphaCentauri to Straylight = 91 | ||
Arbre to Snowdin = 129 | ||
Arbre to Tambi = 53 | ||
Arbre to Straylight = 40 | ||
Snowdin to Tambi = 15 | ||
Snowdin to Straylight = 99 | ||
Tambi to Straylight = 70 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package main | ||
|
||
import ( | ||
"aoc2015/util" | ||
"regexp" | ||
"slices" | ||
|
||
mapset "github.com/deckarep/golang-set/v2" | ||
"github.com/dominikbraun/graph" | ||
) | ||
|
||
type Route struct { | ||
From, To string | ||
Distance int | ||
} | ||
|
||
func getUniqueDestinations(data []Route) []string { | ||
uniqueDestinations := mapset.NewSet[string]() | ||
|
||
for _, route := range data { | ||
uniqueDestinations.Add(route.From) | ||
uniqueDestinations.Add(route.To) | ||
} | ||
|
||
return uniqueDestinations.ToSlice() | ||
} | ||
|
||
func calculateDistances(data []Route) []int { | ||
destinations := getUniqueDestinations(data) | ||
g := graph.New(graph.StringHash, graph.Weighted()) | ||
|
||
for _, destination := range destinations { | ||
g.AddVertex(destination) | ||
} | ||
|
||
for _, route := range data { | ||
g.AddEdge(route.From, route.To, graph.EdgeWeight(route.Distance)) | ||
} | ||
|
||
spanningPaths := [][]string{} | ||
|
||
for _, departure := range destinations { | ||
for _, destination := range destinations { | ||
if departure == destination { | ||
continue | ||
} | ||
|
||
paths, _ := graph.AllPathsBetween(g, departure, destination) | ||
|
||
for _, path := range paths { | ||
if len(path) == len(destinations) { | ||
spanningPaths = append(spanningPaths, path) | ||
} | ||
} | ||
} | ||
} | ||
|
||
return util.ArrayMap(spanningPaths, func(spanningPath []string) int { | ||
distance := 0 | ||
for i := 0; i < len(spanningPath)-1; i++ { | ||
from := spanningPath[i] | ||
to := spanningPath[i+1] | ||
edge, _ := g.Edge(from, to) | ||
|
||
distance += edge.Properties.Weight | ||
} | ||
|
||
return distance | ||
}) | ||
} | ||
|
||
func part1(data []Route) int { | ||
distances := calculateDistances(data) | ||
|
||
return slices.Min(distances) | ||
} | ||
|
||
func part2(data []Route) int { | ||
distances := calculateDistances(data) | ||
|
||
return slices.Max(distances) | ||
} | ||
|
||
func main() { | ||
lineRegexp := regexp.MustCompile(`(\w+) to (\w+) = (\d+)`) | ||
reader := func(name string) []Route { | ||
return util.ArrayMap(util.ReadLines(name), func(line string) Route { | ||
match := lineRegexp.FindStringSubmatch(line) | ||
|
||
return Route{ | ||
From: match[1], | ||
To: match[2], | ||
Distance: util.StringToInt(match[3]), | ||
} | ||
}) | ||
} | ||
|
||
util.TestRuns("09", reader, part1, part2) | ||
util.SolutionRuns("09", reader, part1, part2) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
London to Dublin = 464 | ||
London to Belfast = 518 | ||
Dublin to Belfast = 141 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters