-
-
Save anonymous/d4b5ad99cb3b9522f5da to your computer and use it in GitHub Desktop.
Solve a discrete constraint satisfaction problem to open the portcullises at the end of the Soultaker's Pit in Avernum 5.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
from collections import Counter | |
from constraint import * # <https://pypi.python.org/pypi/python-constraint> | |
class S(object): | |
Red = "red" | |
Blue = "blue" | |
Green = "green" | |
Nonsparkling = "nonsparkling" | |
Absent = "absent" | |
class L(object): | |
NW = "NW" | |
NE = "NE" | |
SW = "SW" | |
SE = "SE" | |
states = (S.Red, S.Blue, S.Green, S.Nonsparkling, S.Absent) | |
sparkling = (S.Red, S.Blue, S.Green) | |
locations = (L.NW, L.NE, L.SW, L.SE) | |
p = Problem() | |
for location in locations: | |
p.addVariable(location, states) | |
# At least one stand to the north has no orb. | |
p.addConstraint( | |
lambda nw, ne: (nw == S.Absent) or (ne == S.Absent), | |
(L.NW, L.NE)) | |
# A western orb has red sparkles. | |
p.addConstraint( | |
lambda nw, sw: (nw == S.Red) or (sw == S.Red), | |
(L.NW, L.SW)) | |
# There are no blue sparkles. | |
p.addConstraint( | |
NotInSetConstraint({S.Blue})) | |
# A sparkling orb to the north is not red. | |
p.addConstraint( | |
lambda nw, ne: (nw in sparkling and not nw == S.Red) or (ne in sparkling and not ne == S.Red), | |
(L.NW, L.NE)) | |
# No sparkles are the same color | |
p.addConstraint( | |
lambda *args: all(Counter(args)[state] <= 1 for state in sparkling), | |
locations) | |
# Two orbs are sparkling on the diagonal | |
p.addConstraint( | |
lambda nw, ne, sw, se: (nw in sparkling and se in sparkling) or (ne in sparkling and sw in sparkling), | |
(L.NW, L.NE, L.SW, L.SE)) | |
print(p.getSolutions()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment