Add functional solution to part 2 day 1
This commit is contained in:
parent
6bb9f89a4b
commit
6fbf7f20b1
20
day1/main.py
20
day1/main.py
|
@ -1,4 +1,5 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
import functools
|
||||||
|
|
||||||
|
|
||||||
def part1():
|
def part1():
|
||||||
|
@ -20,6 +21,25 @@ def part2():
|
||||||
print(total)
|
print(total)
|
||||||
|
|
||||||
|
|
||||||
|
# Somewhat of a weird solution, but by combining reduce with a recursive solution, we can get the correct answer :)
|
||||||
|
def part2_alternate():
|
||||||
|
input_file = open('input.txt', 'r')
|
||||||
|
|
||||||
|
def reducer(total, cost):
|
||||||
|
next_cost = cost//3 - 2
|
||||||
|
if next_cost <= 0:
|
||||||
|
# If the next cost is less than or equal to zero, the current cost we have is the total cost
|
||||||
|
return total
|
||||||
|
|
||||||
|
return total + reducer(next_cost, next_cost)
|
||||||
|
|
||||||
|
total = functools.reduce(reducer, (int(line) for line in input_file), 0)
|
||||||
|
|
||||||
|
print(total)
|
||||||
|
input_file.close()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
part1()
|
part1()
|
||||||
part2()
|
part2()
|
||||||
|
part2_alternate()
|
||||||
|
|
Loading…
Reference in a new issue