2020aoc/day1.py

100 lines
2.8 KiB
Python

expense_report = []
with open('day1/input') as reader:
expense_report.extend([int(l) for l in reader.readlines()])
expense_report.sort()
def calc_two(sub_set, absolute):
first_i = 0
second_i = len(sub_set) - 1
first = sub_set[first_i]
second = sub_set[second_i]
while first_i < second_i and first + second + absolute != 2020:
if first + second + absolute > 2020:
second_i -= 1
second = sub_set[second_i]
else:
first_i += 1
first = sub_set[first_i]
if first_i < second_i:
return (first_i, second_i, first, second)
return None
# part one
# first_i = 0
# second_i = len(expense_report) - 1
# first = expense_report[first_i]
# second = expense_report[second_i]
#
# while first + second != 2020:
# if first + second > 2020:
# second_i -= 1
# second = expense_report[second_i]
# else:
# first_i += 1
# first = expense_report[first_i]
first_i, second_i, first, second = calc_two(expense_report, 0)
print(first, second)
print(first * second)
# second part
for i in range(len(expense_report) - 1):
res = calc_two(expense_report[i+1:], expense_report[i])
if res is not None:
print(res[2], res[3], expense_report[i])
print(res[2] * res[3] * expense_report[i])
break
# second part better?
# does not work! and probably never will!
# first_i = 0
# second_i = 1
# third_i = len(expense_report) - 1
# first = expense_report[first_i]
# second = expense_report[second_i]
# third = expense_report[third_i]
#
# up_with_second = first + second + third < 2020
#
# while first + second + third != 2020:
# if first + second + third > 2020:
# if up_with_second:
# if second_i+1 == third_i:
# second_i -= 1
# second = expense_report[second_i]
# third_i -= 1
# third = expense_report[third_i]
# else:
# if second_i-1 != first_i:
# second_i -= 1
# second = expense_report[second_i]
# else:
# third_i -= 1
# third = expense_report[third_i]
# up_with_second = True
# else:
# if up_with_second:
# if second_i+1 != third_i:
# second_i += 1
# second = expense_report[second_i]
# else:
# first_i += 1
# first = expense_report[first_i]
# up_with_second = False
# else:
# if second_i-1 == first_i:
# second_i += 1
# second = expense_report[second_i]
# first_i += 1
# first = expense_report[first_i]
#
#
# print(first, second, third)
# print(first * second * third)