Created
January 26, 2018 19:21
-
-
Save rvcx/61e4306db5e0f0c385c08e1ee44029a8 to your computer and use it in GitHub Desktop.
Code for 2018-01-26 riddler classic; https://fivethirtyeight.com/features/how-often-does-the-senate-vote-in-palindromes/
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 python3 | |
import argparse | |
import sys | |
import collections | |
def main(): | |
areas = { 'a' : 12, | |
'b' : 12, | |
'c' : 12, | |
'd' : 6, | |
'e' : 3, | |
'f' : 6, | |
'g' : 21, | |
'h' : 6, | |
'i' : 12, | |
'j' : 3, | |
'k' : 9, | |
'l' : 6, | |
'm' : 12, | |
'n' : 24 } | |
adjacencies = { 'a' : 'bcd', | |
'b' : 'ac', | |
'c' : 'abfeg', | |
'd' : 'agm', | |
'e' : 'cfg', | |
'f' : 'ec', | |
'g' : 'cedh', | |
'h' : 'mgi', | |
'i' : 'hl', | |
'j' : 'nk', | |
'k' : 'jl', | |
'l' : 'mki', | |
'm' : 'dhln', | |
'n' : 'mj' } | |
def pick_colors(regions, colors): | |
if not regions: | |
totals = collections.defaultdict(int) | |
for r, c in colors.items(): | |
totals[c] += areas[r] | |
if all(v == 36 for v in totals.values()): | |
print(colors) | |
else: | |
r = regions[0] | |
for c in (x for x in range(4) | |
if all(colors.get(n) != x for n in adjacencies[r])): | |
colors[r] = c | |
pick_colors(regions[1:], colors) | |
del colors[r] | |
pick_colors('defghijklmn', | |
{ 'a' : 0, 'b' : 1, 'c' : 2 }) # remove symmetries | |
if __name__ == "__main__": | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment