2018 day 20 wip

This commit is contained in:
2024-09-02 09:24:55 -04:00
parent 5fae7c0366
commit 381f186a59
2 changed files with 158 additions and 34 deletions

71
2020/d8.py Normal file
View File

@@ -0,0 +1,71 @@
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()