Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local pieces = {
- [0] = { left = {{0,0}}, right = {{3,0}}, bottom = {{0,0}, {1,0}, {2,0}, {3,0}}, all = {{0,0}, {1,0}, {2,0}, {3,0}}, incr = 1 },
- [1] = { left = {{1,0}, {0,-1}, {1,-2}}, right = {{1,0}, {2,-1}, {1,-2}}, bottom = {{1,-2}, {0,-1}, {2,-1}}, all = {{0,-1}, {1,0}, {1,-1}, {2,-1}, {1,-2}}, incr = 3 },
- [2] = { left = {{0,-2}, {2,0}, {2,-1}}, right = {{2,0}, {2,-1}, {2,-2}}, bottom = {{0,-2}, {1,-2}, {2,-2}}, all = {{0,-2}, {1,-2}, {2,-2}, {2,-1}, {2,0}}, incr = 3 },
- [3] = { left = {{0,0}, {0,-1}, {0,-2}, {0,-3}}, right = {{0,0}, {0,-1}, {0,-2}, {0,-3}}, bottom = {{0,-3}}, all = {{0,0}, {0,-1}, {0,-2}, {0,-3}}, incr = 4 },
- [4] = { left = {{0,0}, {0,-1}}, right = {{1,0}, {1,-1}}, bottom = {{0,-1}, {1,-1}}, all = {{0,0}, {1,0}, {0,-1}, {1,-1}}, incr = 2},
- }
- local N = 5
- local inp = io.open(arg[1]):read("*l")
- local inptbl, i = {}, 0
- for c in inp:gmatch(".") do
- inptbl[i] = c
- i = i + 1
- end
- local occupied = {}
- occupied[0] = {}
- for _ = 1,7 do occupied[0][_] = true end
- local maxy = 0
- i = 0
- local pind = 0
- local function turn(i, pieceInd)
- local piece = pieces[pieceInd % N]
- local x, y = 3, maxy + 3 + piece.incr
- while true do
- local canMove = true
- if inptbl[i % #inp] == ">" then
- for _, move in ipairs(piece.right) do
- occupied[y+move[2]] = occupied[y+move[2]] or {}
- if occupied[y+move[2]][x+move[1]+1] or x + move[1] + 1 > 7 then canMove = false break end
- end
- if canMove then x = x + 1 end
- else
- for _, move in ipairs(piece.left) do
- occupied[y+move[2]] = occupied[y+move[2]] or {}
- if occupied[y+move[2]][x+move[1]-1] or x + move[1] - 1 < 1 then canMove = false break end
- end
- if canMove then x = x - 1 end
- end
- i = i + 1
- for _, move in ipairs(piece.bottom) do
- occupied[y+move[2]-1] = occupied[y+move[2]-1] or {}
- if occupied[y+move[2]-1][x+move[1]] then
- for _, v in ipairs(piece.all) do
- occupied[y+v[2]] = occupied[y+v[2]] or {}
- occupied[y+v[2]][x+v[1]] = true
- end
- maxy = math.max(maxy, y)
- return i, pieceInd + 1
- end
- end
- y = y - 1
- end
- end
- local states = {}
- local cycleHeight, cycleSteps, currInd
- local ind = 1
- for n = 1, 10000 do
- local rocks = ""
- for y = 0, -30, -1 do
- occupied[maxy+y] = occupied[maxy+y] or {}
- for x = 1, 7 do
- rocks = rocks .. (occupied[maxy+y][x] and "#" or ".")
- end
- end
- local state = i % #inp .. "|" .. pind % N .. "|" .. rocks
- if states[state] then
- cycleHeight = maxy - states[state][2]
- cycleSteps = n - states[state][1]
- currInd = states[state][3]
- local remCycles = math.floor((1000000000000 - n) / cycleSteps)
- local leftovers = 1000000000000 - (remCycles * cycleSteps + n) + 1
- for _, s in pairs(states) do
- if currInd + leftovers == s[3] then
- print(maxy + cycleHeight * remCycles + (s[2] - states[state][2]))
- break
- end
- end
- break
- end
- states[state] = {n, maxy, ind}
- ind = ind + 1
- i, pind = turn(i, pind)
- if n == 2022 then print(maxy) end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement