-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathday_22.lua
More file actions
288 lines (252 loc) · 9.06 KB
/
Copy pathday_22.lua
File metadata and controls
288 lines (252 loc) · 9.06 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
local eio = require "libs.eio"
local profile = require "libs.profile"
local Vec2 = require "libs.Vec2"
local Vec3 = require "libs.Vec3"
local Deque = require "libs.Deque"
local sequence = require "libs.sequence"
local lines = eio.lines
local printf = eio.printf
local tonumber = tonumber
local match = string.match
local len = string.len
local sub = string.sub
local min = math.min
local max = math.max
local huge = math.huge
local P2D = Vec2.makeVec
local P3D = Vec3.makeVec
local cross = Vec3.cross
local floor = math.floor
local makeDual = sequence.dual
profile.start()
local function parseInput ()
local map = {}
local minys = {}
local maxys = {}
local minxs = {}
local maxxs = {}
local instructionsLine = 0
for y, line in ipairs(lines()) do
if line == "" then
instructionsLine = lines()[y + 1]
break
end
map[y] = {}
for x = 1, len(line) do
local c = sub(line, x, x)
map[y][x] = c
if c ~= " " then
minys[x] = min(minys[x] or huge, y)
maxys[x] = max(maxys[x] or -1, y)
minxs[y] = min(minxs[y] or huge, x)
maxxs[y] = max(maxxs[y] or -1, x)
end
end
end
local instructions = {}
local i = 1
while i <= len(instructionsLine) do
local n = match(instructionsLine, "^(%d+)", i)
local r = match(instructionsLine, "^(%a)", i)
if n then
instructions[#instructions + 1] = tonumber(n)
i = i + len(n)
else
instructions[#instructions + 1] = r
i = i + 1
end
end
return map, minys, maxys, minxs, maxxs, instructions
end
local WEST = P2D(0, 1)
local NORTH = P2D(1, 0)
local EAST = P2D(0, -1)
local SOUTH = P2D(-1, 0)
local DIRS = {NORTH, EAST, SOUTH, WEST}
local ROT_LEFT = P2D(0, 1)
local ROT_RIGHT = P2D(0, -1)
local ROT_BACK = P2D(-1, 0)
local rotVecFromInstr = {
["L"] = ROT_LEFT,
["R"] = ROT_RIGHT
}
local facingFromDir = {
[WEST] = 0,
[NORTH] = 1,
[EAST] = 2,
[SOUTH] = 3
}
local function password (pos, dir)
return 1000 * pos[1] + 4 * pos[2] + facingFromDir[dir]
end
local function rot (v, u)
return P2D(v[1] * u[1] - v[2] * u[2], v[1] * u[2] + v[2] * u[1])
end
local function walkTorus (map, minys, maxys, minxs, maxxs, instructions)
local pos = P2D(1, minxs[1])
local dir = WEST
for _, instr in ipairs(instructions) do
if type(instr) == "number" then
while instr > 0 do
local nextPos = pos + dir
local minx = minxs[pos[1]]
local maxx = maxxs[pos[1]]
local miny = minys[pos[2]]
local maxy = maxys[pos[2]]
if nextPos[2] < minx then
nextPos = P2D(nextPos[1], maxx)
elseif nextPos[2] > maxx then
nextPos = P2D(nextPos[1], minx)
elseif nextPos[1] < miny then
nextPos = P2D(maxy, nextPos[2])
elseif nextPos[1] > maxy then
nextPos = P2D(miny, nextPos[2])
end
if map[nextPos[1]][nextPos[2]] == "#" then
break
end
pos = nextPos
instr = instr - 1
end
else
dir = rot(dir, rotVecFromInstr[instr])
end
end
return pos, dir
end
local UP = P3D(0, 0, 1)
local DOWN = P3D(0, 0, -1)
local LEFT = P3D(-1, 0, 0)
local RIGHT = P3D(1, 0, 0)
local FRONT = P3D(0, 1, 0)
local BACK = P3D(0, -1, 0)
local function findSize (minys, maxys, minxs, maxxs)
local mindiff = huge
for x = 1, #minys do
mindiff = min(maxys[x] - minys[x] + 1, mindiff)
end
for y = 1, #minxs do
mindiff = min(maxxs[y] - minxs[y] + 1, mindiff)
end
return mindiff
end
local map, minys, maxys, minxs, maxxs, instructions = parseInput()
local answer1 = password(walkTorus(map, minys, maxys, minxs, maxxs, instructions))
printf("Part 1: %i\n", answer1)
local SIZE = findSize(minys, maxys, minxs, maxxs)
local HALF_SIZE = (SIZE - 1) / 2
local function cutMap (map)
local faces = {}
local faceFromTile = {}
for miny = 1, #map, SIZE do
for minx = 1, #map[miny], SIZE do
if map[miny][minx] ~= " " then
local face = {}
face.minx = minx
face.maxx = minx + SIZE - 1
face.miny = miny
face.maxy = miny + SIZE - 1
face.tiley = floor(miny / SIZE) + 1
face.tilex = floor(minx / SIZE) + 1
faces[#faces + 1] = face
faceFromTile[P2D(face.tiley, face.tilex)] = face
end
end
end
return faces, faceFromTile
end
local function makeEdges (face, edge2d, neighbourFaceNormal)
face.edges = {[edge2d] = neighbourFaceNormal}
face.edges[rot(edge2d, ROT_RIGHT)] = -cross(face.normal, neighbourFaceNormal)
face.edges[rot(edge2d, ROT_BACK)] = -neighbourFaceNormal
face.edges[rot(edge2d, ROT_LEFT)] = cross(face.normal, neighbourFaceNormal)
face.edgesDual = makeDual(face.edges)
end
local function findCubeFaces (faces, faceFromTile)
local faceFromNormal = {}
local Q = Deque.new()
local visited = {}
faces[1].normal = UP
faceFromNormal[UP] = faces[1]
makeEdges(faces[1], NORTH, FRONT)
Q:pushLast(faces[1])
while not Q:isEmpty() do
local face = Q:popFirst()
if not visited[face] then
visited[face] = true
local tile = P2D(face.tiley, face.tilex)
for _, dir in ipairs(DIRS) do
local neighbourFace = faceFromTile[tile + dir]
if neighbourFace and neighbourFace ~= face then
neighbourFace.normal = face.edges[dir]
faceFromNormal[neighbourFace.normal] = neighbourFace
makeEdges(neighbourFace, -dir, face.normal)
Q:pushLast(neighbourFace)
end
end
end
end
return faceFromNormal
end
local function facePosFromMapPos (face, mapPos)
return P2D(mapPos[1] - face.miny, mapPos[2] - face.minx)
end
local function mapPosFromFacePos (face, facePos)
return P2D(facePos[1] + face.miny, facePos[2] + face.minx)
end
local function complexQuotient (z, w)
local l = w[1] * w[1] + w[2] * w[2]
return P2D((z[1] * w[1] + z[2] * w[2]) / l, (z[2] * w[1] - z[1] * w[2]) / l)
end
local function walkCube (map, faces, faceFromNormal, instructions)
local pos = P2D(1, minxs[1])
local dir = WEST
local face = faces[1]
for _, instr in ipairs(instructions) do
if type(instr) == "number" then
while instr > 0 do
local nextFace = face
local nextDir = dir
local nextPos = pos + dir
local minx = face.minx
local maxx = face.maxx
local miny = face.miny
local maxy = face.maxy
if nextPos[2] < minx or nextPos[2] > maxx or nextPos[1] < miny or nextPos[1] > maxy then
-- we are out of bounds of the current face, we have to find our position on a new face
nextFace = faceFromNormal[face.edges[dir]]
nextDir = -nextFace.edgesDual[face.normal]
-- convert current position to position relative to top-left of the current face: (0, SIZE - 1)
local facePos = facePosFromMapPos(face, pos)
-- translate it so that the coordinates are relative to the center of the face
local facePosRelativeToFaceCenter = P2D(facePos[1] - HALF_SIZE, facePos[2] - HALF_SIZE)
-- rotate so that rotated original direction is matching the direction we'll have on the new face
local rotVec = complexQuotient(nextDir, dir)
local faceNewPos = rot(facePosRelativeToFaceCenter, rotVec)
-- now translate back to pos relative to the top-left of the face and move forward,
-- wrap around so that we end up on the opposite edge
local faceNewPosAfterMoveOnFaceTorus = P2D(
(faceNewPos[1] + HALF_SIZE + nextDir[1]) % SIZE,
(faceNewPos[2] + HALF_SIZE + nextDir[2]) % SIZE)
-- convert back to map pos treating face pos as if we're on the new face
nextPos = mapPosFromFacePos(nextFace, faceNewPosAfterMoveOnFaceTorus)
end
if map[nextPos[1]][nextPos[2]] == "#" then
break
end
face = nextFace
dir = nextDir
pos = nextPos
instr = instr - 1
end
else
dir = rot(dir, rotVecFromInstr[instr])
end
end
return pos, dir
end
local faces, faceFromTile = cutMap(map)
local faceFromNormal = findCubeFaces(faces, faceFromTile)
local answer2 = password(walkCube(map, faces, faceFromNormal, instructions))
printf("Part 2: %i\n", answer2)
return answer1, answer2, profile.finish()