Skip to content

Commit 530b7c9

Browse files
committed
Homogenize input and output handling
1 parent 4076578 commit 530b7c9

36 files changed

+555
-527
lines changed

src/main/python/AoC2015_01.py

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77

88

99
def part_1(inputs: tuple[str]) -> int:
10+
assert len(inputs) == 1
1011
return len(inputs[0]) - 2 * inputs[0].count(")")
1112

1213

1314
def part_2(inputs: tuple[str]) -> int:
15+
assert len(inputs) == 1
1416
sum_ = int()
1517
for i, c in enumerate(inputs[0]):
1618
if c == "(":
@@ -24,35 +26,35 @@ def part_2(inputs: tuple[str]) -> int:
2426
raise RuntimeError("Unreachable")
2527

2628

27-
test1 = "(())".splitlines()
28-
test2 = "()()".splitlines()
29-
test3 = "(((".splitlines()
30-
test4 = "(()(()(".splitlines()
31-
test5 = "))(((((".splitlines()
32-
test6 = "())".splitlines()
33-
test7 = "))(".splitlines()
34-
test8 = ")))".splitlines()
35-
test9 = ")())())".splitlines()
36-
test10 = ")".splitlines()
37-
test11 = "()())".splitlines()
29+
TEST1 = "(())".splitlines()
30+
TEST2 = "()()".splitlines()
31+
TEST3 = "(((".splitlines()
32+
TEST4 = "(()(()(".splitlines()
33+
TEST5 = "))(((((".splitlines()
34+
TEST6 = "())".splitlines()
35+
TEST7 = "))(".splitlines()
36+
TEST8 = ")))".splitlines()
37+
TEST9 = ")())())".splitlines()
38+
TEST10 = ")".splitlines()
39+
TEST11 = "()())".splitlines()
3840

3941

4042
def main() -> None:
4143
my_aocd.print_header(2015, 1)
4244

43-
assert part_1(test1) == 0
44-
assert part_1(test2) == 0
45-
assert part_1(test3) == 3
46-
assert part_1(test4) == 3
47-
assert part_1(test5) == 3
48-
assert part_1(test6) == -1
49-
assert part_1(test7) == -1
50-
assert part_1(test8) == -3
51-
assert part_1(test9) == -3
52-
assert part_2(test10) == 1
53-
assert part_2(test11) == 5
54-
55-
inputs = my_aocd.get_input_as_tuple(2015, 1, 1)
45+
assert part_1(TEST1) == 0
46+
assert part_1(TEST2) == 0
47+
assert part_1(TEST3) == 3
48+
assert part_1(TEST4) == 3
49+
assert part_1(TEST5) == 3
50+
assert part_1(TEST6) == -1
51+
assert part_1(TEST7) == -1
52+
assert part_1(TEST8) == -3
53+
assert part_1(TEST9) == -3
54+
assert part_2(TEST10) == 1
55+
assert part_2(TEST11) == 5
56+
57+
inputs = my_aocd.get_input(2015, 1, 1)
5658
result1 = part_1(inputs)
5759
print(f"Part 1: {result1}")
5860
result2 = part_2(inputs)

src/main/python/AoC2015_02.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,19 @@ def part_2(inputs: tuple[str]) -> int:
4747
return sum([_calculate_required_length(p) for p in presents])
4848

4949

50-
test1 = "2x3x4".splitlines()
51-
test2 = "1x1x10".splitlines()
50+
TEST1 = "2x3x4".splitlines()
51+
TEST2 = "1x1x10".splitlines()
5252

5353

5454
def main() -> None:
5555
my_aocd.print_header(2015, 2)
5656

57-
assert part_1(test1) == 58
58-
assert part_1(test2) == 43
59-
assert part_2(test1) == 34
60-
assert part_2(test2) == 14
57+
assert part_1(TEST1) == 58
58+
assert part_1(TEST2) == 43
59+
assert part_2(TEST1) == 34
60+
assert part_2(TEST2) == 14
6161

62-
inputs = my_aocd.get_input_as_tuple(2015, 2, 1000)
62+
inputs = my_aocd.get_input(2015, 2, 1000)
6363
result1 = part_1(inputs)
6464
print(f"Part 1: {result1}")
6565
result2 = part_2(inputs)

src/main/python/AoC2015_03.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def main() -> None:
7070
assert part_2(TEST2) == 3
7171
assert part_2(TEST3) == 11
7272

73-
inputs = my_aocd.get_input_as_tuple(2015, 3, 1)
73+
inputs = my_aocd.get_input(2015, 3, 1)
7474
result1 = part_1(inputs)
7575
print(f"Part 1: {result1}")
7676
result2 = part_2(inputs)

src/main/python/AoC2015_04.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,18 @@ def _find_md5_starting_with_zeroes(input_: str, zeroes: int) -> int:
2020
return i
2121

2222

