-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10.py
42 lines (31 loc) · 1.06 KB
/
10.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import os
inputFile = open(os.path.dirname(__file__) + '/input.txt', 'r')
nums = [int(line.rstrip('\n')) for line in inputFile]
inputFile.close()
def part_one():
nums.sort()
nums.insert(0, 0)
nums.append(nums[-1] + 3)
one_jolt = three_jolt = 0
for i in range(1, len(nums)):
one_jolt += nums[i] - nums[i - 1] == 1
three_jolt += nums[i] - nums[i - 1] == 3
return one_jolt * three_jolt
def part_two():
nums.sort()
nums.insert(0, 0)
nums.append(nums[-1] + 3)
res = 1
added = {-1: 0, 0: 0}
for i in range(1, len(nums) - 1):
# Check if number can be removed
if nums[i] - nums[i - 1] == 1 and nums[i + 1] - nums[i] == 1:
# We need to minus the number of combinations added 2 numbers ago. They will be invalid
# after the previous number and current number are removed from them
added[i] = res - added[i - 2]
res = res * 2 - added[i - 2]
else:
added[i] = 0
return res
print(f'Part one: {part_one()}')
print(f'Part two: {part_two()}')