Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require("utils")
- local input = getinput()
- local grid = {}
- local gy = 0
- parselines(input, function(line)
- local t = {}
- grid[gy] = t
- gy = gy + 1
- local x = 0
- line:gsub(".", function(c)
- t[x] = c == "#" and 1 or 0
- x = x + 1
- end)
- end)
- local function visible(x, y, x2, y2)
- local dx, dy = x2-x, y2-y
- for i = 2, math.max(dx, dy, -dx, -dy) do
- while dx%i == 0 and dy%i == 0 do
- dx, dy = dx/i, dy/i
- end
- end
- local cx, cy = x, y
- while true do
- cx, cy = cx + dx, cy + dy
- if cx == x2 and cy == y2 then break end
- if grid[cy][cx] == 1 then
- return false
- end
- end
- return true
- end
- local function getVisible(x, y)
- local list = {}
- for y2 = 0, #grid do
- for x2 = 0, #grid[y2] do
- if grid[y2][x2] == 1 and not (x2 == x and y2 == y) then
- if visible(x, y, x2, y2) then
- list[#list+1] = {x = x2, y = y2}
- end
- end
- end
- end
- return list
- end
- local score, best = -1
- for y = 0, #grid do
- for x = 0, #grid[y] do
- if grid[y][x] == 1 then
- local count = #getVisible(x, y)
- if count > score then
- score, best = count, {x = x, y = y}
- end
- end
- end
- end
- print(score, best.x, best.y)
- local x, y = best.x, best.y
- local destroyed = 0
- while true do
- local currentPass = getVisible(x, y)
- for _, p in pairs(currentPass) do
- p.gx, p.gy = p.x, p.y
- p.x, p.y = p.x-x, p.y-y
- end
- local function angle(p)
- return math.atan2(p.x, -p.y)%(math.pi*2)
- end
- table.sort(currentPass, function(a, b)
- return angle(a) < angle(b)
- end)
- for _, p in pairs(currentPass) do
- destroyed = destroyed + 1
- if destroyed == 200 then
- print(p.gx*100 + p.gy)
- return
- end
- grid[p.gy][p.gx] = 0
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement