|
| 1 | +# -------------------------------- Input data -------------------------------- # |
| 2 | +import os |
| 3 | + |
| 4 | +test_data = {} |
| 5 | + |
| 6 | +test = 1 |
| 7 | +test_data[test] = {"input": """value 5 goes to bot 2 |
| 8 | +bot 2 gives low to bot 1 and high to bot 0 |
| 9 | +value 3 goes to bot 1 |
| 10 | +bot 1 gives low to output 1 and high to bot 0 |
| 11 | +bot 0 gives low to output 2 and high to output 0 |
| 12 | +value 2 goes to bot 2""", |
| 13 | + "expected": ['0', 'Unknown'], |
| 14 | + } |
| 15 | + |
| 16 | +test += 1 |
| 17 | +test_data[test] = {"input": """""", |
| 18 | + "expected": ['Unknown', 'Unknown'], |
| 19 | + } |
| 20 | + |
| 21 | +test = 'real' |
| 22 | +input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt')) |
| 23 | +test_data[test] = {"input": open(input_file, "r+").read().strip(), |
| 24 | + "expected": ['27', '13727'], |
| 25 | + } |
| 26 | + |
| 27 | +# -------------------------------- Control program execution -------------------------------- # |
| 28 | + |
| 29 | +case_to_test = 'real' |
| 30 | +part_to_test = 2 |
| 31 | +verbose_level = 1 |
| 32 | + |
| 33 | +# -------------------------------- Initialize some variables -------------------------------- # |
| 34 | + |
| 35 | +puzzle_input = test_data[case_to_test]['input'] |
| 36 | +puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1] |
| 37 | +puzzle_actual_result = 'Unknown' |
| 38 | + |
| 39 | + |
| 40 | +# -------------------------------- Actual code execution -------------------------------- # |
| 41 | + |
| 42 | +bots = {bot:[0,0] for bot in range(300)} |
| 43 | +outputs = [0] * 200 |
| 44 | + |
| 45 | +puzzle_actual_result = '' |
| 46 | + |
| 47 | +while puzzle_actual_result == '': |
| 48 | + instructions = puzzle_input.split('\n') |
| 49 | + for string in instructions: |
| 50 | + if string[0:5] == 'value': |
| 51 | + _, value, _, _, _, bot = string.split(' ') |
| 52 | + value, bot = int(value), int(bot) |
| 53 | + bots[bot][1 if bots[bot][0] else 0] = value |
| 54 | + |
| 55 | + else: |
| 56 | + _, bot, _, _, _, low_botout, low_bot, _, _, _, high_botout, high_bot = string.split(' ') |
| 57 | + bot, low_bot, high_bot = int(bot), int(low_bot), int(high_bot) |
| 58 | + if bots[bot][0] and bots[bot][1]: |
| 59 | + if bots[bot][0] > bots[bot][1]: |
| 60 | + low_value, high_value = bots[bot][1], bots[bot][0] |
| 61 | + else: |
| 62 | + low_value, high_value = bots[bot][0], bots[bot][1] |
| 63 | + |
| 64 | + if part_to_test == 1 and low_value == 17 and high_value == 61: |
| 65 | + puzzle_actual_result = bot |
| 66 | + break |
| 67 | + elif part_to_test == 1 and low_value == 3 and high_value == 5: |
| 68 | + puzzle_actual_result = bot |
| 69 | + break |
| 70 | + elif part_to_test == 2 and outputs[0] * outputs[1] * outputs[2] != 0: |
| 71 | + puzzle_actual_result = outputs[0] * outputs[1] * outputs[2] |
| 72 | + break |
| 73 | + |
| 74 | + if low_botout == 'bot': |
| 75 | + bots[low_bot][1 if bots[low_bot][0] else 0] = low_value |
| 76 | + elif low_botout == 'output': |
| 77 | + outputs[low_bot] = low_value |
| 78 | + |
| 79 | + if high_botout == 'bot': |
| 80 | + bots[high_bot][1 if bots[high_bot][0] else 0] = high_value |
| 81 | + elif high_botout == 'output': |
| 82 | + outputs[high_bot] = high_value |
| 83 | + |
| 84 | + bots[bot][0], bots[bot][1] = 0, 0 |
| 85 | + else: |
| 86 | + instructions.append(string) |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | +# -------------------------------- Outputs / results -------------------------------- # |
| 92 | + |
| 93 | +if verbose_level >= 3: |
| 94 | + print ('Input : ' + puzzle_input) |
| 95 | +print ('Expected result : ' + str(puzzle_expected_result)) |
| 96 | +print ('Actual result : ' + str(puzzle_actual_result)) |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | + |
0 commit comments