-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day10.go
321 lines (239 loc) · 6.51 KB
/
Day10.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package AdventOfCode2023
import (
"fmt"
"math"
"regexp"
"strings"
"github.com/kaitachi/go-challenges/internal/lib"
)
type Day10 struct {
start *cell
grid []map[int]*cell
rows int
cols int
}
type cell struct {
name string
row int
col int
isMainLoop bool
isFilled bool
directions map[string]*cell
}
// 1. Assemble - How should we transform the data from our input files?
func (s *Day10) Assemble(tc *lib.TestCase) {
s.rows = 0
s.cols = 0
s.start = nil
s.grid = make([]map[int]*cell, 0)
re_pipe := regexp.MustCompile(`[|\-LJ7FS.]`)
for row, line := range strings.Split(tc.Input, "\n") {
s.grid = append(s.grid, make(map[int]*cell, 0))
matches := re_pipe.FindAllString(line, -1)
count := re_pipe.FindAllStringIndex(line, -1)
for i := 0; i < len(count); i++ {
s.grid[row][count[i][0]] = &cell{
name: matches[i],
row: row,
col: count[i][0],
directions: make(map[string]*cell, 0),
}
if matches[i] == "S" {
s.start = s.grid[row][count[i][0]]
}
}
}
s.linkPipes()
}
// 2. Activate - Take our transformed input data and make the core logic needed to resolve this Problem
func (s *Day10) Activate(tc *lib.TestCase) {
// Assign final value to TestCase.Actual field
switch tc.Algorithm {
case "part01":
tc.Actual = s.part01()
case "part02":
tc.Actual = s.part02()
}
}
func (s Day10) part01() string {
steps := s.drawLoop()
return fmt.Sprintf("%d", steps)
}
func (s Day10) part02() string {
s.drawLoop()
// Let's replace our starting position
_, okNorth := s.start.directions["N"]
_, okEast := s.start.directions["E"]
_, okSouth := s.start.directions["S"]
_, okWest := s.start.directions["W"]
links := fmt.Sprintf("%5t-%5t-%5t-%5t", okNorth, okEast, okSouth, okWest)
switch links {
case "false- true- true-false": s.start.name = "F"
case " true- true-false-false": s.start.name = "L"
case "false-false- true- true": s.start.name = "7"
case " true-false-false- true": s.start.name = "J"
case " true-false- true-false": s.start.name = "|"
case "false- true-false- true": s.start.name = "-"
}
inner := make([]*cell, 0)
inLoopCol := make([]bool, s.cols) // Scanning from top to bottom, could we say this cell could be within our loop?
loopColCorner := make([]string, s.cols)
for row, _ := range s.grid {
inLoopRow := false
for col := 0; col < s.cols; col++ {
cell := s.grid[row][col]
if cell.isMainLoop {
// Are we entering/exiting the loop?
switch cell.name {
case "|":
inLoopRow = !inLoopRow
case "-", "7", "F":
inLoopCol[col] = !inLoopCol[col]
switch cell.name {
case "7", "F":
loopColCorner[col] = cell.name
}
case "L":
inLoopRow = !inLoopRow
if loopColCorner[col] == "F" {
inLoopCol[col] = !inLoopCol[col]
loopColCorner[col] = ""
}
case "J":
inLoopRow = !inLoopRow
if loopColCorner[col] == "7" {
inLoopCol[col] = !inLoopCol[col]
loopColCorner[col] = ""
}
}
} else if inLoopCol[col] && inLoopRow {
// We're not touching the loop. This cell would then be inside of our loop.
cell.isFilled = true
inner = append(inner, cell)
}
if row == 4 && col == 7 {
fmt.Printf(">>>> (%d, %d): inLoopRow: %t; inLoopCol: %t\n", row, col, inLoopRow, inLoopCol[col])
}
}
}
fmt.Printf("%+v\n", s.start)
// for i := 0; i < len(s.grid); i++ {
// fmt.Printf("[%d]: %v\n", i, s.grid[i])
//
// // for j, pipe := range s.grid[i] {
// // fmt.Printf("[%d]: %+v\n", j, pipe)
// // }
// }
fmt.Printf("%v\n", inner)
s.printMainLoop()
return fmt.Sprintf("%d", len(inner))
}
func (s *Day10) drawLoop() int {
// Let's make two pointers, one travels left and the other travels right
var left, right *cell
var prev_left, prev_right *cell
steps := 0
for _, path := range s.start.directions {
switch {
case left == nil: left = path
default: right = path
}
}
prev_left = right
prev_right = left
for steps = 1; left != right; steps++ {
prev_left, left = left.next(prev_left)
prev_right, right = right.next(prev_right)
}
// Mark first and last nodes that are visited as part of the loop
s.start.isMainLoop = true
left.isMainLoop = true
return steps
}
func (s *Day10) linkPipes() {
for _, line := range s.grid {
s.rows++
for col, _ := range line {
s.cols = int(math.Max(float64(s.cols), float64(col)))
}
}
for row, line := range s.grid {
for col, pipe := range line {
isStart := pipe.name == "S"
// Looking North, can we connect to our neighbour?
if row-1 >= 0 && (pipe.hasPipeFacing("N") || isStart) {
north := s.grid[row-1][col]
if north != nil && north.hasPipeFacing("S") {
pipe.directions["N"] = north
}
}
// Looking East, can we connect to our neighbour?
if col+1 <= s.cols && (pipe.hasPipeFacing("E") || isStart) {
east := s.grid[row][col+1]
if east != nil && east.hasPipeFacing("W") {
pipe.directions["E"] = east
}
}
// Looking South, can we connect to our neighbour?
if row+1 < s.rows && (pipe.hasPipeFacing("S") || isStart) {
south := s.grid[row+1][col]
if south != nil && south.hasPipeFacing("N") {
pipe.directions["S"] = south
}
}
// Looking West, can we connect to our neighbour?
if col-1 >= 0 && (pipe.hasPipeFacing("W") || isStart) {
west := s.grid[row][col-1]
if west != nil && west.hasPipeFacing("E") {
pipe.directions["W"] = west
}
}
}
}
}
func (c cell) isPipe() bool {
return c.name != "."
}
func (c cell) hasPipeFacing(d string) bool {
// Looking North, can we connect to our neighbour?
switch c.name {
case "|", "L", "J": if d == "N" { return true }
}
// Looking East, can we connect to our neighbour?
switch c.name {
case "-", "L", "F": if d == "E" { return true }
}
// Looking South, can we connect to our neighbour?
switch c.name {
case "|", "7", "F": if d == "S" { return true }
}
// Looking West, can we connect to our neighbour?
switch c.name {
case "-", "J", "7": if d == "W" { return true }
}
return false
}
func (c *cell) next(prev *cell) (*cell, *cell) {
c.isMainLoop = true
for _, neighbor := range c.directions {
if neighbor != prev {
return c, neighbor
}
}
return nil, nil
}
func (s *Day10) printMainLoop() {
fmt.Printf("%d rows, %d cols\n", s.rows, s.cols)
for row := 0; row < s.rows; row++ {
fmt.Printf("[%d]: ", row)
for col := 0; col < s.cols; col++ {
cell := s.grid[row][col]
switch {
case cell.isMainLoop: fmt.Printf("%s", cell.name)
case cell.isFilled: fmt.Printf("I")
default: fmt.Printf(".")
}
}
fmt.Printf("\n")
}
}