Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import csv
- import itertools
- def split_into_ints(s, length=2):
- return [int(''.join(x).lstrip('0')) for x in zip(*[list(s[z::length]) for z in range(length)])]
- def assert_no_dupes(seq):
- _list = list(seq)
- assert len(set(_list)) == len(_list), _list
- def get_subsequences(seq):
- [itertools.chain(*x) for x in zip(*[list(a[z::3]) for z in range(3)])]
- def process_round(moves, tickets, round_no, line_organizer):
- print 'Round %s' % round_no
- print '-'*40
- won_round = []
- for num, move in enumerate(moves, 1):
- won_this_move = []
- for ticket, lines in tickets:
- if ticket in won_round:
- continue
- for line in line_organizer(lines):
- if set(line) <= set(moves[0:num]):
- won_this_move.append('%s' % ticket)
- won_round.append(ticket)
- print '#%2d (barrel %2d) - %s' % (num, move, ', '.join(won_this_move))
- print '-'*40
- def run():
- # read tickets
- with open('tickets.txt', 'r') as f:
- tickets = [(int(ln[0]), [split_into_ints(s) for s in ln[1:]]) for ln in csv.reader(f, delimiter=' ', quoting=csv.QUOTE_NONE)]
- # validate from obvious human errors
- for ix, lines in tickets:
- for line in lines:
- assert_no_dupes(line)
- assert_no_dupes([i/10 for i in line])
- assert sorted(line) == line
- assert_no_dupes(itertools.chain(*lines[0:3]))
- assert_no_dupes(itertools.chain(*lines[3:6]))
- # read moves
- with open('moves.txt', 'r') as f:
- moves = [int(ln[0]) for ln in csv.reader(f, quoting=csv.QUOTE_NONE)]
- assert set(moves) == set(i for i in range(1, 91) if i != 11)
- process_round(moves, tickets, 1, lambda x: x)
- process_round(moves, tickets, 2, lambda a: [a[0]+a[1], a[0]+a[2], a[1]+a[2], a[3]+a[4], a[3]+a[5], a[4]+a[5]])
- process_round(moves, tickets, 3, lambda a: [a[0]+a[1]+a[2], a[3]+a[4]+a[5]])
- process_round(moves, tickets, 4, lambda a: [itertools.chain(*a)])
- if __name__ == '__main__':
- run()
Advertisement
Add Comment
Please, Sign In to add comment