summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2024-12-17 01:31:58 -0700
committerJack Sangdahl <[email protected]>2024-12-17 01:31:58 -0700
commitc6178019b677743f01aa302047db8ebec7c16739 (patch)
tree8b0e7d98fdacf92691246e21eb449d415097abc3
parent5c91260838a45f4a422fae26aa040420a2770c62 (diff)
downloadaoc-c6178019b677743f01aa302047db8ebec7c16739.tar.gz
aoc-c6178019b677743f01aa302047db8ebec7c16739.zip
Day 17 2024
-rw-r--r--17_2024.py73
-rw-r--r--data/17_2024.txt5
2 files changed, 78 insertions, 0 deletions
diff --git a/17_2024.py b/17_2024.py
new file mode 100644
index 0000000..a9a781b
--- /dev/null
+++ b/17_2024.py
@@ -0,0 +1,73 @@
+import re
+
+input = "data/17_2024.txt"
+data = open(input).read().strip()
+lines, program = data.split("\n\n")
+a, b, c = [int(x) for x in re.findall("-?\d+", lines)]
+program = program.split(":")[1].strip().split(",")
+program = [int(x) for x in program]
+
+
+def run(ast, part2):
+ def getcombo(x):
+ if x in [0, 1, 2, 3]:
+ return x
+ if x == 4:
+ return a
+ if x == 5:
+ return b
+ if x == 6:
+ return c
+ return -1
+
+ a, b, c = ast, 0, 0
+ ip, res = 0, []
+ while True:
+ if ip >= len(program):
+ return res
+ cmd = program[ip]
+ op = program[ip + 1]
+ combo = getcombo(op)
+ if cmd == 0:
+ a = a // 2**combo
+ ip += 2
+ elif cmd == 1:
+ b = b ^ op
+ ip += 2
+ elif cmd == 2:
+ b = combo % 8
+ ip += 2
+ elif cmd == 3:
+ if a != 0:
+ ip = op
+ else:
+ ip += 2
+ elif cmd == 4:
+ b = b ^ c
+ ip += 2
+ elif cmd == 5:
+ res.append(int(combo % 8))
+ if part2 and res[len(res) - 1] != program[len(res) - 1]:
+ return res
+ ip += 2
+ elif cmd == 6:
+ b = a // 2**combo
+ ip += 2
+ elif cmd == 7:
+ c = a // 2**combo
+ ip += 2
+
+
+part1 = run(a, False)
+print(",".join([str(x) for x in part1]))
+ast = 0
+best = 0
+while True:
+ ast += 1
+ a = ast * 8**9 + 0o676236017
+ res = run(a, True)
+ if res == program:
+ print(a)
+ break
+ elif len(res) > best:
+ best = len(res)
diff --git a/data/17_2024.txt b/data/17_2024.txt
new file mode 100644
index 0000000..328b91b
--- /dev/null
+++ b/data/17_2024.txt
@@ -0,0 +1,5 @@
+Register A: 48744869
+Register B: 0
+Register C: 0
+
+Program: 2,4,1,2,7,5,1,3,4,4,5,5,0,3,3,0