Skip to content

Commit

Permalink
Day 09
Browse files Browse the repository at this point in the history
  • Loading branch information
Magiczne committed Nov 6, 2024
1 parent 2b37203 commit cca9003
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 5 deletions.
2 changes: 1 addition & 1 deletion d05/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ ItemLoop:

func part2(data []string) int {
niceStringsCount := 0
alphabetCombinations := util.ArrayMap(util.Combinations(util.AlphabetArray[:]), func(combination []string) string {
alphabetCombinations := util.ArrayMap(util.Doubles(util.AlphabetArray[:]), func(combination []string) string {
return fmt.Sprintf("%s%s", combination[0], combination[1])
})

Expand Down
28 changes: 28 additions & 0 deletions d09/input.txt
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
100 changes: 100 additions & 0 deletions d09/main.go
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)
}
3 changes: 3 additions & 0 deletions d09/test-runs/0.txt
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
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ go 1.23.2

require (
github.com/deckarep/golang-set/v2 v2.6.0
github.com/dominikbraun/graph v0.23.0
github.com/fatih/color v1.18.0
github.com/mxschmitt/golang-combinations v1.2.0
golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c
)

require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mxschmitt/golang-combinations v1.2.0 // indirect
golang.org/x/sys v0.25.0 // indirect
gonum.org/v1/gonum v0.15.1 // indirect
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/deckarep/golang-set/v2 v2.6.0 h1:XfcQbWM1LlMB8BsJ8N9vW5ehnnPVIw0je80NsVHagjM=
github.com/deckarep/golang-set/v2 v2.6.0/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpOF4IN+y82NBOac4=
github.com/dominikbraun/graph v0.23.0 h1:TdZB4pPqCLFxYhdyMFb1TBdFxp8XLcJfTTBQucVPgCo=
github.com/dominikbraun/graph v0.23.0/go.mod h1:yOjYyogZLY1LSG9E33JWZJiq5k83Qy2C6POAuiViluc=
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
Expand All @@ -15,5 +17,3 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gonum.org/v1/gonum v0.15.1 h1:FNy7N6OUZVUaWG9pTiD+jlhdQ3lMP+/LcTpJ6+a8sQ0=
gonum.org/v1/gonum v0.15.1/go.mod h1:eZTZuRFrzu5pcyjN5wJhcIhnUdNijYxX1T2IcrOGY0o=

0 comments on commit cca9003

Please sign in to comment.