From 4f7907b17c6f13b992d396ead682fcea41c2148e Mon Sep 17 00:00:00 2001 From: Pascal Lais Date: Sun, 5 Dec 2021 14:29:50 +0100 Subject: [PATCH] Use pathlib for input.txt path --- day-01/day-01.py | 7 +++++-- day-02/day-02.py | 12 +++++++----- day-03/day-03.py | 12 +++++++----- day-04/day-04.py | 12 +++++++----- day-05/day-05.py | 14 +++++++------- template.py | 11 ++++++----- 6 files changed, 39 insertions(+), 29 deletions(-) diff --git a/day-01/day-01.py b/day-01/day-01.py index 707c57b..13b2762 100644 --- a/day-01/day-01.py +++ b/day-01/day-01.py @@ -1,4 +1,6 @@ #!/usr/bin/python3 +from pathlib import Path + def part_1(input): last_number = int(input[0]) @@ -34,8 +36,9 @@ def alt_solution(input, num_summands): input = list() -with open('input.txt') as fp: - input = fp.readlines() +p = Path(__file__).with_name('input.txt') +with open(p) as f: + input = f.readlines() part_1(input) part_2(input) diff --git a/day-02/day-02.py b/day-02/day-02.py index 04a4711..a9cf99b 100644 --- a/day-02/day-02.py +++ b/day-02/day-02.py @@ -1,4 +1,6 @@ #!/usr/bin/python3 +from pathlib import Path + def part_1(input): h_pos = 0 @@ -33,8 +35,8 @@ def part_2(input): input = list() -with open('input.txt') as fp: - input = fp.readlines() - -part_1(input) -part_2(input) +p = Path(__file__).with_name('input.txt') +with open(p) as f: + input = f.readlines() + part_1(input) + part_2(input) diff --git a/day-03/day-03.py b/day-03/day-03.py index 1e5068f..cf7e6c3 100644 --- a/day-03/day-03.py +++ b/day-03/day-03.py @@ -1,4 +1,6 @@ #!/usr/bin/python3 +from pathlib import Path + def part_1(input): result = 0 @@ -59,8 +61,8 @@ def part_2(input): input = list() -with open('input.txt') as fp: - input = fp.readlines() - -part_1(input) -part_2(input) +p = Path(__file__).with_name('input.txt') +with open(p) as f: + input = f.readlines() + part_1(input) + part_2(input) diff --git a/day-04/day-04.py b/day-04/day-04.py index a832d3e..bceb51d 100644 --- a/day-04/day-04.py +++ b/day-04/day-04.py @@ -1,4 +1,6 @@ #!/usr/bin/python3 +from pathlib import Path + def read_boards(input): boards = list() @@ -129,8 +131,8 @@ def part_2(input): input = list() -with open('input.txt') as fp: - input = fp.readlines() - -part_1(input.copy()) -part_2(input.copy()) +p = Path(__file__).with_name('input.txt') +with open(p) as f: + input = f.readlines() + part_1(input.copy()) + part_2(input.copy()) diff --git a/day-05/day-05.py b/day-05/day-05.py index 7d0c62d..8ddee59 100644 --- a/day-05/day-05.py +++ b/day-05/day-05.py @@ -1,5 +1,5 @@ -#!/usr/bin/phon3 - +#!/usr/bin/phyton3 +from pathlib import Path from collections import defaultdict @@ -44,8 +44,8 @@ def part_2(input): input = list() -with open('input.txt') as fp: - input = fp.readlines() - -part_1(input) -part_2(input) +p = Path(__file__).with_name('input.txt') +with open(p) as f: + input = f.readlines() + part_1(input) + part_2(input) diff --git a/template.py b/template.py index 5e2a3ec..b1b6d75 100644 --- a/template.py +++ b/template.py @@ -1,4 +1,5 @@ #!/usr/bin/python3 +from pathlib import Path def part_1(input): result = 0 @@ -15,8 +16,8 @@ def part_2(input): input = list() -with open('input.txt') as fp: - input = fp.readlines() - -part_1(input) -part_2(input) +p = Path(__file__).with_name('input.txt') +with open(p) as f: + input = f.readlines() + part_1(input) + part_2(input)