Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local rock = {}
- local sand = {}
- local miny = 0
- local minx, maxx = math.huge, 0
- for line in io.lines(arg[1]) do
- local begin = true
- local x1, y1
- for pair in line:gmatch("%d+,%d+") do
- if begin then
- x1, y1 = pair:match("(%d+),(%d+)")
- x1, y1 = tonumber(x1), tonumber(y1)
- else
- local x2, y2 = pair:match("(%d+),(%d+)")
- x2, y2 = tonumber(x2), tonumber(y2)
- for y = math.min(y1, y2), math.max(y1, y2) do
- if y > miny then miny = y end
- rock[y] = rock[y] or {}
- for x = math.min(x1, x2), math.max(x1, x2) do
- if x > maxx then maxx = x end
- if x < minx then minx = x end
- rock[y][x] = true
- end
- end
- x1 = x2
- y1 = y2
- end
- begin = false
- end
- end
- rock[miny+2] = {}
- for x = minx - 1000, maxx + 1000 do rock[miny+2][x] = true end
- local function fall(count, part2)
- local x, y = 500, 0
- while true do
- rock[y+1] = rock[y+1] or {}
- sand[y+1] = sand[y+1] or {}
- if rock[y+1][x] or sand[y+1][x] then
- if rock[y+1][x-1] or sand[y+1][x-1] then
- if rock[y+1][x+1] or sand[y+1][x+1] then
- sand[y] = sand[y] or {}
- sand[y][x] = true
- return count + 1, false
- else
- y = y + 1
- x = x + 1
- end
- else
- y = y + 1
- x = x - 1
- end
- else
- y = y + 1
- if (not part2) and y > miny then return count, true end
- end
- end
- end
- local count = 0
- local stop = false
- while not stop do
- count, stop = fall(count)
- end
- print(count)
- sand = {}
- sand[0] = {}
- sand[0][500] = false
- count = 0
- while true do
- count = fall(count, true)
- if sand[0][500] then print(count) break end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement