-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
219 lines (200 loc) · 4.21 KB
/
main.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
package main
import (
"context"
"io"
"sync"
aoc "github.com/teivah/advent-of-code"
"golang.org/x/sync/errgroup"
)
type TileType int
const (
empty TileType = iota
mirrorRight
mirrorLeft
splitterVertical
splitterHorizontal
)
type Beam struct {
pos aoc.Position
dir aoc.Direction
}
func fs1(input io.Reader) int {
board, _, _ := aoc.ParseBoard[TileType](aoc.ReaderToStrings(input), toTileType)
return fire(board, Beam{
pos: aoc.Position{Row: 0, Col: 0},
dir: aoc.Right,
})
}
func toTileType(r rune) TileType {
switch r {
case '.':
return empty
case '/':
return mirrorRight
case '\\':
return mirrorLeft
case '|':
return splitterVertical
case '-':
return splitterHorizontal
default:
panic(r)
}
}
func fire(board map[aoc.Position]TileType, first Beam) int {
cache := make(map[Beam]struct{})
energized := make(map[aoc.Position]struct{})
q := []Beam{first}
for len(q) != 0 {
beam := q[0]
q = q[1:]
if _, exists := cache[beam]; exists {
continue
}
t, exists := board[beam.pos]
if !exists {
continue
}
cache[beam] = struct{}{}
energized[beam.pos] = struct{}{}
switch t {
case empty:
beam.pos = beam.pos.Move(beam.dir, 1)
q = append(q, beam)
case mirrorRight:
switch beam.dir {
case aoc.Left:
beam.dir = aoc.Down
case aoc.Right:
beam.dir = aoc.Up
case aoc.Up:
beam.dir = aoc.Right
case aoc.Down:
beam.dir = aoc.Left
default:
panic(beam.dir)
}
beam.pos = beam.pos.Move(beam.dir, 1)
q = append(q, beam)
case mirrorLeft:
switch beam.dir {
case aoc.Left:
beam.dir = aoc.Up
case aoc.Right:
beam.dir = aoc.Down
case aoc.Up:
beam.dir = aoc.Left
case aoc.Down:
beam.dir = aoc.Right
default:
panic(beam.dir)
}
beam.pos = beam.pos.Move(beam.dir, 1)
q = append(q, beam)
case splitterVertical:
switch beam.dir {
case aoc.Left, aoc.Right:
q = append(q, Beam{
pos: beam.pos.Move(aoc.Up, 1),
dir: aoc.Up,
})
q = append(q, Beam{
pos: beam.pos.Move(aoc.Down, 1),
dir: aoc.Down,
})
case aoc.Up, aoc.Down:
beam.pos = beam.pos.Move(beam.dir, 1)
q = append(q, beam)
}
case splitterHorizontal:
switch beam.dir {
case aoc.Left, aoc.Right:
beam.pos = beam.pos.Move(beam.dir, 1)
q = append(q, beam)
case aoc.Up, aoc.Down:
q = append(q, Beam{
pos: beam.pos.Move(aoc.Left, 1),
dir: aoc.Left,
})
q = append(q, Beam{
pos: beam.pos.Move(aoc.Right, 1),
dir: aoc.Right,
})
}
}
}
return len(energized)
}
func fs2(input io.Reader) int {
board, rows, cols := aoc.ParseBoard[TileType](aoc.ReaderToStrings(input), toTileType)
res := 0
for row := 0; row < rows; row++ {
res = max(res, fire(board, Beam{
pos: aoc.Position{Row: row, Col: 0},
dir: aoc.Right,
}))
res = max(res, fire(board, Beam{
pos: aoc.Position{Row: row, Col: cols - 1},
dir: aoc.Left,
}))
}
for col := 0; col < cols; col++ {
res = max(res, fire(board, Beam{
pos: aoc.Position{Row: 0, Col: col},
dir: aoc.Down,
}))
res = max(res, fire(board, Beam{
pos: aoc.Position{Row: rows - 1, Col: col},
dir: aoc.Up,
}))
}
return res
}
// Created for the sake of https://www.reddit.com/r/adventofcode/comments/18k1q32/2023_day16_part_12_golang/
func fs2Concurrency(input io.Reader) int {
board, rows, cols := aoc.ParseBoard[TileType](aoc.ReaderToStrings(input), toTileType)
res := 0
eg, _ := errgroup.WithContext(context.Background())
mu := sync.Mutex{}
updateMax := func(v int) {
mu.Lock()
defer mu.Unlock()
res = max(v, res)
}
for row := 0; row < rows; row++ {
row := row
eg.Go(func() error {
updateMax(fire(board, Beam{
pos: aoc.Position{Row: row, Col: 0},
dir: aoc.Right,
}))
return nil
})
eg.Go(func() error {
updateMax(fire(board, Beam{
pos: aoc.Position{Row: row, Col: cols - 1},
dir: aoc.Left,
}))
return nil
})
}
for col := 0; col < cols; col++ {
col := col
eg.Go(func() error {
updateMax(fire(board, Beam{
pos: aoc.Position{Row: 0, Col: col},
dir: aoc.Down,
}))
return nil
})
eg.Go(func() error {
updateMax(fire(board, Beam{
pos: aoc.Position{Row: rows - 1, Col: col},
dir: aoc.Up,
}))
return nil
})
}
_ = eg.Wait()
return res
}