accept input file as cmdline argument

This commit is contained in:
2025-01-04 15:38:12 +01:00
parent 89b6b5e107
commit 87871aed4d
13 changed files with 41 additions and 19 deletions

View File

@ -32,10 +32,13 @@ def test_part2():
if __name__ == "__main__":
print("Part 1 - total mass: ", calculate_total_fuel_mass("./input.txt"))
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("./input.txt", calculate_fuel_iterative),
calculate_total_fuel_mass(infile, calculate_fuel_iterative),
)

View File

@ -54,7 +54,9 @@ def run_program(memory, noun, verb):
if __name__ == "__main__":
tests()
with open("input.txt") as inp:
import sys
infile = sys.argv[1] if len(sys.argv) > 1 else "example.txt"
with open(infile) as inp:
memory = [int(x) for x in inp.readline().strip().split(",")]
# Pass a copy to avoid modifying the original memory
print("Part 1 answer: ", run_program(memory[:], 12, 2))

View File

@ -70,8 +70,9 @@ def tests():
if __name__ == "__main__":
tests()
with open("input.txt") as raw_input:
import sys
infile = sys.argv[1] if len(sys.argv) > 1 else "example.txt"
with open(infile) as raw_input:
lines = raw_input.readlines()
wire1, wire2 = [line.strip("\n").split(",") for line in lines]
print("Part 1 -- distance = ", find_min_distance(wire1, wire2))