-
Notifications
You must be signed in to change notification settings - Fork 0
/
advent19_test.go
283 lines (259 loc) · 6.98 KB
/
advent19_test.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package main
import (
"fmt"
"io/ioutil"
"regexp"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
const input = ` |
| +--+
A | C
F---|----E|--+
| | | D
+B-+ +--+
`
const input2 = ` |
| +--+
A | C
F---+----E|--+
| | | D
+B-+ +--+
`
func TestExample1(t *testing.T) {
assert.Equal(t, "ABCDEF", advent19(input))
}
func TestExample2(t *testing.T) {
assert.Equal(t, "ABCDEF", advent19(input2))
}
func TestPart1(t *testing.T) {
bytes, err := ioutil.ReadFile("./input.txt")
if err != nil {
fmt.Print(err)
}
assert.Equal(t, "abc", advent19(string(bytes)))
}
const empty = 0
const horizontal = 1
const vertical = 2
const cross = 3
type coord struct {
x int
y int
}
func (c *coord) isValid(width int, height int) bool {
fmt.Println("isvalid ", c, c.x >= 0 && c.x < width && c.y >= 0 && c.y < height)
return c.x >= 0 && c.x < width && c.y >= 0 && c.y < height
}
func (c *coord) move(direction int) coord {
switch direction {
case up:
return c.up()
case down:
return c.down()
case right:
return c.right()
case left:
return c.left()
}
return *c
}
func (c *coord) left() coord {
return coord{x: c.x - 1, y: c.y}
}
func (c *coord) right() coord {
return coord{x: c.x + 1, y: c.y}
}
func (c *coord) up() coord {
return coord{x: c.x, y: c.y - 1}
}
func (c *coord) down() coord {
return coord{x: c.x, y: c.y + 1}
}
const down = 0
const up = 1
const left = 2
const right = 3
const blocked = -1
func padMap(rawNetworkMap string) [][]byte {
rawLines := strings.Split(rawNetworkMap, "\n")
width := 0
for _, line := range rawLines {
if len(line) > width {
width = len(line)
}
}
height := len(rawLines)
width += 2
height += 2
lines := make([][]byte, height)
for y := 0; y < height; y++ {
lines[y] = make([]byte, width)
for x := 0; x < width; x++ {
if x == 0 || y == 0 || x == width-1 || y == height-1 {
lines[y][x] = '.'
} else if len(rawLines[y-1]) <= x-1 {
lines[y][x] = '.'
} else {
lines[y][x] = rawLines[y-1][x-1]
if lines[y][x] == ' ' {
lines[y][x] = '.'
}
}
}
}
drawMap(lines, coord{x: -1, y: -1}, width, height)
return lines
}
func advent19(rawNetworkMap string) string {
chars := []byte{}
IsLetter := regexp.MustCompile(`[A-Z]`).MatchString
lines := padMap(rawNetworkMap)
direction := down
width := len(lines[0])
height := len(lines)
fmt.Println(width, height)
pos := coord{x: findX(1, '|', lines), y: 1}
next := pos.down()
fmt.Println(pos)
for {
if lines[pos.y][pos.x] == '+' {
// drawMap(lines, pos, width, height)
next = pos.move(direction)
if (direction == left || direction == right) && (lines[next.y][next.x] == '-' || IsLetter(string(lines[next.y][next.x]))) {
// fmt.Println("skip + since we horizontal and next is ", string(lines[next.y][next.x]))
} else if (direction == up || direction == down) && (lines[next.y][next.x] == '|' || IsLetter(string(lines[next.y][next.x]))) {
// fmt.Println("skip + since we vertical and next is ", string(lines[next.y][next.x]))
} else {
fmt.Println("Turning on +")
fmt.Println("Next is (", next.x, "/", next.y, ") -> ", string(lines[next.y][next.x]))
fmt.Println("Direction is ", direction)
pos, direction = chooseNextDirection(lines, direction, pos)
}
}
if IsLetter(string(lines[pos.y][pos.x])) {
fmt.Println("FOUND ", string(lines[pos.y][pos.x]))
chars = append(chars, lines[pos.y][pos.x])
}
pos = pos.move(direction)
if lines[pos.y][pos.x] == '.' {
return string(chars)
}
}
return string(chars)
}
func chooseNextDirection(lines [][]byte, direction int, pos coord) (coord, int) {
IsLetter := regexp.MustCompile(`[A-Z]`).MatchString
if direction == up || direction == down {
newpos := pos.right()
if lines[newpos.y][newpos.x] == '-' || IsLetter(string(lines[newpos.y][newpos.x])) {
return newpos, right
}
newpos = pos.left()
if lines[newpos.y][newpos.x] == '-' || IsLetter(string(lines[newpos.y][newpos.x])) {
return newpos, left
}
} else if direction == right || direction == left {
newpos := pos.up()
if lines[newpos.y][newpos.x] == '|' || IsLetter(string(lines[newpos.y][newpos.x])) {
return newpos, up
}
newpos = pos.down()
if lines[newpos.y][newpos.x] == '|' || IsLetter(string(lines[newpos.y][newpos.x])) {
return newpos, down
}
}
return pos, blocked
}
func drawMap(lines [][]byte, pos coord, width int, height int) {
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
if pos.x == x && pos.y == y {
fmt.Print("@")
} else {
fmt.Print(string(lines[y][x]))
}
}
fmt.Println()
}
fmt.Println()
}
func findX(y int, search byte, lines [][]byte) int {
for x, char := range lines[y] {
if char == search {
return x
}
}
return -1
}
// for index, line := range rawLines {
// missing := len(line) - width
// if missing > 0 {
// for index := 0; index < missing; index++ {
//
// }
// }
// }
//
// fmt.Println("width ", width, " height ", height)
// pos := coord{x: findX(0, '|', lines), y: 0}
// direction := down
//
// for pos.isValid(width, height) {
// fmt.Println("start")
// for pos.isValid(width, height) && len(lines[pos.y]) >= pos.x && lines[pos.y][pos.x] != '+' {
// fmt.Println(string(lines[pos.y][pos.x]), pos)
// if IsLetter(string(lines[pos.y][pos.x])) {
// chars = append(chars, lines[pos.y][pos.x])
// }
// switch direction {
// case up:
// pos = pos.up()
// case down:
// pos = pos.down()
// case right:
// pos = pos.right()
// case left:
// pos = pos.left()
// }
// }
// if !pos.isValid(width, height) || len(lines[pos.y]) < pos.x {
// return string(chars)
// }
// fmt.Println("plus found at ", pos.x, pos.y)
// if direction == right || direction == left {
// upPos := pos.up()
// fmt.Println("consider up ?")
// if upPos.isValid(width, height) && len(lines[upPos.y]) > pos.x && (lines[upPos.y][upPos.x] == '|' || IsLetter(string(lines[upPos.y][upPos.x]))) {
// fmt.Println("follow up")
// pos = upPos
// direction = up
// } else {
// downPos := pos.down()
// fmt.Println("consider down ?")
// if downPos.isValid(width, height) && len(lines[downPos.y]) > pos.x && (lines[downPos.y][downPos.x] == '|' || IsLetter(string(lines[downPos.y][downPos.x]))) {
// fmt.Println("follow down")
// pos = downPos
// direction = down
// } else {
// fmt.Println("no proper direction")
// }
// }
// } else if direction == up || direction == down {
// rightPos := pos.right()
// if rightPos.isValid(width, height) && len(lines[rightPos.y]) > pos.x && (lines[rightPos.y][rightPos.x] == '-' || IsLetter(string(lines[rightPos.y][rightPos.x]))) {
// fmt.Println("follow right")
// pos = rightPos
// direction = right
// } else {
// leftPos := pos.left()
// if leftPos.isValid(width, height) && len(lines[leftPos.y]) > pos.x && (lines[leftPos.y][leftPos.x] == '-' || IsLetter(string(lines[leftPos.y][leftPos.x]))) {
// fmt.Println("follow left")
// pos = leftPos
// direction = left
// }
// }
// }
// fmt.Println("end loop with ", pos)
// }