diff --git a/2018/day1/day1.py b/2018/day1/day1.py index 9e05675..6404f56 100644 --- a/2018/day1/day1.py +++ b/2018/day1/day1.py @@ -17,6 +17,8 @@ def main(inp): if __name__ == '__main__': - 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: main(inp.readlines()) diff --git a/2019/day1/day1.py b/2019/day1/day1.py index 55009a3..057a266 100644 --- a/2019/day1/day1.py +++ b/2019/day1/day1.py @@ -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), ) diff --git a/2019/day2/day2.py b/2019/day2/day2.py index 7913f2e..1725bc3 100644 --- a/2019/day2/day2.py +++ b/2019/day2/day2.py @@ -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)) diff --git a/2019/day3/day3.py b/2019/day3/day3.py index 7188378..7729105 100644 --- a/2019/day3/day3.py +++ b/2019/day3/day3.py @@ -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)) diff --git a/2021/day1/day1.py b/2021/day1/day1.py index adef306..0fd2fa1 100644 --- a/2021/day1/day1.py +++ b/2021/day1/day1.py @@ -40,5 +40,7 @@ def part2(infile): if __name__ == "__main__": - part1("./input.txt") - part2("./input.txt") + import sys + infile = sys.argv[1] if len(sys.argv) > 1 else "example.txt" + part1(infile) + part2(infile) diff --git a/2021/day2/day2.py b/2021/day2/day2.py index 64021a6..89a8850 100644 --- a/2021/day2/day2.py +++ b/2021/day2/day2.py @@ -39,4 +39,6 @@ def main(input_file): if __name__ == "__main__": - main("input.txt") + import sys + infile = sys.argv[1] if len(sys.argv) > 1 else "example.txt" + main(infile) diff --git a/2021/day3/day3.py b/2021/day3/day3.py index 7b7d0ff..e17162a 100644 --- a/2021/day3/day3.py +++ b/2021/day3/day3.py @@ -64,4 +64,6 @@ def main(input_file): if __name__ == "__main__": - main("input.txt") + import sys + infile = sys.argv[1] if len(sys.argv) > 1 else "example.txt" + main(infile) diff --git a/2021/day4/day4.py b/2021/day4/day4.py index 98ece64..d657a80 100644 --- a/2021/day4/day4.py +++ b/2021/day4/day4.py @@ -94,4 +94,6 @@ def main(input_file): if __name__ == "__main__": - main("input.txt") + import sys + infile = sys.argv[1] if len(sys.argv) > 1 else "example.txt" + main(infile) diff --git a/2021/day5/day5.py b/2021/day5/day5.py index 3128904..dcc6313 100644 --- a/2021/day5/day5.py +++ b/2021/day5/day5.py @@ -35,7 +35,7 @@ def main(infile): print(res) - - if __name__ == "__main__": - main("input.txt") + import sys + infile = sys.argv[1] if len(sys.argv) > 1 else "example.txt" + main(infile) diff --git a/2021/day6/day6.py b/2021/day6/day6.py index bac503d..c2106f6 100644 --- a/2021/day6/day6.py +++ b/2021/day6/day6.py @@ -23,4 +23,6 @@ def main(infile): print(f"Part 2, {res}") if __name__ == "__main__": - main("input.txt") + import sys + infile = sys.argv[1] if len(sys.argv) > 1 else "example.txt" + main(infile) diff --git a/2021/day6/day6_2.py b/2021/day6/day6_2.py index d7f5908..565baf2 100644 --- a/2021/day6/day6_2.py +++ b/2021/day6/day6_2.py @@ -31,4 +31,4 @@ def rotate(l): for j in range(256): numbers = rotate(numbers) numbers[6] += numbers[8] - print(f'DAY {j+1} AMOUNT OF FISH: {sum(numbers)}') \ No newline at end of file + print(f'DAY {j+1} AMOUNT OF FISH: {sum(numbers)}') diff --git a/2021/day7/day7.py b/2021/day7/day7.py index d7f487a..d6a2ee0 100644 --- a/2021/day7/day7.py +++ b/2021/day7/day7.py @@ -26,8 +26,10 @@ def part2(crabs): if __name__ == "__main__": - with open("input.txt") as infile: - crabs = [int(x) for x in infile.readline().split(",")] + import sys + infile = sys.argv[1] if len(sys.argv) > 1 else "example.txt" + with open(infile) as f: + crabs = [int(x) for x in f.readline().split(",")] part1(crabs) part2(crabs) diff --git a/2021/day8/day8.py b/2021/day8/day8.py index 62d76fb..2fc3930 100644 --- a/2021/day8/day8.py +++ b/2021/day8/day8.py @@ -43,4 +43,6 @@ def main(infile): if __name__ == "__main__": - main("input.txt") + import sys + infile = sys.argv[1] if len(sys.argv) > 1 else "example.txt" + main(infile)