-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path09.go
153 lines (132 loc) · 2.85 KB
/
09.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"bufio"
"fmt"
"log"
"os"
"regexp"
"sort"
"strconv"
)
// Keys returns the keys of a map
func Keys(x map[string]int) []string {
var keys []string
for key := range x {
keys = append(keys, key)
}
sort.Strings(keys)
return keys
}
// Unique dedupliates a list
func Unique(x []string) []string {
m := make(map[string]int)
for _, v := range x {
if _, ok := m[v]; !ok {
m[v] = 0
}
}
return Keys(m)
}
// Contains checks if string y exists in []string x
func Contains(x []string, y string) bool {
for _, v := range x {
if v == y {
return true
}
}
return false
}
// Except returns list x excluding items in list y
func Except(x []string, y []string) []string {
var e []string
for _, v := range x {
if !Contains(y, v) {
e = append(e, v)
}
}
return e
}
type route struct {
from string
to string
distance int
}
type key struct {
from string
to string
}
type queueItem struct {
visited []string
distance int
}
func format(line string) route {
re := regexp.MustCompile("([A-Za-z]+) to ([A-Za-z]+) = ([0-9]+)")
res := re.FindStringSubmatch(line)
if res == nil {
log.Fatal("Regex failed on input", line)
}
distance, err := strconv.Atoi(res[3])
if err != nil {
log.Fatal("Failed to convert distance", res[3])
}
return route{res[1], res[2], distance}
}
func main() {
file, err := os.Open("input/09.txt")
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
routes := make(map[key]int)
cities := []string{}
for scanner.Scan() {
res := format(scanner.Text())
routes[key{res.from, res.to}] = res.distance
routes[key{res.to, res.from}] = res.distance
cities = append(cities, res.from, res.to)
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
var queue []queueItem
var trips []queueItem
cities = Unique(cities)
// setup queue
for _, city := range cities {
queue = append(queue, queueItem{[]string{city}, 0})
}
// fill/drain the queue
for {
if len(queue) == 0 {
break
}
// pop front
var item queueItem
item, queue = queue[0], queue[1:]
// keep track of completed journeys
if len(item.visited) == len(cities) {
trips = append(trips, item)
}
from := item.visited[len(item.visited)-1]
for _, to := range Except(cities, item.visited) {
visited := make([]string, len(item.visited))
/* avoid references */
copy(visited, item.visited)
distance := item.distance + routes[key{from, to}]
queue = append(queue, queueItem{append(visited, to), distance})
}
}
min := 1000000
max := -1
for _, trip := range trips {
if trip.distance < min {
min = trip.distance
}
if trip.distance > max {
max = trip.distance
}
}
fmt.Println(min)
fmt.Println(max)
}