23-
def part_1(input_: str) -> int:
24-
return _find_md5_starting_with_zeroes(input_, 5)
23+
def part_1(inputs: tuple[str]) -> int:
24+
assert len(inputs) == 1
25+
return _find_md5_starting_with_zeroes(inputs[0], 5)
2526

2627

27-
def part_2(input_: str) -> int:
28-
return _find_md5_starting_with_zeroes(input_, 6)
28+
def part_2(inputs: tuple[str]) -> int:
29+
assert len(inputs) == 1
30+
return _find_md5_starting_with_zeroes(inputs[0], 6)
2931

3032

31-
TEST1 = "abcdef"
32-
TEST2 = "pqrstuv"
33+
TEST1 = "abcdef".splitlines()
34+
TEST2 = "pqrstuv".splitlines()
3335

3436

3537
def main() -> None:
@@ -38,9 +40,10 @@ def main() -> None:
3840
assert part_1(TEST1) == 609043
3941
assert part_1(TEST2) == 1048970
4042

41-
result1 = part_1("iwrupvqb")
43+
inputs = my_aocd.get_input(2015, 4, 1)
44+
result1 = part_1(inputs)
4245
print(f"Part 1: {result1}")
43-
result2 = part_2("iwrupvqb")
46+
result2 = part_2(inputs)
4447
print(f"Part 2: {result2}")
4548

4649

src/main/python/AoC2015_05.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def main() -> None:
5252
assert part_2(TEST9) == 0
5353
assert part_2(TEST10) == 1
5454

55-
inputs = my_aocd.get_input_as_tuple(2015, 5, 1000)
55+
inputs = my_aocd.get_input(2015, 5, 1000)
5656
result1 = part_1(inputs)
5757
print(f"Part 1: {result1}")
5858
result2 = part_2(inputs)

src/main/python/AoC2015_06.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def main() -> None:
116116
assert part_2(TEST4) == 1
117117
assert part_2(TEST5) == 2_000_000
118118

119-
inputs = my_aocd.get_input_as_tuple(2015, 6, 300)
119+
inputs = my_aocd.get_input(2015, 6, 300)
120120
result1 = part_1(inputs)
121121
print(f"Part 1: {result1}")
122122
result2 = part_2(inputs)

src/main/python/AoC2015_08.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def main() -> None:
4343
assert part_1(TEST) == 12
4444
assert part_2(TEST) == 19
4545

46-
inputs = my_aocd.get_input_as_tuple(2015, 8, 300)
46+
inputs = my_aocd.get_input(2015, 8, 300)
4747
result1 = part_1(inputs)
4848
print(f"Part 1: {result1}")
4949
result2 = part_2(inputs)

src/main/python/AoC2015_10.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,15 @@ def part_2(inputs: tuple[str]) -> int:
3737
return len(_look_and_say_iterations(inputs[0], 50))
3838

3939

40-
TEST = """\
41-
1
42-
""".splitlines()
40+
TEST = "1".splitlines()
4341

4442

4543
def main() -> None:
4644
my_aocd.print_header(2015, 10)
4745

4846
assert _look_and_say_iterations(TEST, 5) == "312211"
4947

50-
inputs = my_aocd.get_input_as_tuple(2015, 10, 1)
48+
inputs = my_aocd.get_input(2015, 10, 1)
5149
result1 = part_1(inputs)
5250
print(f"Part 1: {result1}")
5351
result2 = part_2(inputs)

src/main/python/AoC2015_12.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def main() -> None:
6161
assert part_2(TEST10) == 0
6262
assert part_2(TEST11) == 6
6363

64-
inputs = my_aocd.get_input_as_tuple(2015, 12, 1)
64+
inputs = my_aocd.get_input(2015, 12, 1)
6565
result1 = part_1(inputs)
6666
print(f"Part 1: {result1}")
6767
result2 = part_2(inputs)

src/main/python/AoC2015_14.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,15 @@ def _distance_reached(reindeer: Reindeer, time: int) -> int:
4141
return periods * period_distance + reindeer.speed * left
4242

4343

44-
def part_1(inputs: tuple[str], time: int) -> int:
44+
def _do_part_1(inputs: tuple[str], time: int) -> int:
4545
return max([_distance_reached(r, time) for r in _parse(inputs)])
4646

4747

48-
def part_2(inputs: tuple[str], time: int) -> int:
48+
def part_1(inputs: tuple[str]) -> int:
49+
return _do_part_1(inputs, 2503)
50+
51+
52+
def _do_part_2(inputs: tuple[str], time: int) -> int:
4953
reindeer = _parse(inputs)
5054
points = defaultdict(int)
5155
for i in range(time):
@@ -54,6 +58,10 @@ def part_2(inputs: tuple[str], time: int) -> int:
5458
return max(points.values())
5559

5660

