Solution to problem 1 part 2 in Python
This commit is contained in:
55
src/P1.py
55
src/P1.py
@@ -37,13 +37,52 @@
|
|||||||
# to 2020; what do you get if you multiply them together?
|
# to 2020; what do you get if you multiply them together?
|
||||||
|
|
||||||
with open("files/P1.txt", "r") as f:
|
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)):
|
def part_1() -> None:
|
||||||
if n + numbers[i] == 2020:
|
for n in numbers:
|
||||||
print(f"{n} times {numbers[i]} is {n*numbers[i]}")
|
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
|
break
|
||||||
else:
|
else:
|
||||||
continue
|
continue
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
part_1()
|
||||||
|
part_2()
|
||||||
|
|||||||
Reference in New Issue
Block a user