Skip to content

Commit c56707e

Browse files
committed
2024 Day 25 Complete - 500 stars
1 parent 4979a32 commit c56707e

2 files changed

Lines changed: 165 additions & 0 deletions

File tree

2024/25/daily.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import itertools
2+
import sys
3+
4+
def parse(input_text):
5+
locks = []
6+
keys = []
7+
8+
working_lock_key = []
9+
for line in input_text.strip().split('\n'):
10+
if not line:
11+
if working_lock_key:
12+
if working_lock_key[0][0] == '.':
13+
keys.append(working_lock_key)
14+
else:
15+
locks.append(working_lock_key)
16+
working_lock_key = []
17+
continue
18+
19+
working_lock_key.append(list(line))
20+
21+
if working_lock_key[0][0] == '.':
22+
keys.append(working_lock_key)
23+
else:
24+
locks.append(working_lock_key)
25+
return locks, keys
26+
27+
def pin_heights(lock_or_key):
28+
print(lock_or_key)
29+
lock_range = range(1, len(lock_or_key))
30+
key = False
31+
32+
# It's a key, read backwards and skip the first element
33+
if lock_or_key[0][0] == '.':
34+
key = True
35+
lock_range = range(len(lock_or_key) - 1, 0, -1)
36+
37+
pin_heights = [0] * len(lock_or_key[0])
38+
for i in lock_range:
39+
print(f"Row: {i} - {lock_or_key[i]}")
40+
for j in range(len(lock_or_key[i])):
41+
if lock_or_key[i][j] == '#':
42+
if key:
43+
pin_heights[j] = len(lock_or_key) - i - 1
44+
else:
45+
pin_heights[j] = i
46+
47+
return pin_heights
48+
49+
def overlap(lock, key):
50+
# zip lock and key together
51+
for l, k in zip(lock, key):
52+
if l + k > 5:
53+
return True
54+
55+
return False
56+
57+
def part1(input_text):
58+
locks, keys = parse(input_text)
59+
60+
lock_pins = []
61+
for l in locks:
62+
lock_pins.append(pin_heights(l))
63+
64+
key_pins = []
65+
for k in keys:
66+
key_pins.append(pin_heights(k))
67+
68+
counter = 0
69+
for l, k in itertools.product(lock_pins, key_pins):
70+
if not overlap(l, k):
71+
counter += 1
72+
73+
return counter
74+
75+
def part2(input_text):
76+
return 0
77+
78+
if __name__ == "__main__":
79+
with open(__file__.rsplit('/', 1)[0] + "/input.txt", 'r') as file:
80+
print(part1(file.read()))
81+
82+
with open(__file__.rsplit('/', 1)[0] + "/input.txt", 'r') as file:
83+
print(part2(file.read()))

2024/25/daily_test.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import pytest
2+
from daily import *
3+
4+
@pytest.fixture
5+
def sample_data():
6+
return """
7+
#####
8+
.####
9+
.####
10+
.####
11+
.#.#.
12+
.#...
13+
.....
14+
15+
#####
16+
##.##
17+
.#.##
18+
...##
19+
...#.
20+
...#.
21+
.....
22+
23+
.....
24+
#....
25+
#....
26+
#...#
27+
#.#.#
28+
#.###
29+
#####
30+
31+
.....
32+
.....
33+
#.#..
34+
###..
35+
###.#
36+
###.#
37+
#####
38+
39+
.....
40+
.....
41+
.....
42+
#....
43+
#.#..
44+
#.#.#
45+
#####
46+
"""
47+
48+
@pytest.fixture
49+
def parsed_data(sample_data):
50+
return parse(sample_data)
51+
52+
def test_parse(sample_data):
53+
locks, keys = parse(sample_data)
54+
55+
assert len(locks) == 2
56+
assert len(keys) == 3
57+
58+
def test_pin_heights(parsed_data):
59+
locks, keys = parsed_data
60+
61+
assert pin_heights(locks[0]) == [0,5,3,4,3]
62+
assert pin_heights(locks[1]) == [1,2,0,5,3]
63+
assert pin_heights(keys[0]) == [5,0,2,1,3]
64+
assert pin_heights(keys[1]) == [4,3,4,0,2]
65+
assert pin_heights(keys[2]) == [3,0,2,0,1]
66+
67+
def test_overlap(parsed_data):
68+
locks, keys = parsed_data
69+
70+
assert overlap(pin_heights(locks[0]), pin_heights(keys[0]))
71+
assert overlap(pin_heights(locks[0]), pin_heights(keys[1]))
72+
assert overlap(pin_heights(locks[1]), pin_heights(keys[0]))
73+
74+
assert not overlap(pin_heights(locks[0]), pin_heights(keys[2]))
75+
assert not overlap(pin_heights(locks[1]), pin_heights(keys[1]))
76+
assert not overlap(pin_heights(locks[1]), pin_heights(keys[2]))
77+
78+
def test_part1(sample_data):
79+
assert part1(sample_data) == 3
80+
81+
# def test_part2(sample_data):
82+
# assert part2(sample_data) == 81

0 commit comments

Comments
 (0)