-
Notifications
You must be signed in to change notification settings - Fork 0
/
day01.py
44 lines (33 loc) · 1.07 KB
/
day01.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import functools, time
import aoc
def main():
aoc.header("The Tyranny of the Rocket Equation")
aoc.run_tests()
aoc.output(1, part1)
aoc.output(2, part2)
def part1():
return sum(map(get_fuel_required, map(int, aoc.get_input().readlines())))
def part2():
return sum(map(get_fuel_required_recursive, map(int, aoc.get_input().readlines())))
def test():
# part 1
assert get_fuel_required(12) == 2
assert get_fuel_required(14) == 2
assert get_fuel_required(1969) == 654
assert get_fuel_required(100756) == 33583
# part 2
assert get_fuel_required_recursive(14) == 2
assert get_fuel_required_recursive(1969) == 966
assert get_fuel_required_recursive(100756) == 50346
print("✓ All tests passed!")
@functools.lru_cache
def get_fuel_required(mass : int):
return max((mass // 3) - 2, 0)
@functools.lru_cache
def get_fuel_required_recursive(mass : int):
base = get_fuel_required(mass)
if base > 8:
return base + get_fuel_required_recursive(base)
else: return base
if __name__ == "__main__":
main()