Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2014
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.10 KB | None | 0 0
  1. #! /usr/bin/python
  2. import datetime
  3. import math
  4. import collections
  5. import random
  6. import re
  7. import itertools
  8.  
  9. raw_decks = []
  10.  
  11. # raw_decks.append("""""")
  12.  
  13. def ProcessDeck(raw_deck):
  14.   result = collections.defaultdict(int)
  15.   name = re.match("\[b\]\[URL=.*\](?P<id>.+)\[/URL\]\[/b\]", raw_deck).groupdict()['id']
  16.   #print name
  17.   for line in raw_deck.split('\n'):
  18.     line = line.strip()
  19.     if not line:
  20.       continue
  21.     id_matcher = re.match("(?P<num>\d+) \[URL=.*\](?P<card>.+)\[/URL\]", line)
  22.     if not id_matcher:
  23.       continue
  24.     #print "%s: %s" % (line, id_matcher.groupdict()['num'])
  25.     result[id_matcher.groupdict()['card']] = id_matcher.groupdict()['num']
  26.   return (name, result)
  27.  
  28. MAX_CARD_LEN = 12
  29. def ShortenCardName(card):
  30.   if len(card) > MAX_CARD_LEN + 3:
  31.     return card[:MAX_CARD_LEN] + '...'
  32.   return card
  33.  
  34. decks = []
  35.  
  36. for raw_deck in raw_decks:
  37.   if raw_deck:
  38.     decks.append(ProcessDeck(raw_deck))
  39.  
  40. verbose = False
  41.  
  42. for (d1_name, d1), (d2_name, d2) in itertools.combinations(decks, 2):
  43.   overlap_cards = set(d1).intersection(set(d2))
  44.   non_overlap_cards = set(d1).symmetric_difference(set(d2))
  45.   if not overlap_cards:
  46.     if verbose:
  47.       print 'PASS: These decks have no cards in common.'
  48.     continue
  49.   if len(overlap_cards) < len(non_overlap_cards):
  50.     print 'IOU %s -> %s' % (d1_name, d2_name)
  51.     for card in sorted(overlap_cards):
  52.       n1, n2 = [int(d[card]) for d in [d1, d2]]
  53.       card = ShortenCardName(card)
  54.       total = n1 + n2
  55.       if total <= 3:
  56.         if verbose:
  57.           print 'WARN: %s Only %s total.' % (card.ljust(MAX_CARD_LEN + 3), total)
  58.       if n1 and n2:
  59.         diff = abs(n1 -n2)
  60.         transfer = max(n1,n2) - diff
  61.         print '%s Transfer %s (%s for LHS & %s for RHS)' % (card.ljust(MAX_CARD_LEN + 3), transfer, n1, n2)
  62.     print
  63.   else:
  64.     print 'XOR %s & %s' % (d1_name, d2_name)
  65.     for card in sorted(set(d1).union(set(d2))):
  66.       n1, n2 = [int(d[card]) for d in [d1, d2]]
  67.       if n1 != n2:
  68.         card = ShortenCardName(card)
  69.         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