Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.66 KB | None | 0 0
  1. require("utils")
  2.  
  3. local input = getinput()
  4.  
  5. local grid = {}
  6. local gy = 0
  7. parselines(input, function(line)
  8.     local t = {}
  9.     grid[gy] = t
  10.     gy = gy + 1
  11.     local x = 0
  12.     line:gsub(".", function(c)
  13.         t[x] = c == "#" and 1 or 0
  14.         x = x + 1
  15.     end)
  16. end)
  17.  
  18. local function visible(x, y, x2, y2)
  19.     local dx, dy = x2-x, y2-y
  20.     for i = 2, math.max(dx, dy, -dx, -dy) do
  21.         while dx%i == 0 and dy%i == 0 do
  22.             dx, dy = dx/i, dy/i
  23.         end
  24.     end
  25.     local cx, cy = x, y
  26.     while true do
  27.         cx, cy = cx + dx, cy + dy
  28.         if cx == x2 and cy == y2 then break end
  29.         if grid[cy][cx] == 1 then
  30.             return false
  31.         end
  32.     end
  33.     return true
  34. end
  35.  
  36. local function getVisible(x, y)
  37.     local list = {}
  38.     for y2 = 0, #grid do
  39.         for x2 = 0, #grid[y2] do
  40.             if grid[y2][x2] == 1 and not (x2 == x and y2 == y) then
  41.                 if visible(x, y, x2, y2) then
  42.                     list[#list+1] = {x = x2, y = y2}
  43.                 end
  44.             end
  45.         end
  46.     end
  47.     return list
  48. end
  49.  
  50. local score, best = -1
  51. for y = 0, #grid do
  52.     for x = 0, #grid[y] do
  53.         if grid[y][x] == 1 then
  54.             local count = #getVisible(x, y)
  55.             if count > score then
  56.                 score, best = count, {x = x, y = y}
  57.             end
  58.         end
  59.     end
  60. end
  61. print(score, best.x, best.y)
  62.  
  63. local x, y = best.x, best.y
  64. local destroyed = 0
  65. while true do
  66.     local currentPass = getVisible(x, y)
  67.     for _, p in pairs(currentPass) do
  68.         p.gx, p.gy = p.x, p.y
  69.         p.x, p.y = p.x-x, p.y-y
  70.     end
  71.     local function angle(p)
  72.         return math.atan2(p.x, -p.y)%(math.pi*2)
  73.     end
  74.     table.sort(currentPass, function(a, b)
  75.         return angle(a) < angle(b)
  76.     end)
  77.     for _, p in pairs(currentPass) do
  78.         destroyed = destroyed + 1
  79.         if destroyed == 200 then
  80.             print(p.gx*100 + p.gy)
  81.             return
  82.         end
  83.         grid[p.gy][p.gx] = 0
  84.     end
  85. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement