summaryrefslogtreecommitdiff
path: root/day8/day8.py
diff options
context:
space:
mode:
Diffstat (limited to 'day8/day8.py')
-rw-r--r--day8/day8.py55
1 files changed, 55 insertions, 0 deletions
diff --git a/day8/day8.py b/day8/day8.py
new file mode 100644
index 0000000..446a02f
--- /dev/null
+++ b/day8/day8.py
@@ -0,0 +1,55 @@
+"""Day 7."""
+with open("input") as f:
+ instructions = f.read().rstrip().split("\n")
+
+def parse_instructions(instructions):
+ """Parse Instructions."""
+ acc = 0
+ visited = set()
+ loc = 0
+ for x in range(len(instructions)):
+ try:
+ inst, val = instructions[loc].split(" ")
+ except IndexError:
+ if x == len(instructions):
+ return acc, False
+ else:
+ return acc, True
+ if loc in visited:
+ break
+ else:
+ visited.add(loc)
+ if inst == "jmp":
+ loc += int(val)
+ elif inst == "acc":
+ acc += int(val)
+ loc += 1
+ elif inst == "nop":
+ loc += 1
+ successful = x == len(instructions)
+ return acc, successful
+
+
+def instruction_generator(instructions):
+ yield instructions
+ for x, line in enumerate(instructions):
+ inst, val = line.split(" ")
+ if inst == 'nop':
+ new = instructions.copy()
+ new[x] = f"jmp {val}"
+ yield new
+ elif inst == 'jmp':
+ new = instructions.copy()
+ new[x] = f"nop {val}"
+ yield new
+
+
+# Part 1
+acc, _ = parse_instructions(instructions)
+print(acc)
+
+# Part 2
+for intset in list(instruction_generator(instructions)):
+ acc, successful = parse_instructions(intset)
+ if successful:
+ print(acc)