mirror of
https://github.com/Crocmagnon/advent-of-code.git
synced 2024-11-05 22:33:59 +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():
|
def main():
|
||||||
lines = []
|
lines = []
|
||||||
with open("inputs/day01") as f:
|
with open("inputs/day01") as f:
|
||||||
for line in f:
|
for line in f:
|
||||||
lines.append(int(line.strip()))
|
lines.append(int(line.strip()))
|
||||||
|
|
||||||
stop = len(lines)
|
res = solve(lines, 2)
|
||||||
for i, value in enumerate(lines):
|
print("result is", res)
|
||||||
for j in range(i+1, stop):
|
res = solve(lines, 3)
|
||||||
other = lines[j]
|
print("result is", res)
|
||||||
for k in range(j+1, stop):
|
|
||||||
third = lines[k]
|
|
||||||
addition = other + value + third
|
def solve(expense_report, fix_number):
|
||||||
if addition == 2020:
|
for combination in combinations(expense_report, fix_number):
|
||||||
print("result is", other * value * third)
|
if sum(combination) == 2020:
|
||||||
|
return prod(combination)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
Loading…
Reference in a new issue