Advertisement
Guest User

Untitled

a guest
Dec 18th, 2022
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.93 KB | None | 0 0
  1. local pieces = {
  2.     [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 },
  3.     [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 },
  4.     [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 },
  5.     [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 },
  6.     [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},
  7. }
  8. local N = 5
  9.  
  10. local inp = io.open(arg[1]):read("*l")
  11. local inptbl, i = {}, 0
  12. for c in inp:gmatch(".") do
  13.     inptbl[i] = c
  14.     i = i + 1
  15. end
  16.  
  17. local occupied = {}
  18. occupied[0] = {}
  19. for _ = 1,7 do occupied[0][_] = true end
  20. local maxy = 0
  21. i = 0
  22. local pind = 0
  23.  
  24. local function turn(i, pieceInd)
  25.     local piece = pieces[pieceInd % N]
  26.     local x, y = 3, maxy + 3 + piece.incr
  27.     while true do
  28.         local canMove = true
  29.         if inptbl[i % #inp] == ">" then
  30.             for _, move in ipairs(piece.right) do
  31.                 occupied[y+move[2]] = occupied[y+move[2]] or {}
  32.                 if occupied[y+move[2]][x+move[1]+1] or x + move[1] + 1 > 7 then canMove = false break end
  33.             end
  34.             if canMove then x = x + 1 end
  35.         else
  36.             for _, move in ipairs(piece.left) do
  37.                 occupied[y+move[2]] = occupied[y+move[2]] or {}
  38.                 if occupied[y+move[2]][x+move[1]-1] or x + move[1] - 1 < 1 then canMove = false break end
  39.             end
  40.             if canMove then x = x - 1 end
  41.         end
  42.         i = i + 1
  43.         for _, move in ipairs(piece.bottom) do
  44.             occupied[y+move[2]-1] = occupied[y+move[2]-1] or {}
  45.             if occupied[y+move[2]-1][x+move[1]] then
  46.                 for _, v in ipairs(piece.all) do
  47.                     occupied[y+v[2]] = occupied[y+v[2]] or {}
  48.                     occupied[y+v[2]][x+v[1]] = true
  49.                 end
  50.                 maxy = math.max(maxy, y)
  51.                 return i, pieceInd + 1
  52.             end
  53.         end
  54.         y = y - 1
  55.     end
  56. end
  57.  
  58. local states = {}
  59. local cycleHeight, cycleSteps, currInd
  60. local ind = 1
  61. for n = 1, 10000 do
  62.     local rocks = ""
  63.     for y = 0, -30, -1 do
  64.         occupied[maxy+y] = occupied[maxy+y] or {}
  65.         for x = 1, 7 do
  66.             rocks = rocks .. (occupied[maxy+y][x] and "#" or ".")
  67.         end
  68.     end
  69.     local state = i % #inp .. "|" .. pind % N .. "|" .. rocks
  70.     if states[state] then
  71.         cycleHeight = maxy - states[state][2]
  72.         cycleSteps = n - states[state][1]
  73.         currInd = states[state][3]
  74.         local remCycles = math.floor((1000000000000 - n) / cycleSteps)
  75.         local leftovers = 1000000000000 - (remCycles * cycleSteps + n) + 1
  76.         for _, s in pairs(states) do
  77.             if currInd + leftovers == s[3] then
  78.                 print(maxy + cycleHeight * remCycles + (s[2] - states[state][2]))
  79.                 break
  80.             end
  81.         end
  82.         break
  83.     end
  84.     states[state] = {n, maxy, ind}
  85.     ind = ind + 1
  86.     i, pind = turn(i, pind)
  87.     if n == 2022 then print(maxy) end
  88. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement