Refactoring
This commit is contained in:
32
src/P8.py
32
src/P8.py
@@ -74,34 +74,34 @@ with open("files/P8.txt", "r") as f:
|
|||||||
|
|
||||||
|
|
||||||
def part_1(lines: List[str], debug: bool = False) -> Tuple[int, bool]:
|
def part_1(lines: List[str], debug: bool = False) -> Tuple[int, bool]:
|
||||||
acc = 0
|
accu = 0
|
||||||
executed = []
|
executed = []
|
||||||
nline = 0
|
nline = 0
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
instr = lines[nline]
|
instr, acc = lines[nline].split(" ")
|
||||||
if nline in executed:
|
if nline in executed:
|
||||||
# we are in a for loop :(
|
# we are in a for loop :(
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
executed.append(nline)
|
executed.append(nline)
|
||||||
if instr[:3] == "acc":
|
if instr == "acc":
|
||||||
acc += int(instr[4:])
|
accu += int(acc)
|
||||||
nline += 1
|
nline += 1
|
||||||
continue
|
continue
|
||||||
elif instr[:3] == "jmp":
|
elif instr == "jmp":
|
||||||
nline = nline + int(instr[4:])
|
nline = nline + int(acc)
|
||||||
continue
|
continue
|
||||||
elif instr[:3] == "nop":
|
elif instr == "nop":
|
||||||
nline += 1
|
nline += 1
|
||||||
continue
|
continue
|
||||||
except IndexError:
|
except IndexError:
|
||||||
if debug:
|
if debug:
|
||||||
print("Code has run successfully")
|
print("Code has run successfully")
|
||||||
return acc, True
|
return accu, True
|
||||||
if debug:
|
if debug:
|
||||||
print(f"The value of the accumulator was {acc} before exiting")
|
print(f"The value of the accumulator was {accu} before exiting")
|
||||||
return acc, False
|
return accu, False
|
||||||
|
|
||||||
|
|
||||||
# --- Part Two ---
|
# --- Part Two ---
|
||||||
@@ -159,20 +159,20 @@ def part_1(lines: List[str], debug: bool = False) -> Tuple[int, bool]:
|
|||||||
|
|
||||||
def part_2() -> None:
|
def part_2() -> None:
|
||||||
for nline, _ in enumerate(lines):
|
for nline, _ in enumerate(lines):
|
||||||
inst = lines[nline]
|
inst, acc = lines[nline].split(" ")
|
||||||
# change instructions
|
# change instructions
|
||||||
if inst[:3] == "nop":
|
if inst == "nop":
|
||||||
new_inst = "jmp"
|
new_inst = "jmp"
|
||||||
elif inst[:3] == "jmp":
|
elif inst == "jmp":
|
||||||
new_inst = "nop"
|
new_inst = "nop"
|
||||||
|
|
||||||
# make a copy of the original code
|
# make a copy of the original code
|
||||||
new_lines = lines.copy()
|
new_lines = lines.copy()
|
||||||
# change line according to the instructions in the original code
|
# change line according to the instructions in the original code
|
||||||
new_lines[nline] = " ".join((new_inst, inst[4:]))
|
new_lines[nline] = " ".join((new_inst, acc))
|
||||||
acc, has_ended = part_1(new_lines)
|
accu, has_ended = part_1(new_lines)
|
||||||
if has_ended:
|
if has_ended:
|
||||||
print(f"The value of the accumulator was {acc} after terminates")
|
print(f"The value of the accumulator was {accu} after terminates")
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user