From 6fbf7f20b110afebb198af6c3fa96c986a9db37c Mon Sep 17 00:00:00 2001 From: Nick Krichevsky Date: Sun, 1 Dec 2019 00:44:21 -0500 Subject: [PATCH] Add functional solution to part 2 day 1 --- day1/main.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/day1/main.py b/day1/main.py index b6afa79..86a07ce 100644 --- a/day1/main.py +++ b/day1/main.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +import functools def part1(): @@ -20,6 +21,25 @@ def part2(): 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__': part1() part2() + part2_alternate()