-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday1.py
More file actions
40 lines (36 loc) · 1.22 KB
/
day1.py
File metadata and controls
40 lines (36 loc) · 1.22 KB
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
def part_one(data):
expenses = strings_to_integers(data)
first, second = find_two_sum(expenses, 2020)
return first * second
def strings_to_integers(data):
return list(map(int, data.split()))
def find_two_sum(integers, target_sum):
integers = sorted(integers)
low_index = 0
high_index = -1
total = 0
while total != target_sum:
total = integers[low_index] + integers[high_index]
if total == target_sum:
return (integers[low_index], integers[high_index])
elif low_index == len(integers)-1 or high_index == 0-len(integers):
break
elif total > target_sum:
high_index -= 1
elif total < target_sum:
high_index = -1
low_index += 1
def part_two(data):
expenses = strings_to_integers(data)
first, second, third = find_three_sum(expenses, 2020)
return first * second * third
def find_three_sum(integers, target_sum):
for i in integers:
new_integers = integers
new_integers.remove(i)
new_target_sum = target_sum - i
result = find_two_sum(new_integers, new_target_sum)
if result is None:
continue
else:
return {i, result[0], result[1]}