Advertisement
Guest User

Untitled

a guest
Dec 23rd, 2022
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.32 KB | None | 0 0
  1. local grid = {}
  2. local elves = {}
  3. local y = 1
  4. local minx, miny, maxx, maxy = math.huge, math.huge, 0, 0
  5. for line in io.lines(arg[1]) do
  6.     local row = {}
  7.     local x = 1
  8.     for c in line:gmatch(".") do
  9.         table.insert(row, c == "#" and true or false)
  10.         if c == "#" then
  11.             table.insert(elves, {x, y, nil, nil})
  12.             maxx = math.max(maxx, x)
  13.             maxy = math.max(maxy, y)
  14.             minx = math.min(minx, x)
  15.             miny = math.min(miny, y)
  16.         end
  17.         x = x + 1
  18.     end
  19.     table.insert(grid, row)
  20.     y = y + 1
  21. end
  22.  
  23. local prio = {
  24.     {{{0,-1}, {1,-1}, {-1,-1}}, 0, -1},
  25.     {{{0,1}, {1,1}, {-1,1}}, 0, 1},
  26.     {{{-1,0}, {-1,1}, {-1,-1}}, -1, 0},
  27.     {{{1,0}, {1,1}, {1,-1}}, 1, 0},
  28. }
  29.  
  30. local function isAlone(x, y)
  31.     for dy = -1, 1 do
  32.         for dx = -1, 1 do
  33.             if not (dy == 0 and dx == 0) then
  34.                 grid[y+dy] = grid[y+dy] or {}
  35.                 if grid[y+dy][x+dx] then return false end
  36.             end
  37.         end
  38.     end
  39.     return true
  40. end
  41.  
  42. local function move(x, y)
  43.     for _, dir in ipairs(prio) do
  44.         local valid = true
  45.         for _, v in ipairs(dir[1]) do
  46.             grid[y+v[2]] = grid[y+v[2]] or {}
  47.             if grid[y+v[2]][x+v[1]] then valid = false break end
  48.         end
  49.         if valid then return x+dir[2], y+dir[3] end
  50.     end
  51.     return x, y
  52. end
  53.  
  54. local function step(elves)
  55.     local moved = false
  56.     local newElves = {}
  57.     local chosen = {}
  58.     for i, elf in ipairs(elves) do
  59.         if not isAlone(elf[1], elf[2]) then
  60.             local newx, newy = move(elf[1], elf[2])
  61.             chosen[newy] = chosen[newy] or {}
  62.             chosen[newy][newx] = (chosen[newy][newx] or 0) + 1
  63.             elves[i][3] = newx
  64.             elves[i][4] = newy
  65.         end
  66.     end
  67.     for _, elf in ipairs(elves) do
  68.         if (not elf[3]) or chosen[elf[4]][elf[3]] > 1 then
  69.             table.insert(newElves, {elf[1], elf[2], nil, nil})
  70.         elseif chosen[elf[4]][elf[3]] == 1 then
  71.             moved = true
  72.             grid[elf[2]][elf[1]] = false
  73.             grid[elf[4]][elf[3]] = true
  74.             minx = math.min(minx, elf[3])
  75.             miny = math.min(miny, elf[4])
  76.             maxx = math.max(maxx, elf[3])
  77.             maxy = math.max(maxy, elf[4])
  78.             table.insert(newElves, {elf[3], elf[4], nil, nil})
  79.         end
  80.     end
  81.     table.insert(prio, table.remove(prio,1))
  82.     return newElves, moved
  83. end
  84.  
  85. local moved
  86. for _ = 1, 100000 do
  87.     elves, moved = step(elves)
  88.     if _ == 10 then
  89.         local count = 0
  90.         for y = miny, maxy do
  91.             for x = minx, maxx do
  92.                 if not grid[y][x] then count = count + 1 end
  93.             end
  94.         end
  95.         print(count)
  96.     end
  97.     if not moved then print(_) break end
  98. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement