function tick2(occupied) {
const next = new Set(occupied)
const counts = {}
for (const seat of occupied) {
const [x0, y0] = seat.split(',').map(Number)
for (const [dx, dy] of directions()) {
let p = [x0, y0]
do {
p[0] += dx
p[1] += dy
if (seats.has(String(p))) {
counts[p] = (counts[p] || 0) + 1
break
}
} while (p[0] >= 0 && p[0] < grid.length && p[1] >= 0 && p[1] < grid[0].length)
}
}
for (const seat of seats) {
const neighbors = counts[seat] || 0
if (!occupied.has(seat) && neighbors === 0) {
next.add(seat)
} else if (occupied.has(seat) && neighbors >= 5) {
next.delete(seat)
}
}
return next
}