-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
189 lines (163 loc) · 3.38 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
package main
import (
"io"
lib "github.com/teivah/advent-of-code"
)
func fs1(input io.Reader) int {
regex := lib.ReaderToString(input)
regex = regex[1 : len(regex)-1]
current := &Room{}
_ = traverse(regex, 0, current)
return bfs(current)
}
func traverse(s string, i int, current *Room) []*Room {
if i == len(s) {
return nil
}
root := current
var rooms []*Room
for ; i < len(s); i++ {
switch s[i] {
case 'N':
if current.north == nil {
current.north = &Room{pos: current.pos.delta(-1, 0), south: current}
}
current = current.north
case 'S':
if current.south == nil {
current.south = &Room{pos: current.pos.delta(1, 0), north: current}
}
current = current.south
case 'W':
if current.west == nil {
current.west = &Room{pos: current.pos.delta(0, -1), east: current}
}
current = current.west
case 'E':
if current.east == nil {
current.east = &Room{pos: current.pos.delta(0, 1), west: current}
}
current = current.east
case '(':
rooms = append(rooms, traverse(s, i+1, current)...)
// We need to find the next index
i++
open := 1
for ; ; i++ {
if s[i] == '(' {
open++
} else if s[i] == ')' {
open--
}
if open == 0 {
break
}
}
case '|':
rooms = append(rooms, current)
current = root
if s[i+1] == ')' {
// Empty option
rooms = append(rooms, root)
}
case ')':
return rooms
}
}
return rooms
}
func bfs(root *Room) int {
distance := make(map[Position]int)
type State struct {
room *Room
dst int
}
q := []State{{room: root}}
for len(q) != 0 {
state := q[0]
q = q[1:]
if dst, exists := distance[state.room.pos]; exists {
if dst <= state.dst {
continue
}
}
distance[state.room.pos] = state.dst
if state.room.north != nil {
q = append(q, State{state.room.north, state.dst + 1})
}
if state.room.south != nil {
q = append(q, State{state.room.south, state.dst + 1})
}
if state.room.west != nil {
q = append(q, State{state.room.west, state.dst + 1})
}
if state.room.east != nil {
q = append(q, State{state.room.east, state.dst + 1})
}
}
maxDistance := lib.NewMaxer()
for _, v := range distance {
maxDistance.Add(v)
}
return maxDistance.Get()
}
func bfs2(root *Room) int {
distance := make(map[Position]int)
type State struct {
room *Room
dst int
}
q := []State{{room: root}}
for len(q) != 0 {
state := q[0]
q = q[1:]
if dst, exists := distance[state.room.pos]; exists {
if dst <= state.dst {
continue
}
}
distance[state.room.pos] = state.dst
if state.room.north != nil {
q = append(q, State{state.room.north, state.dst + 1})
}
if state.room.south != nil {
q = append(q, State{state.room.south, state.dst + 1})
}
if state.room.west != nil {
q = append(q, State{state.room.west, state.dst + 1})
}
if state.room.east != nil {
q = append(q, State{state.room.east, state.dst + 1})
}
}
sum := 0
for _, v := range distance {
if v >= 1000 {
sum++
}
}
return sum
}
type Room struct {
pos Position
north *Room
south *Room
west *Room
east *Room
}
type Position struct {
row int
col int
}
func (p Position) delta(row, col int) Position {
p.row += row
p.col += col
return p
}
func fs2(input io.Reader) int {
regex := lib.ReaderToString(input)
regex = regex[1 : len(regex)-1]
current := &Room{}
_ = traverse(regex, 0, current)
return bfs2(current)
}