aocpy/2020/d8.py
2024-09-02 09:24:55 -04:00

72 lines
1.6 KiB
Python

from lib import get_data, str_to_ints
def part_1(data):
insts = data.splitlines()
acc = 0
i = 0
seen = set()
while i < len(insts) and i not in seen:
inst = insts[i]
seen.add(i)
if inst.startswith("acc"):
(v,) = str_to_ints(inst)
acc += v
i += 1
elif inst.startswith("jmp"):
(v,) = str_to_ints(inst)
i += v
elif inst.startswith("nop"):
i += 1
else:
assert False
print(acc)
for j in range(len(insts)):
if "jmp" in insts[j]:
insts[j] = insts[j].replace("jmp", "nop")
elif "nop" in insts[j]:
insts[j] = insts[j].replace("nop", "jmp")
else:
continue
i = 0
acc = 0
seen = set()
while i < len(insts) and i not in seen:
inst = insts[i]
seen.add(i)
if inst.startswith("acc"):
(v,) = str_to_ints(inst)
acc += v
i += 1
elif inst.startswith("jmp"):
(v,) = str_to_ints(inst)
i += v
elif inst.startswith("nop"):
i += 1
else:
assert False
if not i in seen:
print(acc)
if "jmp" in insts[j]:
insts[j] = insts[j].replace("jmp", "nop")
elif "nop" in insts[j]:
insts[j] = insts[j].replace("nop", "jmp")
else:
continue
def main():
data = get_data(__file__)
part_1(data)
if __name__ == "__main__":
main()