-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path25.py
91 lines (69 loc) · 1.38 KB
/
25.py
1
2
3
4
5
6
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import sys
from itertools import product
def test_input():
_input = """#####
.####
.####
.####
.#.#.
.#...
.....
#####
##.##
.#.##
...##
...#.
...#.
.....
.....
#....
#....
#...#
#.#.#
#.###
#####
.....
.....
#.#..
###..
###.#
###.#
#####
.....
.....
.....
#....
#.#..
#.#.#
#####""".strip().split('\n')
keys, locks = get_keys_and_locks(_input)
assert part_1(keys, locks) == 3
def read_input(path: str):
with open(path, 'r') as f:
return [line.strip() for line in f]
def get_keys_and_locks(input_data):
keys = []
locks = []
current = None
is_key = False
for line in input_data:
if not line:
if current is not None:
(keys if is_key else locks).append(current)
current = None
continue
if current is None:
is_key = line[0] == '.'
current = [-1] * 5
current = [sum(x) for x in zip(current, [1 if c == '#' else 0 for c in line])]
if current is not None:
(keys if is_key else locks).append(current)
return keys, locks
def part_1(keys, locks):
return sum(1 for lock, key in product(locks, keys) if all(l + k <= 5 for l, k in zip(lock, key)))
def main():
input_data = read_input(sys.argv[1])
keys, locks = get_keys_and_locks(input_data)
print(f'Part 1: {part_1(keys, locks)}')
if __name__ == '__main__':
main()