mirror of
https://github.com/Crocmagnon/advent-of-code.git
synced 2024-11-22 06:28:11 +01:00
Improve solution for day 1 using combinations
This commit is contained in:
parent
c52fe0aa76
commit
0841f8fba6
1 changed files with 14 additions and 9 deletions
|
@ -1,18 +1,23 @@
|
|||
from itertools import combinations
|
||||
from math import prod
|
||||
|
||||
|
||||
def main():
|
||||
lines = []
|
||||
with open("inputs/day01") as f:
|
||||
for line in f:
|
||||
lines.append(int(line.strip()))
|
||||
|
||||
stop = len(lines)
|
||||
for i, value in enumerate(lines):
|
||||
for j in range(i+1, stop):
|
||||
other = lines[j]
|
||||
for k in range(j+1, stop):
|
||||
third = lines[k]
|
||||
addition = other + value + third
|
||||
if addition == 2020:
|
||||
print("result is", other * value * third)
|
||||
res = solve(lines, 2)
|
||||
print("result is", res)
|
||||
res = solve(lines, 3)
|
||||
print("result is", res)
|
||||
|
||||
|
||||
def solve(expense_report, fix_number):
|
||||
for combination in combinations(expense_report, fix_number):
|
||||
if sum(combination) == 2020:
|
||||
return prod(combination)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
Loading…
Reference in a new issue