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()