summaryrefslogtreecommitdiffstats
path: root/20
diff options
context:
space:
mode:
authorGustav Sörnäs <gustav@sornas.net>2020-12-09 07:17:24 +0100
committerGustav Sörnäs <gustav@sornas.net>2020-12-09 07:17:40 +0100
commit26654567a4abd6e19766e946f769c6061f350d32 (patch)
treeae40c7bca62ff5445339f0fc6edd17809b35b7f0 /20
parent20d89f9c5a98105b5b094be27f65347d21288b5c (diff)
downloadaoc-26654567a4abd6e19766e946f769c6061f350d32.tar.gz
day 9
Diffstat (limited to '20')
-rw-r--r--20/py/d09.py49
1 files changed, 49 insertions, 0 deletions
diff --git a/20/py/d09.py b/20/py/d09.py
new file mode 100644
index 0000000..4379039
--- /dev/null
+++ b/20/py/d09.py
@@ -0,0 +1,49 @@
+#!/usr/bin/env python3
+import sys
+from collections import deque
+
+
+def intify(_in):
+ return list(int(n.strip()) for n in _in)
+
+
+def pt1(_in):
+ valid = []
+ for i in range(25):
+ for j in range(i+1, 25):
+ valid.append(_in[i] + _in[j])
+ for i in range(25, len(_in)):
+ if _in[i] not in valid:
+ return _in[i]
+ valid = valid[24:]
+ offset = 0
+ for j in range(1, 25)[::-1]:
+ valid.insert(offset, _in[i - j] + _in[i])
+ offset += j
+
+
+def pt2(_in):
+ invalid = pt1(_in)
+
+ i, s, nums = 0, 0, deque()
+ while s != invalid:
+ if s < invalid:
+ # add
+ s += _in[i]
+ nums.append(_in[i])
+ i += 1
+ else:
+ # pop
+ s -= nums[0]
+ nums.popleft()
+ return min(nums) + max(nums)
+
+
+if __name__ == "__main__":
+ if len(sys.argv) > 1:
+ input = open(sys.argv[1], "r").readlines()
+ else:
+ input = open("../input/09", "r").readlines()
+ input = intify(input)
+ print(pt1(input))
+ print(pt2(input))