-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10.go
238 lines (205 loc) · 4.86 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
package day10
type Day struct{}
type Direction string
const (
START_PIPE = 'S'
NODIR Direction = "NODIR"
UP Direction = "UP"
RIGHT Direction = "RIGHT"
DOWN Direction = "DOWN"
LEFT Direction = "LEFT"
)
func (d Direction) Opposite() Direction {
switch d {
case UP:
return DOWN
case DOWN:
return UP
case LEFT:
return RIGHT
case RIGHT:
return LEFT
}
return NODIR
}
var PIPEDIRS = map[rune]map[Direction]bool{
'|': {
UP: true,
RIGHT: false,
DOWN: true,
LEFT: false,
},
'-': {
UP: false,
RIGHT: true,
DOWN: false,
LEFT: true,
},
'L': {
UP: true,
RIGHT: true,
DOWN: false,
LEFT: false,
},
'J': {
UP: true,
RIGHT: false,
DOWN: false,
LEFT: true,
},
'7': {
UP: false,
RIGHT: false,
DOWN: true,
LEFT: true,
},
'F': {
UP: false,
RIGHT: true,
DOWN: true,
LEFT: false,
},
'.': {
UP: false,
RIGHT: false,
DOWN: false,
LEFT: false,
},
'S': {
UP: true,
RIGHT: true,
DOWN: true,
LEFT: true,
},
}
type Node struct {
Next *Node
Rune rune
Pos [2]int
}
// Priority -> Top, Right, Down, Left
func getNextRune(pipeMap [][]rune, tgtI, tgtJ int, previousInvertedDir Direction) ([2]int, Direction) {
connectedWidth := getConnectedWith(pipeMap, tgtI, tgtJ)
previousDir := previousInvertedDir.Opposite()
if val, ok := connectedWidth[UP]; ok && previousDir != UP {
return val, UP
}
if val, ok := connectedWidth[RIGHT]; ok && previousDir != RIGHT {
return val, RIGHT
}
if val, ok := connectedWidth[DOWN]; ok && previousDir != DOWN {
return val, DOWN
}
if val, ok := connectedWidth[LEFT]; ok && previousDir != LEFT {
return val, LEFT
}
return [2]int{-1, -1}, NODIR
}
func getConnectedWith(pipeMap [][]rune, tgtI, tgtJ int) map[Direction][2]int {
connectedWith := make(map[Direction][2]int, 4)
// If top is connected with tgt (which is bottom):
if tgtI > 0 && PIPEDIRS[pipeMap[tgtI][tgtJ]][UP] {
if PIPEDIRS[pipeMap[tgtI-1][tgtJ]][DOWN] {
connectedWith[UP] = [2]int{tgtI - 1, tgtJ}
}
}
// If bottom is connected with tgt (which is top):
if tgtI < len(pipeMap)-1 && PIPEDIRS[pipeMap[tgtI][tgtJ]][DOWN] {
if PIPEDIRS[pipeMap[tgtI+1][tgtJ]][UP] {
connectedWith[DOWN] = [2]int{tgtI + 1, tgtJ}
}
}
// If left is connected with tgt (which is right):
if tgtJ > 0 && PIPEDIRS[pipeMap[tgtI][tgtJ]][LEFT] {
if PIPEDIRS[pipeMap[tgtI][tgtJ-1]][RIGHT] {
connectedWith[LEFT] = [2]int{tgtI, tgtJ - 1}
}
}
// If right is connected with tgt (which is left):
if tgtJ < len(pipeMap[tgtI])-1 && PIPEDIRS[pipeMap[tgtI][tgtJ]][RIGHT] {
if PIPEDIRS[pipeMap[tgtI][tgtJ+1]][LEFT] {
connectedWith[RIGHT] = [2]int{tgtI, tgtJ + 1}
}
}
return connectedWith
}
func (d Day) GetInput(lines []string) interface{} {
pipeMap := make([][]rune, len(lines))
startPos := [2]int{-1, -1}
for i, line := range lines {
pipeMap[i] = make([]rune, len(line))
for j, element := range line {
pipeMap[i][j] = element
if element == START_PIPE {
startPos = [2]int{i, j}
}
}
}
beginNode := &Node{Rune: START_PIPE, Next: nil, Pos: startPos}
currentNode := beginNode
lastUsedPos := NODIR
for {
var newPos [2]int
newPos, lastUsedPos = getNextRune(pipeMap, currentNode.Pos[0], currentNode.Pos[1], lastUsedPos)
if newPos == startPos {
break
}
currentNode.Next = &Node{Rune: pipeMap[newPos[0]][newPos[1]], Pos: newPos, Next: nil}
currentNode = currentNode.Next
}
currentNode.Next = beginNode // Close last node with starting node
return ParsedInput{BeginNode: beginNode, PipeMap: pipeMap}
}
type ParsedInput struct {
BeginNode *Node
PipeMap [][]rune
}
func (d Day) SolvePart1(parsedInputI interface{}) int {
beginNode := parsedInputI.(ParsedInput).BeginNode
currentNode := beginNode
cicleSteps := 1 // Count the S
for currentNode.Next != beginNode {
currentNode = currentNode.Next
cicleSteps++
}
return cicleSteps / 2
}
func isPartOfTheCycle(beginNode *Node, i, j int) bool {
currentNode := beginNode
began := false
for !(currentNode == beginNode && began) {
began = true
if currentNode.Pos[0] == i && currentNode.Pos[1] == j {
return true
}
currentNode = currentNode.Next
}
return false
}
func isPointInside(beginNode *Node, y, x int) bool {
count := 0
currentNode := beginNode
for currentNode.Next != beginNode {
p1 := currentNode.Pos
p2 := currentNode.Next.Pos
x1, y1 := p1[1], p1[0]
x2, y2 := p2[1], p2[0]
if (y < y1) != (y < y2) && x < x1+((y-y1)/(y2-y1))*(x2-x1) {
count++
}
currentNode = currentNode.Next
}
return count%2 == 1
}
func (d Day) SolvePart2(parsedInputI interface{}) int {
parsedInput := parsedInputI.(ParsedInput)
count := 0
for i := 0; i < len(parsedInput.PipeMap); i++ {
for j := 0; j < len(parsedInput.PipeMap[i]); j++ {
if !isPartOfTheCycle(parsedInput.BeginNode, i, j) && isPointInside(parsedInput.BeginNode, i, j) {
count++
}
}
}
return count
}