summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomasz Kramkowski <tk@the-tk.com>2021-12-24 23:53:05 +0000
committerTomasz Kramkowski <tk@the-tk.com>2021-12-24 23:53:05 +0000
commitab12c493a9cb2a4d66ae612c708b9ebb5dc9743c (patch)
tree3f7bdb0a14dad1e30064546e9ef771d5c7b68c64
parentc2e7b78fd4129dbf3671886bb4a8d8d02b78f62c (diff)
downloadaoc2021-ab12c493a9cb2a4d66ae612c708b9ebb5dc9743c.tar.gz
aoc2021-ab12c493a9cb2a4d66ae612c708b9ebb5dc9743c.tar.xz
aoc2021-ab12c493a9cb2a4d66ae612c708b9ebb5dc9743c.zip
day 24: pythonHEADmaster
-rw-r--r--24.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/24.py b/24.py
new file mode 100644
index 0000000..fee2116
--- /dev/null
+++ b/24.py
@@ -0,0 +1,28 @@
+from utils import open_day
+
+digits = dict()
+stack = list()
+
+with open_day(24) as f:
+ dig = 0
+ for i, line in enumerate(f):
+ _, *operands = line.rstrip().split(' ')
+ if i % 18 == 4: push = operands[1] == '1'
+ if i % 18 == 5: sub = int(operands[1])
+ if i % 18 == 15:
+ add = int(operands[1])
+ if push:
+ stack.append((dig, add))
+ else:
+ sibling, add = stack.pop()
+ diff = add + sub
+ if diff < 0:
+ digits[sibling] = (-diff + 1, 9)
+ digits[dig] = (1, 9 + diff)
+ else:
+ digits[sibling] = (1, 9 - diff)
+ digits[dig] = (1 + diff, 9)
+ dig += 1
+
+print(''.join(str(digits[d][1]) for d in sorted(digits.keys())))
+print(''.join(str(digits[d][0]) for d in sorted(digits.keys())))