Minor changes to 2018 Day 16

This commit is contained in:
Luke Hubmayer-Werner 2018-12-17 11:34:44 +10:30
parent 107933cb99
commit 4c583274ab
1 changed files with 16 additions and 17 deletions

View File

@ -29,8 +29,8 @@ instructions = [oper_factory(op, False, imm) for op in bin_ops for imm in (False
+ [oper_factory(op, *imm) for op in comp_ops for imm in comp_imms] + [oper_factory(op, *imm) for op in comp_ops for imm in comp_imms]
num_3_or_more = 0 samples_3_or_more = 0
instruction_codes = {} instruction_candidates = {}
for before, opcodes, after in zip(numbers_pt1[::3], numbers_pt1[1::3], numbers_pt1[2::3]): for before, opcodes, after in zip(numbers_pt1[::3], numbers_pt1[1::3], numbers_pt1[2::3]):
possibilities = set() possibilities = set()
for op in instructions: for op in instructions:
@ -41,29 +41,28 @@ for before, opcodes, after in zip(numbers_pt1[::3], numbers_pt1[1::3], numbers_p
except Exception: except Exception:
pass pass
if len(possibilities) >= 3: if len(possibilities) >= 3:
num_3_or_more += 1 samples_3_or_more += 1
if opcodes[0] in instruction_codes: if opcodes[0] in instruction_candidates:
instruction_codes[opcodes[0]] &= possibilities instruction_candidates[opcodes[0]] &= possibilities
else: else:
instruction_codes[opcodes[0]] = set(possibilities) instruction_candidates[opcodes[0]] = set(possibilities)
print(num_3_or_more) # Part 1 print(samples_3_or_more) # Part 1
while sum([len(i) for i in instruction_codes.values()]) > len(instructions): instruction_codes = {}
for code, candidates in instruction_codes.items(): while instruction_candidates:
for code, candidates in instruction_candidates.items():
if len(candidates) == 1: if len(candidates) == 1:
for code2 in [c for c in instruction_codes if c != code]: instruction_codes[code] = tuple(candidates)[0]
instruction_codes[code2] -= candidates for code2 in [c for c in instruction_candidates if c != code]:
instruction_codes = {k: tuple(s)[0] for k, s in instruction_codes.items()} instruction_candidates[code2].discard(instruction_codes[code])
instruction_candidates.pop(code)
break # Can't continue iterating on mutated sequence, also want to recheck start anyway
regs = [0, 0, 0, 0] regs = [0, 0, 0, 0]
for i, opcodes in enumerate(numbers_pt2): for i, opcodes in enumerate(numbers_pt2):
try: regs = instruction_codes[opcodes[0]](regs, opcodes)
regs = instruction_codes[opcodes[0]](regs, opcodes)
except Exception:
print(f'Line {i}: {regs} {opcodes}')
raise
print(regs) # Part 2 is first number print(regs) # Part 2 is first number