Solution to problem 1 part 2 in Python

This commit is contained in:
David Doblas Jiménez 2021-11-14 17:06:14 +01:00
parent ea8d62a30f
commit 7498ac6c8a

View File

@ -37,13 +37,52 @@
# to 2020; what do you get if you multiply them together?
with open("files/P1.txt", "r") as f:
numbers = [int(l) for l in f.read().strip().split("\n")]
numbers = [int(number) for number in f.read().strip().split("\n")]
for n in numbers:
for i in range(len(numbers)):
if n + numbers[i] == 2020:
print(f"{n} times {numbers[i]} is {n*numbers[i]}")
def part_1() -> None:
for n in numbers:
for i in range(len(numbers)):
if n + numbers[i] == 2020:
print(f"{n} times {numbers[i]} is {n*numbers[i]}")
break
else:
continue
break
# --- Part Two ---
# The Elves in accounting are thankful for your help; one of them even offers
# you a starfish coin they had left over from a past vacation. They offer you
# a second one if you can find three numbers in your expense report that meet
# the same criteria.
# Using the above example again, the three entries that sum to 2020 are 979,
# 366, and 675. Multiplying them together produces the answer, 241861950.
# In your expense report, what is the product of the three entries that sum to
# 2020?
def part_2() -> None:
for n in numbers:
for i in range(len(numbers)):
for j in range(len(numbers)):
if n + numbers[i] + numbers[j] == 2020:
print(
f"{n} times {numbers[i]} times {numbers[j]} is "
f"{n*numbers[i]*numbers[j]}"
)
break
else:
continue
break
else:
continue
break
else:
continue
break
if __name__ == "__main__":
part_1()
part_2()