generated from devries/aoc_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.go
194 lines (167 loc) · 3.89 KB
/
solution.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
package day10p2
import (
"fmt"
"io"
"aoc/utils"
)
func Solve(r io.Reader) any {
lines := utils.ReadLines(r)
maze := make(PipeMaze)
for j, ln := range lines {
for i, r := range ln {
p := utils.Point{X: i, Y: -j}
maze[p] = r
}
}
bfs := utils.NewBFS[utils.Point]()
_, err := bfs.Run(maze)
if err != utils.BFSNotFound {
utils.Check(err, "Error during search")
}
// Remove all non-loop pipe pieces
for p := range maze {
if !bfs.Visited[p] {
maze[p] = '.'
}
}
// Replace initial starting point with actual piece
start := maze.GetInitial()
neighbors := maze.GetNeighbors(start)
switch {
case neighbors[0] == start.Add(utils.North) && neighbors[1] == start.Add(utils.South):
maze[start] = '|'
case neighbors[0] == start.Add(utils.North) && neighbors[1] == start.Add(utils.East):
maze[start] = 'L'
case neighbors[0] == start.Add(utils.North) && neighbors[1] == start.Add(utils.West):
maze[start] = 'J'
case neighbors[0] == start.Add(utils.South) && neighbors[1] == start.Add(utils.East):
maze[start] = 'F'
case neighbors[0] == start.Add(utils.South) && neighbors[1] == start.Add(utils.West):
maze[start] = '7'
case neighbors[0] == start.Add(utils.East) && neighbors[1] == start.Add(utils.West):
maze[start] = '-'
default:
panic("Unable to find pipe that fits starting position")
}
// Count how many times we cut through an east side of
// a pipe moving from a blank point north. If even,
// outside, if odd inside
sum := 0
for p, v := range maze {
if v == '.' {
// Empty point
count := 0
for j := p.Y + 1; j <= 0; j++ {
nv := maze[utils.Point{X: p.X, Y: j}]
if nv == '-' || nv == 'F' || nv == 'L' {
count++
}
}
if count%2 == 0 {
maze[p] = 'O'
} else {
maze[p] = 'I'
sum++
}
}
}
if utils.Verbose {
maze.Print()
}
return sum
}
type PipeMaze map[utils.Point]rune
func (m PipeMaze) Print() {
translations := map[rune]rune{
'I': '█',
'O': ' ',
'L': '└',
'F': '┌',
'7': '┐',
'J': '┘',
'-': '─',
'|': '│',
}
maxx := 0
miny := 0
for p := range m {
if p.X > maxx {
maxx = p.X
}
if p.Y < miny {
miny = p.Y
}
}
for j := 0; j >= miny; j-- {
for i := 0; i <= maxx; i++ {
v := m[utils.Point{X: i, Y: j}]
c := translations[m[utils.Point{X: i, Y: j}]]
if c == 0 {
c = v
}
fmt.Printf("%c", c)
}
fmt.Printf("\n")
}
fmt.Printf("\n")
}
func (m PipeMaze) GetInitial() utils.Point {
for k, v := range m {
if v == 'S' {
return k
}
}
return utils.Point{}
}
func (m PipeMaze) GetNeighbors(p utils.Point) []utils.Point {
var directions []utils.Point
switch m[p] {
case '|':
directions = []utils.Point{utils.North, utils.South}
case '-':
directions = []utils.Point{utils.East, utils.West}
case 'L':
directions = []utils.Point{utils.North, utils.East}
case 'J':
directions = []utils.Point{utils.North, utils.West}
case '7':
directions = []utils.Point{utils.South, utils.West}
case 'F':
directions = []utils.Point{utils.South, utils.East}
case '.':
directions = []utils.Point{}
case 'S':
directions = []utils.Point{utils.North, utils.South, utils.East, utils.West}
}
// Validate connector and add if there is a connecting pipe
neighbors := []utils.Point{}
for _, d := range directions {
switch d {
case utils.North:
n := p.Add(d)
if m[n] == '|' || m[n] == '7' || m[n] == 'F' {
neighbors = append(neighbors, n)
}
case utils.South:
n := p.Add(d)
if m[n] == '|' || m[n] == 'L' || m[n] == 'J' {
neighbors = append(neighbors, n)
}
case utils.East:
n := p.Add(d)
if m[n] == '-' || m[n] == 'J' || m[n] == '7' {
neighbors = append(neighbors, n)
}
case utils.West:
n := p.Add(d)
if m[n] == '-' || m[n] == 'L' || m[n] == 'F' {
neighbors = append(neighbors, n)
}
}
}
return neighbors
}
func (m PipeMaze) IsFinal(p utils.Point) bool {
// Going to run until there are no more points
return false
}