xyzman

Untitled

Feb 27th, 2014
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.99 KB | None | 0 0
  1. import csv
  2. import itertools
  3.  
  4.  
  5. def split_into_ints(s, length=2):
  6.     return [int(''.join(x).lstrip('0')) for x in zip(*[list(s[z::length]) for z in range(length)])]
  7.  
  8.  
  9. def assert_no_dupes(seq):
  10.     _list = list(seq)
  11.     assert len(set(_list)) == len(_list), _list
  12.  
  13. def get_subsequences(seq):
  14.     [itertools.chain(*x) for x in zip(*[list(a[z::3]) for z in range(3)])]
  15.  
  16. def process_round(moves, tickets, round_no, line_organizer):
  17.     print 'Round %s' % round_no
  18.     print '-'*40
  19.  
  20.     won_round = []
  21.     for num, move in enumerate(moves, 1):
  22.         won_this_move = []
  23.         for ticket, lines in tickets:
  24.             if ticket in won_round:
  25.                 continue
  26.             for line in line_organizer(lines):
  27.                 if set(line) <= set(moves[0:num]):
  28.                     won_this_move.append('%s' % ticket)
  29.                     won_round.append(ticket)
  30.         print '#%2d (barrel %2d) - %s' % (num, move, ', '.join(won_this_move))
  31.     print '-'*40
  32.  
  33.  
  34. def run():
  35.     # read tickets
  36.     with open('tickets.txt', 'r') as f:
  37.         tickets = [(int(ln[0]), [split_into_ints(s) for s in ln[1:]]) for ln in csv.reader(f, delimiter=' ', quoting=csv.QUOTE_NONE)]
  38.  
  39.     # validate from obvious human errors
  40.     for ix, lines in tickets:
  41.         for line in lines:
  42.             assert_no_dupes(line)
  43.             assert_no_dupes([i/10 for i in line])
  44.             assert sorted(line) == line
  45.         assert_no_dupes(itertools.chain(*lines[0:3]))
  46.         assert_no_dupes(itertools.chain(*lines[3:6]))
  47.  
  48.     # read moves
  49.     with open('moves.txt', 'r') as f:
  50.         moves = [int(ln[0]) for ln in csv.reader(f, quoting=csv.QUOTE_NONE)]
  51.  
  52.     assert set(moves) == set(i for i in range(1, 91) if i != 11)
  53.  
  54.     process_round(moves, tickets, 1, lambda x: x)
  55.     process_round(moves, tickets, 1, lambda a: [itertools.chain(*x) for x in zip(*[list(a[z::3]) for z in range(3)])])
  56.     process_round(moves, tickets, 1, lambda a: [itertools.chain(*a)])
  57.  
  58.  
  59.  
  60. if __name__ == '__main__':
  61.     run()
Advertisement
Add Comment
Please, Sign In to add comment