mirror of
https://github.com/thib8956/advent-of-code.git
synced 2025-08-24 08:21:57 +00:00
chore: create new project structure and aoc.py runner script
This commit is contained in:
44
adventofcode/2019/day1/day1.py
Normal file
44
adventofcode/2019/day1/day1.py
Normal file
@@ -0,0 +1,44 @@
|
||||
#! /usr/bin/env python3
|
||||
|
||||
|
||||
def calculate_fuel_iterative(mass):
|
||||
total = 0
|
||||
new_mass = mass
|
||||
while True:
|
||||
new_mass = new_mass // 3 - 2
|
||||
if new_mass < 0:
|
||||
break
|
||||
total += new_mass
|
||||
return total
|
||||
|
||||
|
||||
def calculate_total_fuel_mass(input_file, mass_function=lambda x: x // 3 - 2):
|
||||
total = 0
|
||||
with open(input_file) as masses:
|
||||
for mass in masses:
|
||||
total += mass_function(int(mass))
|
||||
return total
|
||||
|
||||
|
||||
def test_part2():
|
||||
inputs = (14, 1969, 100756)
|
||||
expected = (2, 966, 50346)
|
||||
for i, inp in enumerate(inputs):
|
||||
result = calculate_fuel_iterative(inp)
|
||||
assert result == expected[i], "Result for {} should be {}, was {}".format(
|
||||
inp, expected[i], result
|
||||
)
|
||||
print("All tests passed for part 2.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import sys
|
||||
infile = sys.argv[1] if len(sys.argv) > 1 else "example.txt"
|
||||
|
||||
print("Part 1 - total mass: ", calculate_total_fuel_mass(infile))
|
||||
|
||||
test_part2()
|
||||
print(
|
||||
"Part 2 -- total mass: ",
|
||||
calculate_total_fuel_mass(infile, calculate_fuel_iterative),
|
||||
)
|
Reference in New Issue
Block a user