#!/usr/bin/env python3 ''' reveal the connected zeros of a minesweeper board board is 1-indexed and square ''' def is_onboard(c): (a,b) = c return a >= 1 and b >= 1 and a <= BOARD and b <= BOARD def adjacents(c): (a,b) = c adjs = [ (a+x, b+y) for x in range(-1,2) for y in range(-1,2) ] return [ x for x in adjs if is_onboard(x) ] def mine_count(c): return len([ x for x in adjacents(c) if x in MINES ]) def ls_zeros(unvisited, visited): unvisited = [ x for x in unvisited if x not in visited and mine_count(x) == 0 ] if unvisited == []: return sorted(visited) hd, *tl = unvisited return ls_zeros(tl + adjacents(hd), visited + [hd]) BOARD = 3 MINES = [ (1,1) ] assert ls_zeros([(1,1)], []) == [] assert ls_zeros([(2,1)], []) == [] assert ls_zeros([(1,3)], []) == [(1, 3), (2, 3), (3, 1), (3, 2), (3, 3)] MINES = [ (1,1), (3,1) ] assert ls_zeros([(1,3)], []) == [(1, 3), (2, 3), (3, 3)] assert ls_zeros([(2,3)], []) == [(1, 3), (2, 3), (3, 3)] BOARD = 5 MINES = [ (1,3), (2,3), (3,3), (4,3), (5,3) ] assert ls_zeros([(5,5)], []) == [(1, 5), (2, 5), (3, 5), (4, 5), (5, 5)]