Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local file = io.open(arg[1])
- local grid = {}
- while true do
- local line = file:read("*l")
- if line == "" then break end
- local row = {}
- for c in line:gmatch(".") do table.insert(row, c) end
- table.insert(grid, row)
- end
- local route = file:read("*l")
- local dirs = {["R"] = {1,0}, ["L"] = {-1,0}, ["U"] = {0,-1}, ["D"] = {0,1}}
- local turnR = {["R"] = "D", ["D"] = "L", ["L"] = "U", ["U"] = "R"}
- local turnL = {["D"] = "R", ["L"] = "D", ["U"] = "L", ["R"] = "U"}
- local score = {["R"] = 0, ["D"] = 1, ["L"] = 2, ["U"] = 3}
- local function warp(dir, x, y)
- if dir == "R" then
- for i, c in ipairs(grid[y]) do
- if c == "#" then return false end
- if c == "." then return i, y end
- end
- elseif dir == "L" then
- for i = #grid[y], 1, -1 do
- if grid[y][i] == "#" then return false end
- if grid[y][i] == "." then return i, y end
- end
- elseif dir == "U" then
- for i = #grid, 1, -1 do
- if grid[i][x] == "#" then return false end
- if grid[i][x] == "." then return x, i end
- end
- elseif dir == "D" then
- for i, row in ipairs(grid) do
- if row[x] == "#" then return false end
- if row[x] == "." then return x, i end
- end
- end
- end
- local function getSquare(x, y)
- if x > 50 and x <= 100 and y >= 1 and y <= 50 then return 1 end
- if x > 100 and x <= 150 and y >= 1 and y <= 50 then return 2 end
- if x > 50 and x <= 100 and y > 50 and y <= 100 then return 3 end
- if x > 50 and x <= 100 and y > 100 and y <= 150 then return 4 end
- if x >= 1 and x <= 50 and y > 100 and y <= 150 then return 5 end
- if x >= 1 and x <= 50 and y > 150 and y <= 200 then return 6 end
- return nil
- end
- local function warp2(zone, face, x, y)
- if zone == 1 then
- if face == "L" then
- if grid[151-y][1] == "#" then
- return false
- else
- return "R", 1, 151-y
- end
- elseif face == "U" then
- if grid[x+100][1] == "#" then
- return false
- else
- return "R", 1, x+100
- end
- end
- elseif zone == 2 then
- if face == "U" then
- if grid[200][x-100] == "#" then
- return false
- else
- return "U", x-100, 200
- end
- elseif face == "R" then
- if grid[151-y][100] == "#" then
- return false
- else
- return "L", 100, 151-y
- end
- elseif face == "D" then
- if grid[x-50][100] == "#" then
- return false
- else
- return "L", 100, x-50
- end
- end
- elseif zone == 3 then
- if face == "R" then
- if grid[50][y+50] == "#" then
- return false
- else
- return "U", y+50, 50
- end
- elseif face == "L" then
- if grid[101][y-50] == "#" then
- return false
- else
- return "D", y-50, 101
- end
- end
- elseif zone == 4 then
- if face == "R" then
- if grid[151-y][150] == "#" then
- return false
- else
- return "L", 150, 151-y
- end
- elseif face == "D" then
- if grid[x+100][50] == "#" then
- return false
- else
- return "L", 50, x+100
- end
- end
- elseif zone == 5 then
- if face == "U" then
- if grid[x+50][51] == "#" then
- return false
- else
- return "R", 51, x+50
- end
- elseif face == "L" then
- if grid[151-y][51] == "#" then
- return false
- else
- return "R", 51, 151-y
- end
- end
- elseif zone == 6 then
- if face == "L" then
- if grid[1][y-100] == "#" then
- return false
- else
- return "D", y-100, 1
- end
- elseif face == "D" then
- if grid[1][x+100] == "#" then
- return false
- else
- return "D", x+100, 1
- end
- elseif face == "R" then
- if grid[150][y-100] == "#" then
- return false
- else
- return "U", y-100, 150
- end
- end
- end
- end
- local function solve(part2)
- local y, x = 1
- local face = "R"
- for i, c in ipairs(grid[1]) do if c == "." then x = i break end end
- for n, rotate in route:gmatch("(%d+)(%w?)") do
- n = tonumber(n)
- for _ = 1, n do
- if (not grid[y+dirs[face][2]]) or (not grid[y+dirs[face][2]][x+dirs[face][1]]) or (grid[y+dirs[face][2]][x+dirs[face][1]] == " ") then
- if part2 then
- if warp2(getSquare(x,y), face, x, y) then
- face, x, y = warp2(getSquare(x,y), face, x, y)
- else
- break
- end
- else
- if warp(face, x, y) then
- x, y = warp(face, x, y)
- else
- break
- end
- end
- elseif grid[y+dirs[face][2]][x+dirs[face][1]] == "#" then
- break
- else
- x = x + dirs[face][1]
- y = y + dirs[face][2]
- end
- end
- if rotate ~= "" then
- if rotate == "L" then
- face = turnL[face]
- else
- face = turnR[face]
- end
- end
- end
- return 1000*y + 4*x + score[face]
- end
- print(solve())
- print(solve(true))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement