Skip to content

Instantly share code, notes, and snippets.

@glukianets
glukianets / 8Queens.swift
Created August 15, 2024 15:38
Naive 8Queens puzzle solution with bitmasks
func queen(atFile file: Int, rank: Int) -> UInt64 {
0b00000001_00000001_00000001_00000001_00000001_00000001_00000001_00000001 << (file) |
0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_11111111 << (rank * 8) |
0b10000000_01000000_00100000_00010000_00001000_00000100_00000010_00000001 >> ((file - rank) * 8) |
0b00000001_00000010_00000100_00001000_00010000_00100000_01000000_10000000 >> ((7 - file - rank) * 8)
}
func occupiedIndices(in board: UInt64) -> some Sequence<(file: Int, rank: Int)> {
sequence(state: (board, 0)) { state in
let offset = state.0.trailingZeroBitCount