summaryrefslogtreecommitdiff
path: root/day9/day9.py
diff options
context:
space:
mode:
authorCody Hiar <cody@hiar.ca>2021-03-05 12:31:00 -0700
committerCody Hiar <cody@hiar.ca>2021-03-05 12:31:00 -0700
commit54998c7452e423a6aa7deef3047f0c334c3272f6 (patch)
tree36e5bbe4e144b49b6e842196c7785d673b218fea /day9/day9.py
parent872cf02664b8979a0b22b394bf239941774028d8 (diff)
Updating till day 10
Diffstat (limited to 'day9/day9.py')
-rw-r--r--day9/day9.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/day9/day9.py b/day9/day9.py
new file mode 100644
index 0000000..3bd8155
--- /dev/null
+++ b/day9/day9.py
@@ -0,0 +1,35 @@
+"""Day 9."""
+from itertools import permutations
+
+with open("input") as f:
+ nums = [int(x) for x in f.read().rstrip().split("\n")]
+
+preamble_length = 25
+invalid_number = None
+
+# Part 1
+for idx, val in enumerate(nums[preamble_length:], start=preamble_length):
+ pre = nums[idx - preamble_length: idx]
+ valid = False
+ for combs in permutations(pre, 2):
+ if sum(combs) == val:
+ valid = True
+ if not valid:
+ invalid_number = val
+ print(invalid_number)
+ break
+
+# Part 2
+found = False
+for x in range(len(nums)):
+ for y in range(x + 1, len(nums)):
+ subset = nums[x:y]
+ total = sum(subset)
+ if total > invalid_number:
+ break
+ if total == invalid_number:
+ print(min(subset) + max(subset))
+ found = True
+ break
+ if found:
+ break