Skip to content

Commit 0643d31

Browse files
committed
2024 Day 20 Part 2 Completed
1 parent edd6b25 commit 0643d31

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

2024/20/daily.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import itertools
2+
13
def parse(input_text):
24
grid = []
35
s = None
@@ -56,8 +58,23 @@ def part1(input_text, cheat_threshold=100):
5658

5759
return len([o for o in options if o >= cheat_threshold])
5860

59-
def part2(input_text):
60-
return 0
61+
def part2(input_text, cheat_threshold=100):
62+
grid, s, e = parse(input_text)
63+
steps = fill_path(grid, s, e)
64+
65+
counter = 0
66+
for (pos_a, steps_a), (pos_b, steps_b) in itertools.combinations(steps.items(), 2):
67+
distance = abs(pos_a[0] - pos_b[0]) + abs(pos_a[1] - pos_b[1])
68+
if distance > 20:
69+
continue
70+
diff = abs(steps_a - steps_b)
71+
if diff - distance >= cheat_threshold:
72+
counter += 1
73+
74+
return counter
75+
76+
# Too High - 42297218
77+
# Too low - 947532
6178

6279
if __name__ == "__main__":
6380
with open(__file__.rsplit('/', 1)[0] + "/input.txt", 'r') as file:

2024/20/daily_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,5 @@ def test_cheat_options(parsed_data):
6161
def test_part1(sample_data):
6262
assert part1(sample_data, 36) == 4
6363

64-
# def test_part2(sample_data):
65-
# assert part2(sample_data) == 16
64+
def test_part2(sample_data):
65+
assert part2(sample_data, 50) == 285

0 commit comments

Comments
 (0)