Skip to content

Instantly share code, notes, and snippets.

@relsqui
Last active May 14, 2016 06:34
Show Gist options
  • Save relsqui/d5d4ccd8c4bf257b7be3a376cf007076 to your computer and use it in GitHub Desktop.
Save relsqui/d5d4ccd8c4bf257b7be3a376cf007076 to your computer and use it in GitHub Desktop.
Find the maximum unique rulings on a Magic card.
#!/usr/bin/python3
import json
print("Loading ... ", end="")
with open("AllCards-x.json") as f:
# that's http://mtgjson.com/json/AllSets-x.json.zip, unzipped
cards = json.loads(f.read())
print("found {} cards.".format(len(cards)))
print("Filtering out tokens, planes, phenomena, schemes, and vanguards.")
filter_layouts = ["token", "plane", "scheme", "phenomenon", "vanguard"]
cards = {n:c for n,c in cards.items() if c["layout"] not in filter_layouts}
print("Collecting rulings.")
all_rulings = {}
for c in cards.values():
for ruling in c.get("rulings", []):
# set dict value to true iff the key was already there
all_rulings[ruling["text"]] = ruling["text"] in all_rulings
# unique rulings are the keys corresponding to false values
print("Filtering duplicate rulings.")
unique_rulings = [r for r in all_rulings if not all_rulings[r]]
max_unique = 0
card_names = []
print("Counting unique rulings on {} cards ... ".format(len(cards)), end="")
for name, c in cards.items():
rulings = c.get("rulings", [])
if len(rulings) < max_unique:
continue
uniques = sum(1 for r in rulings if r["text"] in unique_rulings)
if uniques > max_unique:
max_unique = uniques
card_names = [name]
elif uniques == max_unique:
card_names.append(name)
print("done.")
print("The most unique rulings on any card is {}.".format(max_unique))
print("Cards with that many:")
for name in card_names:
print(" ", name)
Loading ... found 16187 cards.
Filtering out tokens, planes, phenomena, schemes, and vanguards.
Collecting rulings.
Filtering duplicate rulings.
Counting unique rulings on 15928 cards ... done.
The most unique rulings on any card is 17.
Cards with that many:
Lich's Mirror
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment