patternpythonMinor
Alberi puzzle creator - follow-up
Viewed 0 times
creatorpuzzlefollowalberi
Problem
This is a follow-up to this question.
Using his
```
"""Solver for an Alberi puzzle using exact cover
"""
from collections import Iterator
from itertools import product
from ExactCoverExt import ExactCover
class Alberi(Iterator):
"""An iterator that yields the solutions to an Alberi problem.
>>> puzzle = '''
... aabbbccccccc
... accccccdddee
... accfcfdddgee
... afffffddggee
... aafffdddggee
... afffddddgggg
... hffffdiijjjj
... hffffiiikkkk
... hhhfiiiiiklk
... hhhffiilllll
... hhhffiilllll
... hhhfffilllll
... '''
>>> asolver = Alberi(*Alberi.multiline_to_solver_params(puzzle))
>>> print(asolver.decode_solution(next(asolver)))
>>> quit()
aaBbBccccccc
acccccCdddeE
acCfcfdddGee
AfffffDdggee
aafffdddGgEe
AfffDdddgggg
hffffdiiJjJj
hffFfIiikkkk
hhhfiiiiiKlK
hHhffiiLllll
hhhFfIilllll
hHhfffiLllll
"""
WHITESPACE = ' '
PERIOD = '.'
def __init__(self, puzzle, m, n, per_row=2, per_column=2, per_region=2):
"""Construct an Alberi instance.
Required argument:
puzzle -- The puzzle to solve, in the form of a string of n*m
letters indicating the regions. The whitespace ' ' can be used
as a placeholder for a cell with no region assigned. The
period '.' can be used to force an empty cell in the solution.
n -- Number of rows
m -- Number of columns
Optional arguments:
per_row -- Number of trees per row. (Default: 2.)
per_column -- Number of trees per column. (Default: 2.)
per_region -- Number of trees per region. (Default: 2.)
"""
self.m = m
self.n = n
self.puzzle = puzzle
WHITESPACE = Alberi.WHITESPACE
PERIOD = Alberi.PERIOD
WP = WHITESPACE+PERIOD
regi
Using his
ExactCover class, I slightly modified Garth Rees' Alberi class to cope with regionless schemes and forced void cells:```
"""Solver for an Alberi puzzle using exact cover
"""
from collections import Iterator
from itertools import product
from ExactCoverExt import ExactCover
class Alberi(Iterator):
"""An iterator that yields the solutions to an Alberi problem.
>>> puzzle = '''
... aabbbccccccc
... accccccdddee
... accfcfdddgee
... afffffddggee
... aafffdddggee
... afffddddgggg
... hffffdiijjjj
... hffffiiikkkk
... hhhfiiiiiklk
... hhhffiilllll
... hhhffiilllll
... hhhfffilllll
... '''
>>> asolver = Alberi(*Alberi.multiline_to_solver_params(puzzle))
>>> print(asolver.decode_solution(next(asolver)))
>>> quit()
aaBbBccccccc
acccccCdddeE
acCfcfdddGee
AfffffDdggee
aafffdddGgEe
AfffDdddgggg
hffffdiiJjJj
hffFfIiikkkk
hhhfiiiiiKlK
hHhffiiLllll
hhhFfIilllll
hHhfffiLllll
"""
WHITESPACE = ' '
PERIOD = '.'
def __init__(self, puzzle, m, n, per_row=2, per_column=2, per_region=2):
"""Construct an Alberi instance.
Required argument:
puzzle -- The puzzle to solve, in the form of a string of n*m
letters indicating the regions. The whitespace ' ' can be used
as a placeholder for a cell with no region assigned. The
period '.' can be used to force an empty cell in the solution.
n -- Number of rows
m -- Number of columns
Optional arguments:
per_row -- Number of trees per row. (Default: 2.)
per_column -- Number of trees per column. (Default: 2.)
per_region -- Number of trees per region. (Default: 2.)
"""
self.m = m
self.n = n
self.puzzle = puzzle
WHITESPACE = Alberi.WHITESPACE
PERIOD = Alberi.PERIOD
WP = WHITESPACE+PERIOD
regi
Solution
I wonder why
Alternatively, if you don't need
starting_grid returns only the solution and not the puzzle. I think the locations of the forced empty cells would be useful: you could first grow regions into the whitespace, without checking uniqueness of solution at this stage. The forced cells maintain uniqueness until you start joining them into regions.Alternatively, if you don't need
starting_grid to return the puzzle, you could take advantage of the random parameter of the solver and return a random solution.Context
StackExchange Code Review Q#109548, answer score: 3
Revisions (0)
No revisions yet.