Editor

Sudoku Clash Finder

#310 · Games and puzzles · Intermediate · 70 XP · about 15 min

A sudoku app paints a square red when its digit breaks the rules. The board is a list of 9 strings of 9 characters each: a digit from '1' to '9' for a filled square and '.' for an empty one. A digit clashes when the same digit appears again in its row, in its column or in its 3 by 3 box. The board is split into nine boxes: rows 0 to 2, 3 to 5 and 6 to 8, crossed with columns 0 to 2, 3 to 5 and 6 to 8.

Write clashes(board) that returns a list of (row, col) tuples, one for every square holding a digit that clashes with at least one other square, sorted by row and then by column (rows and columns are numbered from 0). Each square appears once, however many clashes it is part of. An empty list means the board breaks no rule so far. Empty squares never clash, and you do not have to check whether the puzzle can still be solved.

Examples

board = [
    '53..7....',
    '6..195...',
    '.98....6.',
    '8...6...3',
    '4..8.3..1',
    '7...2...6',
    '.6....28.',
    '...419..5',
    '....8..79',
]
clashes(board)  # returns []

If the first row were '539.7....' instead, the new 9 at (0, 2) would share its box with the 9 at (2, 1), and the answer would be [(0, 2), (2, 1)].

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.