from itertools import combinations # G = Pier at G flag # T = Tree between A/B flag # C = Cauldron in C flag # E = Rock near E flag # H = Bricks near H flag # B = Pagoda near B flag # W = Waterfall # O = Original lights on light_table = [ "GT", # upper-left corner "CE", "HBW", "HBW", "EW", "EW", "CE", # upper-right corner "W", "EW", "HT", "BC", # lower-right corner "BC", "GT", "W", "CE", "OGW", "BC", # lower-left corner "OGW", "W", "OGW", ] def solve_all(): # brute force all combinations flags = "GTCEHBW" for length in range(len(flags)): for light_combo in combinations(flags, length+1): if solve(light_table, light_combo): print "WINNER!", ",".join(light_combo) return print "No combination found :(" def solve(light_table, light_combo): output_light_table = [ False ] * len(light_table) # Fill in original lights for i, lights in enumerate(light_table): if "O" in lights: output_light_table[i] = True # Toggle lights for light in light_combo: for i, lights in enumerate(light_table): if light in lights: output_light_table[i] = not output_light_table[i] # Fill in single lights for i, lights in enumerate(light_table): if len(lights) == 1: output_light_table[i] = True return all(output_light_table) if __name__ == "__main__": solve_all()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question