Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/python
- import datetime
- import math
- import collections
- import random
- import re
- import itertools
- raw_decks = []
- # raw_decks.append("""""")
- def ProcessDeck(raw_deck):
- result = collections.defaultdict(int)
- name = re.match("\[b\]\[URL=.*\](?P<id>.+)\[/URL\]\[/b\]", raw_deck).groupdict()['id']
- #print name
- for line in raw_deck.split('\n'):
- line = line.strip()
- if not line:
- continue
- id_matcher = re.match("(?P<num>\d+) \[URL=.*\](?P<card>.+)\[/URL\]", line)
- if not id_matcher:
- continue
- #print "%s: %s" % (line, id_matcher.groupdict()['num'])
- result[id_matcher.groupdict()['card']] = id_matcher.groupdict()['num']
- return (name, result)
- MAX_CARD_LEN = 12
- def ShortenCardName(card):
- if len(card) > MAX_CARD_LEN + 3:
- return card[:MAX_CARD_LEN] + '...'
- return card
- decks = []
- for raw_deck in raw_decks:
- if raw_deck:
- decks.append(ProcessDeck(raw_deck))
- verbose = False
- for (d1_name, d1), (d2_name, d2) in itertools.combinations(decks, 2):
- overlap_cards = set(d1).intersection(set(d2))
- non_overlap_cards = set(d1).symmetric_difference(set(d2))
- if not overlap_cards:
- if verbose:
- print 'PASS: These decks have no cards in common.'
- continue
- if len(overlap_cards) < len(non_overlap_cards):
- print 'IOU %s -> %s' % (d1_name, d2_name)
- for card in sorted(overlap_cards):
- n1, n2 = [int(d[card]) for d in [d1, d2]]
- card = ShortenCardName(card)
- total = n1 + n2
- if total <= 3:
- if verbose:
- print 'WARN: %s Only %s total.' % (card.ljust(MAX_CARD_LEN + 3), total)
- if n1 and n2:
- diff = abs(n1 -n2)
- transfer = max(n1,n2) - diff
- print '%s Transfer %s (%s for LHS & %s for RHS)' % (card.ljust(MAX_CARD_LEN + 3), transfer, n1, n2)
- print
- else:
- print 'XOR %s & %s' % (d1_name, d2_name)
- for card in sorted(set(d1).union(set(d2))):
- n1, n2 = [int(d[card]) for d in [d1, d2]]
- if n1 != n2:
- card = ShortenCardName(card)
- print '%s (%s for LHS & %s for RHS)' % (card.ljust(MAX_CARD_LEN + 3), n1, n2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement