diff options
| author | Gustav Sörnäs <gustav@sornas.net> | 2020-12-08 06:29:19 +0100 |
|---|---|---|
| committer | Gustav Sörnäs <gustav@sornas.net> | 2020-12-08 06:29:19 +0100 |
| commit | 82c8bb2d12886ff1d34e71fbd543f04886ca3180 (patch) | |
| tree | 548c557d1bd909c86a4e5ba4fa512f18d09df442 /20/py | |
| parent | f2301c17e9dbc1a4aeda6821f6e518f5c5e29cc5 (diff) | |
| download | aoc-82c8bb2d12886ff1d34e71fbd543f04886ca3180.tar.gz | |
common run()
Diffstat (limited to '20/py')
| -rw-r--r-- | 20/py/d08.py | 32 |
1 files changed, 9 insertions, 23 deletions
diff --git a/20/py/d08.py b/20/py/d08.py index c53a6ff..f2aa572 100644 --- a/20/py/d08.py +++ b/20/py/d08.py @@ -2,15 +2,15 @@ import sys -def pt1(_in): +def run(prog): acc = 0 loc = 0 execed = set() - while True: + while loc < len(prog): if loc in execed: - return acc + return False, acc execed.add(loc) - inst, offset = _in[loc][:-1].split() # \n + inst, offset = prog[loc].strip().split() offset = int(offset) if inst == "acc": acc += offset @@ -19,28 +19,14 @@ def pt1(_in): loc += offset else: loc += 1 + return True, acc -def pt2(_in): - def run(prog): - acc = 0 - loc = 0 - execed = set() - while loc < len(prog): - if loc in execed: - return False, acc - execed.add(loc) - inst, offset = prog[loc].strip().split() - offset = int(offset) - if inst == "acc": - acc += offset - loc += 1 - elif inst == "jmp": - loc += offset - else: - loc += 1 - return True, acc +def pt1(_in): + return run(_in)[1] + +def pt2(_in): for i in range(len(_in)): prog = _in.copy() inst, offset = prog[i].strip().split() |
