Created
December 11, 2016 18:36
-
-
Save cjauvin/14f8797f6d5709960ffd9ecaad604e3e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from collections import defaultdict | |
| import re | |
| bot_values = defaultdict(list) # bot i -> list of values | |
| bins = {} | |
| s = """value 5 goes to bot 2 | |
| bot 2 gives low to bot 1 and high to bot 0 | |
| value 3 goes to bot 1 | |
| bot 1 gives low to output 1 and high to bot 0 | |
| bot 0 gives low to output 2 and high to output 0 | |
| value 2 goes to bot 2""" | |
| #s = s.split('\n') | |
| s = open('data/day10.txt').readlines() | |
| t = [] | |
| for x in s: | |
| if 'value' in x: | |
| val, bot = map(int, re.findall('\d+', x)) | |
| bot_values[bot].append(val) | |
| else: | |
| t.append(x) | |
| bot_actions = {} | |
| for x in t: | |
| m = re.search('bot (\d+) gives low to (\w+) (\d+) and high to (\w+) (\d+)', x) | |
| action_bot, low_type, low_dest, hi_type, hi_dest = m.groups() | |
| action_bot, low_dest, hi_dest = map(int, [action_bot, low_dest, hi_dest]) | |
| assert action_bot not in bot_actions | |
| bot_actions[action_bot] = [low_type, low_dest, hi_type, hi_dest] | |
| def execute(type_, val, dest): | |
| # print(type_, val, dest) | |
| global bot_values, bins | |
| if type_ == 'bot': | |
| bot_values[dest].append(val) | |
| else: | |
| bins[dest] = val | |
| changed = True | |
| while changed: | |
| changed = False | |
| for action_bot in bot_actions: | |
| if len(bot_values[action_bot]) > 1: | |
| low_type, low_dest, hi_type, hi_dest = bot_actions[action_bot] | |
| low_val = min(bot_values[action_bot]) | |
| hi_val = max(bot_values[action_bot]) | |
| #print(action_bot, low_val, hi_val) | |
| execute(low_type, low_val, low_dest) | |
| execute(hi_type, hi_val, hi_dest) | |
| bot_values[action_bot] = [] | |
| changed = True | |
| # print(bins) | |
| # print(bot_values) | |
| print(bins[0] * bins[1] * bins[2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment