Sixteen corners
[]
Jake Wharton
fun validTables(): Sequence<IntArray> = sequence { val state = IntArray(16) - suspend fun SequenceScope<IntArray>.placeCorner(index: Int) { + suspend fun SequenceScope<IntArray>.placeCorner(index: Int, used: Int) { if (index == 16) { yield(state.clone()) return } for (corner in 0 until 16) { + if (used.hasBit(corner)) continue + // TODO validate corner fits here! state[index] = corner - placeCorner(index + 1) + placeCorner(index + 1, used.withBit(corner)) } } - placeCorner(0) + placeCorner(0, 0) } + +fun Int.hasBit(bit: Int) = ((1 shl bit) and this) != 0 +fun Int.withBit(bit: Int) = (1 shl bit) or this If there is a corner to the left of the current index in the 4x4 grid, this corner can only have a left segment if that corner has a right segment.