Skip to content

Instantly share code, notes, and snippets.

@lancewalton
lancewalton / Chess
Created August 31, 2013 21:14
Two solutions to the problem of finding all placements of some collection of chess pieces on a chess board of specified size such that no piece is under threat from any other. The problem was recently posed to me by a friend. The 'optimised' solution is significantly faster that the 'unoptimised' one. For example, on my laptop, the n-queens prob…
package chess
// Everything from this line down to the cut is common to the 'unoptimised' and 'optimised' solutions
case class Location(row: Int, column: Int)
trait Piece {
def canAttackLocation(from: Location, to: Location): Boolean
}
case object Rook extends Piece {
@lancewalton
lancewalton / Sudoku.scala
Created November 2, 2011 22:21
A Sudoku solver in Scala
package sudoku
case class Location(row: Int, col: Int) {
def areValuesMutuallyExclusive(other: Location, size: Int) =
isInSameRow(other) ||
isInSameColumn(other) ||
isInSameSmallSquare(other, size)
private def isInSameRow(other: Location): Boolean = row == other.row