Advertisement
Guest User

Untitled

a guest
Dec 10th, 2022
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.35 KB | None | 0 0
  1. local program = {}
  2. for line in io.lines(arg[1]) do
  3.     local code, amount = line:match("(%w+) (%-?%d+)")
  4.     if code and amount then
  5.         table.insert(program, { code = code, amount = tonumber(amount) })
  6.     else
  7.         table.insert(program, { code = "noop", amount = nil })
  8.     end
  9. end
  10.  
  11. local steps = {[20] = true, [60] = true, [100] = true, [140] = true, [180] = true, [220] = true}
  12. local grid = {}
  13. local X = 1
  14. local cycle = 1
  15. local ind = 1
  16. local total = 0
  17. while cycle <= 240 do
  18.     if program[ind].code == "noop" then
  19.         local row = math.floor((cycle - 1) / 40)
  20.         local col = cycle - 40 * row - 1
  21.         grid[row] = grid[row] or {}
  22.         if (col == X - 1) or (col == X) or (col == X + 1) then
  23.             grid[row][col] = "#"
  24.         else
  25.             grid[row][col] = "."
  26.         end
  27.         if steps[cycle] then
  28.             total = total + cycle * X
  29.         end
  30.         cycle = cycle + 1
  31.         ind = ind + 1
  32.     else
  33.         for _ = 1, 2 do
  34.             local row = math.floor((cycle - 1) / 40)
  35.             local col = cycle - 40 * row - 1
  36.             grid[row] = grid[row] or {}
  37.             if (col == X - 1) or (col == X) or (col == X + 1) then
  38.                 grid[row][col] = "#"
  39.             else
  40.                 grid[row][col] = "."
  41.             end
  42.             if steps[cycle] then
  43.                 total = total + cycle * X
  44.             end
  45.             cycle = cycle + 1
  46.         end
  47.         X = X + program[ind].amount
  48.         ind = ind + 1
  49.     end
  50. end
  51. print(total)
  52.  
  53. for y = 0, 5 do
  54.     local s = ""
  55.     for x = 0, 39 do
  56.         s = s .. grid[y][x]
  57.     end
  58.     print(s)
  59. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement