-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path''
187 lines (151 loc) · 4 KB
/
''
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"fmt"
"github.com/stephensli/aoc/helpers/algorithms"
"github.com/stephensli/aoc/helpers/aoc"
"github.com/stephensli/aoc/helpers/file"
"github.com/stephensli/aoc/helpers/printers"
)
func parse(grid [][]string) [][]*Position {
positions := make([][]*Position, len(grid))
galexyIndex := 1
for i := 0; i < len(grid); i++ {
row := make([]*Position, len(grid[i]))
positions[i] = row
for j := 0; j < len(grid[i]); j++ {
position := &Position{
PositionType: EmptySpaceType,
PositionValue: 0,
}
if grid[i][j] == "#" {
position.PositionType = GalexySpaceType
position.PositionValue = galexyIndex
galexyIndex += 1
}
row[j] = position
}
}
return positions
}
func expandSpace(grid [][]*Position) [][]*Position {
// rows
emptyRow := []*Position{}
for i := 0; i < len(grid); i++ {
emptyRow = append(emptyRow, &Position{
Coords: algorithms.Coords{},
PositionType: 0,
PositionValue: 0,
})
}
for i := 0; i < len(grid); i++ {
empty := true
for j := 0; j < len(grid[i]); j++ {
if grid[i][j].Galexy() {
empty = false
break
}
}
if empty {
grid = append(grid[:i+1], grid[i:]...)
grid[i] = emptyRow
i += 1
}
}
// columns
for i := 0; i < len(grid[0]); i++ {
empty := true
for j := 0; j < len(grid); j++ {
if grid[j][i].Galexy() {
empty = false
break
}
}
if empty {
for j := 0; j < len(grid); j++ {
grid[j] = append(grid[j][:i+1], grid[j][i:]...)
grid[j][i] = &Position{
Coords: algorithms.Coords{},
PositionType: 0,
PositionValue: 0,
}
}
i += 1
}
}
return grid
}
func getGalexyPositions(grid [][]*Position) ([]algorithms.Coords, map[int]algorithms.Coords) {
list := []algorithms.Coords{}
mapView := map[int]algorithms.Coords{}
for i, v := range grid {
for j, p := range v {
if p.Galexy() {
list = append(list, algorithms.Coords{X: i, Y: j})
mapView[p.PositionValue] = algorithms.Coords{X: i, Y: j}
}
}
}
return list, mapView
}
func main() {
path, complete := aoc.Setup(2023, 11, true)
defer complete()
grid := expandSpace(parse(file.ToTextSplit(path, "")))
_, galexiesMappings := getGalexyPositions(grid)
//galexies, galexiesMappings := getGalexyPositions(grid)
printers.PrettyPrintGird(grid)
p1 := 0
//history := map[string]bool{}
var nodes [][]algorithms.Node
for i, v := range grid {
var innerNodes []algorithms.Node
for j, p := range v {
p.Coords = algorithms.Coords{X: i, Y: j}
innerNodes = append(innerNodes, p)
}
nodes = append(nodes, innerNodes)
}
// for i, c := range galexies {
// for j, c2 := range galexies {
// historykeyOne := fmt.Sprintf("%d:%d:%d:%d", c.X, c.Y, c2.X, c2.Y)
// historykeyTwo := fmt.Sprintf("%d:%d:%d:%d", c2.X, c2.Y, c.X, c.Y)
//
// if i != j && !history[historykeyOne] && !history[historykeyTwo] {
// history[historykeyOne] = true
// history[historykeyTwo] = true
//
// fmt.Printf("grid[c.X][c.Y].PositionValue: %v\n", grid[c.X][c.Y].PositionValue)
// fmt.Printf("grid[c2.X][c2.Y].PositionValue: %v\n\n", grid[c2.X][c2.Y].PositionValue)
//
// shortest, distance, previous := algorithms.DijkstraGrid(nodes, algorithms.NonDigagnonalDirections, c, c2)
// p1 += shortest
//
// fmt.Printf("distance: %v\n", distance)
// fmt.Printf("previous: %v\n", previous)
// }
// }
// }
fmt.Println()
fmt.Println()
fmt.Println()
fmt.Println()
shortest, distance, previous := algorithms.DijkstraGrid(nodes, algorithms.NonDigagnonalDirections,
galexiesMappings[1], galexiesMappings[4])
for _, n := range distance {
// if n.(*Position).Galexy() {
// continue
// }
fmt.Printf("n.Position(): %v\n", n.Position())
grid[n.Position().X][n.Position().Y] = &Position{
Coords: n.Position(),
PositionType: Filled,
PositionValue: 0,
}
}
fmt.Printf("shortest: %v\n", shortest)
fmt.Printf("distance: %v\n", distance)
fmt.Printf("previous: %v\n", previous)
printers.PrettyPrintGird(grid)
aoc.PrintAnswer(1, p1)
aoc.PrintAnswer(2, path)
}