|
|
|
|
@ -1,6 +1,6 @@ |
|
|
|
|
import unittest |
|
|
|
|
import os |
|
|
|
|
from . import comp |
|
|
|
|
from .comp import OpcodeComputer |
|
|
|
|
import itertools |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -11,20 +11,37 @@ class Day7(unittest.TestCase): |
|
|
|
|
return [int(k) for k in fp.readline().split(',')] |
|
|
|
|
|
|
|
|
|
def test_day7a(self): |
|
|
|
|
com = comp.OpcodeComputer(self.get_code()) |
|
|
|
|
com = OpcodeComputer(self.get_code()) |
|
|
|
|
configs = itertools.permutations(range(5), 5) |
|
|
|
|
|
|
|
|
|
res = 0 |
|
|
|
|
out = [] |
|
|
|
|
res: int = 0 |
|
|
|
|
for conf in configs: |
|
|
|
|
sig = 0 |
|
|
|
|
sig: int = 0 |
|
|
|
|
for phase in conf: |
|
|
|
|
out.clear() |
|
|
|
|
com.process_op([phase, sig], out) |
|
|
|
|
sig = out[0] |
|
|
|
|
res = int(out[0]) if int(out[0]) > res else res |
|
|
|
|
sig = int(list(com.process_op([phase, sig]))[0]) |
|
|
|
|
res = sig if sig > res else res |
|
|
|
|
|
|
|
|
|
self.assertEqual(res, 21000) |
|
|
|
|
|
|
|
|
|
def test_day7b(self): |
|
|
|
|
pass |
|
|
|
|
""" |
|
|
|
|
Shoutout to OverjoyedBanana for his helpful solution |
|
|
|
|
https://www.reddit.com/r/adventofcode/comments/e7om28/2019_day_7_part_2_python3_help_with/fa3bh4w/ |
|
|
|
|
https://pastebin.com/cyLTHUYV |
|
|
|
|
""" |
|
|
|
|
com = OpcodeComputer(self.get_code()) |
|
|
|
|
res: int = 0 |
|
|
|
|
for config in itertools.permutations(range(5, 10), 5): |
|
|
|
|
sig: int = 0 |
|
|
|
|
buffer = [[conf] for conf in config] |
|
|
|
|
comps = list(map(com.process_op, buffer)) |
|
|
|
|
|
|
|
|
|
try: |
|
|
|
|
while True: |
|
|
|
|
for i in range(5): |
|
|
|
|
buffer[i].append(sig) |
|
|
|
|
sig = int(next(comps[i])) |
|
|
|
|
except StopIteration: |
|
|
|
|
pass |
|
|
|
|
res = sig if sig > res else res |
|
|
|
|
self.assertEqual(res, 61379886) |
|
|
|
|
|