Combine AoC Repos
This commit is contained in:
BIN
2015/day09/day9
Normal file
BIN
2015/day09/day9
Normal file
Binary file not shown.
28
2015/day09/input
Normal file
28
2015/day09/input
Normal file
@@ -0,0 +1,28 @@
|
||||
Tristram to AlphaCentauri = 34
|
||||
Tristram to Snowdin = 100
|
||||
Tristram to Tambi = 63
|
||||
Tristram to Faerun = 108
|
||||
Tristram to Norrath = 111
|
||||
Tristram to Straylight = 89
|
||||
Tristram to Arbre = 132
|
||||
AlphaCentauri to Snowdin = 4
|
||||
AlphaCentauri to Tambi = 79
|
||||
AlphaCentauri to Faerun = 44
|
||||
AlphaCentauri to Norrath = 147
|
||||
AlphaCentauri to Straylight = 133
|
||||
AlphaCentauri to Arbre = 74
|
||||
Snowdin to Tambi = 105
|
||||
Snowdin to Faerun = 95
|
||||
Snowdin to Norrath = 48
|
||||
Snowdin to Straylight = 88
|
||||
Snowdin to Arbre = 7
|
||||
Tambi to Faerun = 68
|
||||
Tambi to Norrath = 134
|
||||
Tambi to Straylight = 107
|
||||
Tambi to Arbre = 40
|
||||
Faerun to Norrath = 11
|
||||
Faerun to Straylight = 66
|
||||
Faerun to Arbre = 144
|
||||
Norrath to Straylight = 115
|
||||
Norrath to Arbre = 135
|
||||
Straylight to Arbre = 127
|
166
2015/day09/main.go
Normal file
166
2015/day09/main.go
Normal file
@@ -0,0 +1,166 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var allTowns map[string]roads
|
||||
|
||||
type roads map[string]int
|
||||
|
||||
func main() {
|
||||
allTowns = make(map[string]roads)
|
||||
var input []string
|
||||
var readInp, bldInput string
|
||||
for {
|
||||
_, err := fmt.Scan(&readInp)
|
||||
if err != nil {
|
||||
if err != io.EOF {
|
||||
log.Fatal(err)
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
if strings.HasSuffix(bldInput, "=") {
|
||||
bldInput = bldInput + " " + readInp
|
||||
input = append(input, bldInput)
|
||||
bldInput = ""
|
||||
continue
|
||||
}
|
||||
if len(bldInput) > 0 {
|
||||
bldInput += " "
|
||||
}
|
||||
bldInput = bldInput + readInp
|
||||
}
|
||||
|
||||
for i := range input {
|
||||
dirTowns := strings.Split(input[i], " to ")
|
||||
secondPart := strings.Split(dirTowns[1], " = ")
|
||||
town1 := dirTowns[0]
|
||||
town2 := secondPart[0]
|
||||
distance := mustAtoi(secondPart[1])
|
||||
buildRoute(town1, town2, distance)
|
||||
}
|
||||
|
||||
// All routes should be built, find the shortest
|
||||
var shortestRoute []string
|
||||
shortestDistance := -1
|
||||
for i := range allTowns {
|
||||
var visited []string
|
||||
lastVisit := i
|
||||
tripDistance := 0
|
||||
for len(visited) < len(allTowns) {
|
||||
var dist int
|
||||
visited = append(visited, lastVisit)
|
||||
lastVisit, dist = findShortest(lastVisit, visited)
|
||||
if dist > 0 {
|
||||
tripDistance += dist
|
||||
}
|
||||
}
|
||||
if shortestDistance == -1 || tripDistance < shortestDistance {
|
||||
shortestDistance = tripDistance
|
||||
shortestRoute = visited
|
||||
}
|
||||
}
|
||||
|
||||
// Now find the longest
|
||||
var longestRoute []string
|
||||
longestDistance := -1
|
||||
for i := range allTowns {
|
||||
var visited []string
|
||||
lastVisit := i
|
||||
tripDistance := 0
|
||||
for len(visited) < len(allTowns) {
|
||||
var dist int
|
||||
visited = append(visited, lastVisit)
|
||||
lastVisit, dist = findLongest(lastVisit, visited)
|
||||
if dist > 0 {
|
||||
tripDistance += dist
|
||||
}
|
||||
}
|
||||
if longestDistance == -1 || tripDistance > longestDistance {
|
||||
longestDistance = tripDistance
|
||||
longestRoute = visited
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("Shortest Route:" + strconv.Itoa(shortestDistance))
|
||||
fmt.Println(strings.Join(shortestRoute, "->"))
|
||||
fmt.Println("Longest Route:" + strconv.Itoa(longestDistance))
|
||||
fmt.Println(strings.Join(longestRoute, "->"))
|
||||
}
|
||||
|
||||
func findShortest(from string, exclude []string) (string, int) {
|
||||
var toTown string
|
||||
shortest := -1
|
||||
for j := range allTowns[from] {
|
||||
var didIt bool
|
||||
for ex := range exclude {
|
||||
if j == exclude[ex] {
|
||||
didIt = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if didIt {
|
||||
continue
|
||||
}
|
||||
// Find the quickest trip from here
|
||||
if shortest == -1 || allTowns[from][j] < shortest {
|
||||
shortest = allTowns[from][j]
|
||||
toTown = j
|
||||
}
|
||||
}
|
||||
return toTown, shortest
|
||||
}
|
||||
|
||||
func findLongest(from string, exclude []string) (string, int) {
|
||||
var toTown string
|
||||
longest := -1
|
||||
for j := range allTowns[from] {
|
||||
var didIt bool
|
||||
for ex := range exclude {
|
||||
if j == exclude[ex] {
|
||||
didIt = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if didIt {
|
||||
continue
|
||||
}
|
||||
// Find the longest trip from here
|
||||
if longest == -1 || allTowns[from][j] > longest {
|
||||
longest = allTowns[from][j]
|
||||
toTown = j
|
||||
}
|
||||
}
|
||||
return toTown, longest
|
||||
}
|
||||
|
||||
func mustAtoi(i string) int {
|
||||
var ret int
|
||||
var err error
|
||||
if ret, err = strconv.Atoi(i); err != nil {
|
||||
fmt.Println("Tried to mustAtoi: " + i)
|
||||
os.Exit(1)
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func buildRoute(t1, t2 string, dist int) {
|
||||
if _, ok := allTowns[t1]; !ok {
|
||||
// Add this town
|
||||
allTowns[t1] = make(map[string]int)
|
||||
}
|
||||
allTowns[t1][t2] = dist
|
||||
// And the other direction
|
||||
if _, ok := allTowns[t2]; !ok {
|
||||
// Add this town
|
||||
allTowns[t2] = make(map[string]int)
|
||||
}
|
||||
allTowns[t2][t1] = dist
|
||||
}
|
78
2015/day09/problem
Normal file
78
2015/day09/problem
Normal file
@@ -0,0 +1,78 @@
|
||||
Advent of Code
|
||||
|
||||
br0xen 40*
|
||||
|
||||
• [About]
|
||||
• [Stats]
|
||||
• [Leaderboard]
|
||||
• [Settings]
|
||||
• [Log out]
|
||||
|
||||
--- Day 9: All in a Single Night ---
|
||||
|
||||
Every year, Santa manages to deliver all of his presents in a single night.
|
||||
|
||||
This year, however, he has some new locations to visit; his elves have provided him the
|
||||
distances between every pair of locations. He can start and end at any two (different)
|
||||
locations he wants, but he must visit each location exactly once. What is the shortest distance
|
||||
he can travel to achieve this?
|
||||
|
||||
For example, given the following distances:
|
||||
|
||||
London to Dublin = 464
|
||||
London to Belfast = 518
|
||||
Dublin to Belfast = 141
|
||||
|
||||
The possible routes are therefore:
|
||||
|
||||
Dublin -> London -> Belfast = 982
|
||||
London -> Dublin -> Belfast = 605
|
||||
London -> Belfast -> Dublin = 659
|
||||
Dublin -> Belfast -> London = 659
|
||||
Belfast -> Dublin -> London = 605
|
||||
Belfast -> London -> Dublin = 982
|
||||
|
||||
The shortest of these is London -> Dublin -> Belfast = 605, and so the answer is 605 in this
|
||||
example.
|
||||
|
||||
What is the distance of the shortest route?
|
||||
|
||||
Your puzzle answer was 251.
|
||||
|
||||
--- Part Two ---
|
||||
|
||||
The next year, just to show off, Santa decides to take the route with the longest distance
|
||||
instead.
|
||||
|
||||
He can still start and end at any two (different) locations he wants, and he still must visit
|
||||
each location exactly once.
|
||||
|
||||
For example, given the distances above, the longest route would be 982 via (for example) Dublin
|
||||
-> London -> Belfast.
|
||||
|
||||
What is the distance of the longest route?
|
||||
|
||||
Your puzzle answer was 898.
|
||||
|
||||
Both parts of this puzzle are complete! They provide two gold stars: **
|
||||
|
||||
At this point, you should return to your advent calendar and try another puzzle.
|
||||
|
||||
If you still want to see it, you can get your puzzle input.
|
||||
|
||||
You can also [Shareon Twitter Google+ Reddit] this puzzle.
|
||||
|
||||
References
|
||||
|
||||
Visible links
|
||||
. http://adventofcode.com/
|
||||
. http://adventofcode.com/about
|
||||
. http://adventofcode.com/stats
|
||||
. http://adventofcode.com/leaderboard
|
||||
. http://adventofcode.com/settings
|
||||
. http://adventofcode.com/auth/logout
|
||||
. http://adventofcode.com/
|
||||
. http://adventofcode.com/day/9/input
|
||||
. https://twitter.com/intent/tweet?text=I%27ve+completed+%22All+in+a+Single+Night%22+%2D+Day+9+%2D+Advent+of+Code&url=http%3A%2F%2Fadventofcode%2Ecom%2Fday%2F9&related=ericwastl&hashtags=AdventOfCode
|
||||
. https://plus.google.com/share?url=http%3A%2F%2Fadventofcode%2Ecom%2Fday%2F9
|
||||
. http://www.reddit.com/submit?url=http%3A%2F%2Fadventofcode%2Ecom%2Fday%2F9&title=I%27ve+completed+%22All+in+a+Single+Night%22+%2D+Day+9+%2D+Advent+of+Code
|
Reference in New Issue
Block a user