diff --git a/2020/day1/day1.py b/2020/day1/day1.py index 2123615..a1913e1 100644 --- a/2020/day1/day1.py +++ b/2020/day1/day1.py @@ -17,5 +17,8 @@ def part2(inp): if __name__ == "__main__": - with open("input.txt") as f: - part2(f.readlines()) + import fileinput + lines = [x for x in fileinput.input()] + part1(lines) + part2(lines) + diff --git a/2020/day10/day10.py b/2020/day10/day10.py index d667976..3c4c54f 100644 --- a/2020/day10/day10.py +++ b/2020/day10/day10.py @@ -18,7 +18,13 @@ def part2(adapters): return max(counts.values()) -if __name__ == "__main__": - adapters = sorted(int(l.rstrip()) for l in open("input.txt")) +def main(f): + adapters = sorted(int(l.rstrip()) for l in f) print(part1(adapters)) print(part2(adapters)) + + +if __name__ == "__main__": + import fileinput + main(fileinput.input()) + diff --git a/2020/day11/day11.py b/2020/day11/day11.py index d92b3f1..f3f709a 100644 --- a/2020/day11/day11.py +++ b/2020/day11/day11.py @@ -27,7 +27,8 @@ def step(grid, rules): if __name__ == "__main__": - with open("input.txt") as infile: + import sys + with open(sys.argv[1]) as infile: grid = list("".join(infile.read().splitlines())) print("Part 1 ", main(grid, rules=part1_rules)) print("Part 2 ", main(grid, rules=part2_rules)) diff --git a/2020/day2/day2.py b/2020/day2/day2.py new file mode 100644 index 0000000..d7d62ff --- /dev/null +++ b/2020/day2/day2.py @@ -0,0 +1,14 @@ +import part1 +import part2 + + +def main(lines): + part1.main(lines) + part2.main(lines) + + +if __name__ == "__main__": + import fileinput + lines = [x for x in fileinput.input()] + main(lines) + diff --git a/2020/day2/part1.py b/2020/day2/part1.py index da33a13..b4e2e02 100644 --- a/2020/day2/part1.py +++ b/2020/day2/part1.py @@ -15,14 +15,10 @@ def test_password(line): return count in repeat_range -def main(inp): - with open(inp) as passwds: - valid_counter = 0 - for l in passwds: - if test_password(l): - valid_counter += 1 - print(f"Number of valid password in input : {valid_counter}") +def main(passwds): + valid_counter = 0 + for l in passwds: + if test_password(l): + valid_counter += 1 + print(f"Number of valid password in input : {valid_counter}") - -if __name__ == "__main__": - main('./input.txt') diff --git a/2020/day2/part2.py b/2020/day2/part2.py index b6fca22..a931699 100644 --- a/2020/day2/part2.py +++ b/2020/day2/part2.py @@ -19,14 +19,10 @@ def test_password(line): pwd[second_pos - 1] == letter) -def main(inp): - with open(inp) as passwds: - valid_counter = 0 - for l in passwds: - if test_password(l): - valid_counter += 1 - print(f"Number of valid password in input : {valid_counter}") +def main(passwds): + valid_counter = 0 + for l in passwds: + if test_password(l): + valid_counter += 1 + print(f"Number of valid password in input : {valid_counter}") - -if __name__ == "__main__": - main('./input.txt') diff --git a/2020/day3/day3.py b/2020/day3/day3.py index c1daf39..fd5a06f 100644 --- a/2020/day3/day3.py +++ b/2020/day3/day3.py @@ -15,14 +15,12 @@ def check_slope_for_trees(plan, x_right=3, y_down=1): return tree_count -def part1(inp): - plan = [line.rstrip() for line in open(inp)] +def part1(plan): tree_count = check_slope_for_trees(plan) print(f"number of trees : {tree_count}") -def part2(inp): - plan = [line.rstrip() for line in open(inp)] +def part2(plan): slopes = [{"x": 1, "y": 1}, {"x": 3, "y": 1}, {"x": 5, "y": 1}, {"x": 7, "y": 1}, {"x": 1, "y": 2}] tree_product = 1 for slope in slopes: @@ -31,6 +29,14 @@ def part2(inp): print(f"slope {slope} number of trees : {tree_count}") print(f"Cumulative product of tress : {tree_product}") + +def main(f): + lines = [line.rstrip() for line in f] + part1(lines) + part2(lines) + + if __name__ == "__main__": - part1('input.txt') - part2('input.txt') + import fileinput + main(fileinput.input()) + diff --git a/2020/day4/day4.py b/2020/day4/day4.py index 519da43..f2c7be6 100644 --- a/2020/day4/day4.py +++ b/2020/day4/day4.py @@ -3,17 +3,20 @@ import re def main(inp): - inp = open(inp).read().split('\n\n') - valid_passeports = 0 + inp = open(inp).read().rstrip().split('\n\n') + valid_passeports_1, valid_passeports_2 = 0, 0 for l in inp: - l = re.split('[\n\s]', l) + l = re.split(r'[\n\s]', l) passeport = dict(p.split(':') for p in l) if check_passeport(passeport): - valid_passeports += 1 - print("Valid passeports ", valid_passeports) + valid_passeports_1 += 1 + if check_passeport(passeport, run_validators=True): + valid_passeports_2 += 1 + print("Part 1: valid passeports: ", valid_passeports_1) + print("Part 2: valid passeports: ", valid_passeports_2) -def check_passeport(passeport): +def check_passeport(passeport, run_validators=False): fields = [ ('byr', lambda v: 1920 <= int(v) <= 2002), # (Birth Year) ('iyr', lambda v: 2010 <= int(v) <= 2020), # (Issue Year) @@ -28,7 +31,7 @@ def check_passeport(passeport): value = passeport.get(field) if value is None: return False - elif not validator(value): + elif run_validators and not validator(value): return False return True @@ -44,4 +47,6 @@ def validate_height(v): if __name__ == "__main__": - main('input.txt') + import sys + main(sys.argv[1]) + diff --git a/2020/day5/day5.py b/2020/day5/day5.py index df143ce..8ed43b2 100644 --- a/2020/day5/day5.py +++ b/2020/day5/day5.py @@ -3,20 +3,18 @@ from itertools import product from bisect import bisect -def main(filename): +def main(inp): results = {} - with open(filename) as inp: - for line in inp: - boarding_pass = line.rstrip() - row, col = parse_boarding_pass(boarding_pass) - seat_id = get_seat_id(row, col) - results[boarding_pass] = (row, col, seat_id) + for line in inp: + boarding_pass = line.rstrip() + row, col = parse_boarding_pass(boarding_pass) + seat_id = get_seat_id(row, col) + results[boarding_pass] = (row, col, seat_id) part1(results) part2(results) def part1(results): - # part 1 max_seat_id = max(x[2] for x in results.values()) print("Max seat ID: ", max_seat_id) @@ -66,4 +64,5 @@ def get_seat_id(row, col): if __name__ == "__main__": - main('input.txt') + import fileinput + main(fileinput.input()) diff --git a/2020/day6/day6.py b/2020/day6/day6.py index c3f8a72..3491109 100644 --- a/2020/day6/day6.py +++ b/2020/day6/day6.py @@ -2,8 +2,7 @@ from collections import Counter -def part1(inp): - groups = open(inp).read().split("\n\n") +def part1(groups): number_of_questions = 0 for group in groups: unique_questions = set(group.replace("\n", "")) @@ -11,11 +10,9 @@ def part1(inp): print(number_of_questions) -def part2(inp): +def part2(groups): # number of questions for which everyone in a group answered 'yes' number_of_questions = 0 - groups = open(inp).read().split("\n\n") - for group in groups: group_length = group.count("\n") + 1 group_counter = Counter(group.replace("\n", "")) @@ -25,5 +22,9 @@ def part2(inp): if __name__ == "__main__": - part1("input.txt") - part2("input.txt") + import sys + inp = sys.argv[1] + groups = open(inp).read().split("\n\n") + part1(groups) + part2(groups) + diff --git a/2020/day7/day7.py b/2020/day7/day7.py index 0a49c32..cdba546 100644 --- a/2020/day7/day7.py +++ b/2020/day7/day7.py @@ -3,19 +3,18 @@ import re from collections import defaultdict, deque -def main(inp): - with open(inp) as input_rules: - rules = parse_rules(input_rules) - reverse_rules = build_reverse_rules(rules) - print(part1(reverse_rules)) - print(part2(rules, "shiny gold")) +def main(input_rules): + rules = parse_rules(input_rules) + reverse_rules = build_reverse_rules(rules) + print(part1(reverse_rules)) + print(part2(rules, "shiny gold")) def parse_rules(input_rules): rules = {} for input_rule in input_rules: color, rule = input_rule.split(" bags contain ") - rules[color] = {color: int(number) for number, color in re.findall('(\d+) (\w+ \w+)', rule)} + rules[color] = {color: int(number) for number, color in re.findall(r'(\d+) (\w+ \w+)', rule)} return rules @@ -44,4 +43,5 @@ def part2(rules, color): if __name__ == "__main__": - main("input.txt") + import fileinput + main(fileinput.input()) diff --git a/2020/day8/day8.py b/2020/day8/day8.py index 81ed6f0..f3b26be 100644 --- a/2020/day8/day8.py +++ b/2020/day8/day8.py @@ -43,7 +43,13 @@ def generate_permutation(instructions, line, index): return permutation -if __name__ == "__main__": - instructions = [line.rstrip() for line in open("input.txt")] +def main(inp): + instructions = [line.rstrip() for line in fileinput.input()] print("Part 1 : (ip, acc) ", part1(instructions)[1]) print("Part 2 : (ip, acc) ", part2(instructions)) + + +if __name__ == "__main__": + import fileinput + main(fileinput.input()) + diff --git a/2020/day9/day9.py b/2020/day9/day9.py index 2c739fe..5bddf1e 100644 --- a/2020/day9/day9.py +++ b/2020/day9/day9.py @@ -2,14 +2,12 @@ import itertools -def part1(inp): +def part1(lines): preamble_size = 25 - with open(inp) as infile: - cleanfile = (int(l.rstrip()) for l in infile) - for nums in window(cleanfile, preamble_size + 1): - candidate = nums[-1] - if not test_number(candidate, nums[:-1]): - return candidate + for nums in window(lines, preamble_size + 1): + candidate = nums[-1] + if not test_number(candidate, nums[:-1]): + return candidate return -1 @@ -28,8 +26,7 @@ def test_number(num, previous): return num in sums -def part2(infile, target: int): - lines = [int(l.rstrip()) for l in open(infile).readlines()] +def part2(lines, target: int): total = 0 visited = [] for index, _ in enumerate(lines): @@ -44,7 +41,14 @@ def part2(infile, target: int): total = 0 -if __name__ == "__main__": - invalid_number = part1("input.txt") +def main(f): + lines = [int(l.rstrip()) for l in f] + invalid_number = part1(lines) print("part1 ", invalid_number) - print("part2 ", part2("input.txt", invalid_number)) + print("part2 ", part2(lines, invalid_number)) + + +if __name__ == "__main__": + import fileinput + main(fileinput.input()) + diff --git a/run.py b/run.py index c5dacc3..78d7f97 100644 --- a/run.py +++ b/run.py @@ -50,7 +50,7 @@ def main(year, day): input_path = path / Path("input.txt") if script_path.exists(): if not input_path.exists(): - print(f"Downloading input file {input_path}") + print(f"- downloading input file {input_path}") get_auth() with open(input_path, "wb") as f: res = get_input_file(year, day) @@ -60,13 +60,14 @@ def main(year, day): def run_day(script_path, input_path): try: + print(f"> running {script_path}") start = time.time() res = subprocess.run([sys.executable, script_path.absolute(), input_path.absolute()], check=True, stdout=subprocess.PIPE, timeout=30) elapsed = time.time() - start - #print(res.stdout) - print(f"ran {script_path} in {elapsed:.3f}s") + print(res.stdout.decode()) + print(f"> ran {script_path} in {elapsed:.3f}s") except subprocess.TimeoutExpired: - print(f"timeout {script_path} after 30s", file=sys.stderr) + print(f"> timeout {script_path} after 30s", file=sys.stderr) if __name__ == "__main__":