61+
def part_2(inputs: tuple[str]) -> int:
62+
return _do_part_2(inputs, 2503)
63+
64+
5765
TEST = """\
5866
Comet can fly 14 km/s for 10 seconds, but then must rest for 127 seconds.
5967
Dancer can fly 16 km/s for 11 seconds, but then must rest for 162 seconds.
@@ -63,13 +71,13 @@ def part_2(inputs: tuple[str], time: int) -> int:
6371
def main() -> None:
6472
my_aocd.print_header(2015, 14)
6573

66-
assert part_1(TEST, 1000) == 1120
67-
assert part_2(TEST, 1000) == 689
74+
assert _do_part_1(TEST, 1000) == 1120
75+
assert _do_part_2(TEST, 1000) == 689
6876

69-
inputs = my_aocd.get_input_as_tuple(2015, 14, 9)
70-
result1 = part_1(inputs, 2503)
77+
inputs = my_aocd.get_input(2015, 14, 9)
78+
result1 = part_1(inputs)
7179
print(f"Part 1: {result1}")
72-
result2 = part_2(inputs, 2503)
80+
result2 = part_2(inputs)
7381
print(f"Part 2: {result2}")
7482

7583

src/main/python/AoC2020_01.py

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ def _print_part_2(prefix: str, first: int, second: int, third: int,
1919
print(f"{prefix} Threesome: {first} * {second} * {third} = {result}")
2020

2121

22-
def part_1_primeagen(inputs: tuple[int]) -> int:
22+
def part_1_primeagen(inputs: tuple[str]) -> int:
2323
others = set[int]()
2424
start = time.perf_counter()
25-
for n1 in inputs:
25+
for i in range(len(inputs)):
26+
n1 = int(inputs[i])
2627
others.add(n1)
2728
check = 2020 - n1
2829
if check in others:
@@ -33,13 +34,14 @@ def part_1_primeagen(inputs: tuple[int]) -> int:
3334
return result
3435

3536

36-
def part_2_primeagen(inputs: tuple[int]) -> int:
37+
def part_2_primeagen(inputs: tuple[str]) -> int:
3738
others = set[int]()
3839
start = time.perf_counter()
3940
for i in range(len(inputs)):
40-
n1 = inputs[i]
41+
n1 = int(inputs[i])
4142
others.add(n1)
42-
for n2 in inputs[i:]:
43+
for j in range(i, len(inputs)):
44+
n2 = int(inputs[j])
4345
check = 2020 - n1 - n2
4446
if check in others:
4547
result = n1*n2*check
@@ -49,15 +51,16 @@ def part_2_primeagen(inputs: tuple[int]) -> int:
4951
return result
5052

5153

52-
def _squared(inputs: tuple[int], target: int) -> tuple[int]:
54+
def _squared(inputs: tuple[str], target: int) -> tuple[int]:
5355
for i in range(len(inputs)):
54-
n1 = inputs[i]
55-
for n2 in inputs[i:]:
56+
n1 = int(inputs[i])
57+
for j in range(i, len(inputs)):
58+
n2 = int(inputs[j])
5659
if n1 + n2 == target:
5760
return (n1, n2)
5861

5962

60-
def part_1_squared(inputs: tuple[int]) -> int:
63+
def part_1_squared(inputs: tuple[str]) -> int:
6164
start = time.perf_counter()
6265
twosome = _squared(inputs, 2020)
6366
result = twosome[0]*twosome[1]
@@ -67,10 +70,10 @@ def part_1_squared(inputs: tuple[int]) -> int:
6770
return result
6871

6972

70-
def part_2_cubed(inputs: tuple[int]) -> int:
73+
def part_2_cubed(inputs: tuple[str]) -> int:
7174
start = time.perf_counter()
7275
for i in range(len(inputs)):
73-
n1 = inputs[i]
76+
n1 = int(inputs[i])
7477
twosome = _squared(inputs[i:], 2020-n1)
7578
if twosome is not None:
7679
threesome = (twosome[0], twosome[1], n1)
@@ -83,24 +86,25 @@ def part_2_cubed(inputs: tuple[int]) -> int:
8386
return result
8487

8588

86-
test = (1721,
87-
979,
88-
366,
89-
299,
90-
675,
91-
1456,
92-
)
89+
TEST = """\
90+
1721
91+
979
92+
366
93+
299
94+
675
95+
1456
96+
""".splitlines()
9397

9498

9599
def main() -> None:
96100
my_aocd.print_header(2020, 1)
97101

98-
assert part_1_squared(test) == 514579
99-
assert part_2_cubed(test) == 241861950
100-
assert part_1_primeagen(test) == 514579
101-
assert part_2_primeagen(test) == 241861950
102+
assert part_1_squared(TEST) == 514579
103+
assert part_2_cubed(TEST) == 241861950
104+
assert part_1_primeagen(TEST) == 514579
105+
assert part_2_primeagen(TEST) == 241861950
102106

103-
inputs = my_aocd.get_input_as_ints_tuple(2020, 1, 200)
107+
inputs = my_aocd.get_input(2020, 1, 200)
104108
part_1_squared(inputs)
105109
part_1_primeagen(inputs)
106110
part_2_cubed(inputs)

0 commit comments

Comments
 (0)