-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
150 lines (135 loc) · 2.73 KB
/
main.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
package main
import (
_ "embed"
"fmt"
. "github.com/beanz/advent/lib-go"
)
//go:embed input.txt
var input []byte
type Game struct {
m map[Point]byte
bb *BoundingBox
numbers map[byte]Point
debug bool
}
func (g *Game) String() string {
s := ""
for y := g.bb.Min.Y; y <= g.bb.Max.Y; y++ {
for x := g.bb.Min.X; x <= g.bb.Max.X; x++ {
s += string(g.m[Point{x, y}])
}
s += "\n"
}
for k, v := range g.numbers {
s += fmt.Sprintf("%d: %s\n", k, v)
}
return s
}
func readGame(lines []string) *Game {
bb := &BoundingBox{Point{0, 0}, Point{len(lines[0]) - 1, len(lines)}}
g := &Game{map[Point]byte{}, bb, map[byte]Point{}, false}
for y, l := range lines {
for x, ch := range l {
b := byte(ch)
if 48 <= b && b <= 57 {
num := b - 48
g.numbers[num] = Point{x, y}
}
g.m[Point{x, y}] = byte(ch)
}
}
return g
}
type Search struct {
p Point
steps int
}
func (g *Game) DistanceBetween(i, j byte) int {
todo := []Search{Search{g.numbers[i], 0}}
visited := map[Point]bool{}
for len(todo) > 0 {
cur := todo[0]
todo = todo[1:]
if visited[cur.p] {
continue
}
visited[cur.p] = true
for _, np := range cur.p.Neighbours() {
if g.m[np] == '#' {
continue
}
if np == g.numbers[j] {
return cur.steps + 1
}
todo = append(todo, Search{np, cur.steps + 1})
}
}
return -1
}
type Pair struct {
i, j byte
}
type Distances map[Pair]int
func (g *Game) CalculateDistances() Distances {
dist := Distances{}
for i := range g.numbers {
for j := range g.numbers {
if j <= i {
continue
}
d := g.DistanceBetween(i, j)
dist[Pair{i, j}] = d
dist[Pair{j, i}] = d
//fmt.Printf("%d -> %d = %d\n", i, j, d)
}
}
return dist
}
func (g *Game) Part1() int {
dist := g.CalculateDistances()
min := 1000000000
for _, p := range Permutations(1, len(g.numbers)-1) {
d := dist[Pair{0, byte(p[0])}]
for i := 0; i < len(p)-1; i++ {
sd := dist[Pair{byte(p[i]), byte(p[i+1])}]
//fmt.Printf("%d => %d: %d\n", p[i], p[i+1], sd)
d += sd
}
if min > d {
min = d
}
//fmt.Println()
}
return min
}
func (g *Game) Part2() int {
dist := g.CalculateDistances()
min := 1000000000
for _, p := range Permutations(1, len(g.numbers)-1) {
d := dist[Pair{0, byte(p[0])}]
for i := 0; i < len(p)-1; i++ {
sd := dist[Pair{byte(p[i]), byte(p[i+1])}]
//fmt.Printf("%d => %d: %d\n", p[i], p[i+1], sd)
d += sd
}
d += dist[Pair{0, byte(p[len(p)-1])}]
if min > d {
min = d
}
//fmt.Println()
}
return min
}
func main() {
game := readGame(InputLines(input))
p1 := game.Part1()
if !benchmark {
fmt.Printf("Part 1: %d\n", p1)
}
game = readGame(InputLines(input))
p2 := game.Part2()
if !benchmark {
fmt.Printf("Part 2: %d\n", p2)
}
}
var benchmark = false