-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpartTwo.py
More file actions
63 lines (45 loc) · 1.61 KB
/
Copy pathpartTwo.py
File metadata and controls
63 lines (45 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import copy
from typing import List, Tuple
from common import *
def execute(instructions: List[Instruction]) -> Tuple[bool, int]:
# Returning false means a loop occured. Returning true means no loop occured.
# The value of the accumulator is always returned
acc = 0
pc = 0
visited = [] # indexes of visited instructions
while True:
if pc >= len(instructions):
# Clean exit
return True, acc
if pc in visited:
# Loop is going to occur at some point
return False, acc
else:
visited.append(pc)
cir = instructions[pc]
if cir.opcode == "jmp":
pc += cir.operand
else:
if cir.opcode == "acc":
acc += cir.operand
elif cir.opcode == "nop":
pass
pc += 1
def partTwo(instr: str) -> int:
master_instructions = parse(instr)
# I imagine there's probably a smart way to do this... but I'm going to bruteforce it haha
# Build list of locations of instructions to be switched
switch_set_locations = []
for idx, instruction in enumerate(master_instructions):
if instruction.opcode in ["jmp", "nop"]:
switch_set_locations.append(idx)
for slc in switch_set_locations:
instructions = copy.deepcopy(master_instructions)
# Switch instruction
oldval = instructions[slc].opcode
instructions[slc].opcode = "jmp" if oldval == "nop" else "nop"
# Execute
clean_exit, acc = execute(instructions)
if clean_exit:
break
return acc