generated from devries/aoc_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.go
197 lines (161 loc) · 4.04 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
195
196
197
package day16p2
import (
"io"
"aoc/utils"
)
func Solve(r io.Reader) any {
lines := utils.ReadLines(r)
c := NewContraption(lines)
maxEnergized := 0
// top row
dir := utils.South
for i := 0; i <= c.BottomRight.X; i++ {
c.Starting = BeamState{utils.Point{X: i, Y: 0}, dir}
bfs := utils.NewBFS[BeamState]()
_, err := bfs.Run(c)
if err != utils.BFSNotFound {
utils.Check(err, "Error before searching full map")
}
e := len(c.Energized)
if e > maxEnergized {
maxEnergized = e
}
clear(c.Energized)
}
// bottom row
dir = utils.North
for i := 0; i <= c.BottomRight.X; i++ {
c.Starting = BeamState{utils.Point{X: i, Y: c.BottomRight.Y}, dir}
bfs := utils.NewBFS[BeamState]()
_, err := bfs.Run(c)
if err != utils.BFSNotFound {
utils.Check(err, "Error before searching full map")
}
e := len(c.Energized)
if e > maxEnergized {
maxEnergized = e
}
clear(c.Energized)
}
// left column
dir = utils.East
for i := 0; i >= c.BottomRight.Y; i-- {
c.Starting = BeamState{utils.Point{X: 0, Y: i}, dir}
bfs := utils.NewBFS[BeamState]()
_, err := bfs.Run(c)
if err != utils.BFSNotFound {
utils.Check(err, "Error before searching full map")
}
e := len(c.Energized)
if e > maxEnergized {
maxEnergized = e
}
clear(c.Energized)
}
// right column
dir = utils.West
for i := 0; i >= c.BottomRight.Y; i-- {
c.Starting = BeamState{utils.Point{X: c.BottomRight.X, Y: i}, dir}
bfs := utils.NewBFS[BeamState]()
_, err := bfs.Run(c)
if err != utils.BFSNotFound {
utils.Check(err, "Error before searching full map")
}
e := len(c.Energized)
if e > maxEnergized {
maxEnergized = e
}
clear(c.Energized)
}
return maxEnergized
}
type BeamState struct {
Position utils.Point
Direction utils.Point
}
type Contraption struct {
Tiles map[utils.Point]rune
Energized map[utils.Point]bool
Starting BeamState
BottomRight utils.Point
}
func NewContraption(lines []string) *Contraption {
v := Contraption{Tiles: make(map[utils.Point]rune), Energized: make(map[utils.Point]bool)}
br := utils.Point{}
for j, ln := range lines {
for i, r := range ln {
p := utils.Point{X: i, Y: -j}
v.Tiles[p] = r
br = p
}
}
v.BottomRight = br
return &v
}
func (c *Contraption) GetInitial() BeamState {
return c.Starting
}
func (c *Contraption) GetNeighbors(s BeamState) []BeamState {
tile := c.Tiles[s.Position]
switch tile {
case 0:
// off map
return []BeamState{}
case '.':
// continue on
c.Energized[s.Position] = true
newstate := BeamState{s.Position.Add(s.Direction), s.Direction}
return []BeamState{newstate}
case '\\':
c.Energized[s.Position] = true
var newDirection utils.Point
switch s.Direction {
case utils.North:
newDirection = utils.West
case utils.East:
newDirection = utils.South
case utils.South:
newDirection = utils.East
case utils.West:
newDirection = utils.North
}
newstate := BeamState{s.Position.Add(newDirection), newDirection}
return []BeamState{newstate}
case '/':
c.Energized[s.Position] = true
var newDirection utils.Point
switch s.Direction {
case utils.North:
newDirection = utils.East
case utils.East:
newDirection = utils.North
case utils.South:
newDirection = utils.West
case utils.West:
newDirection = utils.South
}
newstate := BeamState{s.Position.Add(newDirection), newDirection}
return []BeamState{newstate}
case '-':
c.Energized[s.Position] = true
switch s.Direction {
case utils.North, utils.South:
return []BeamState{{s.Position.Add(utils.East), utils.East}, {s.Position.Add(utils.West), utils.West}}
default:
return []BeamState{{s.Position.Add(s.Direction), s.Direction}}
}
case '|':
c.Energized[s.Position] = true
switch s.Direction {
case utils.East, utils.West:
return []BeamState{{s.Position.Add(utils.North), utils.North}, {s.Position.Add(utils.South), utils.South}}
default:
return []BeamState{{s.Position.Add(s.Direction), s.Direction}}
}
default:
panic("Unexpected tile found")
}
}
func (c *Contraption) IsFinal(s BeamState) bool {
return false
}