-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay25_Code_Chronicle.py
More file actions
55 lines (37 loc) · 1.17 KB
/
Day25_Code_Chronicle.py
File metadata and controls
55 lines (37 loc) · 1.17 KB
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
from itertools import product
import os
import numpy as np
INPUT_DATA_PATH = os.path.join("data", "day25.txt")
def parse_data(text: str):
text = text.replace("#", "1").replace(".", "0")
schematics = []
for block in text.split("\n\n"):
schematic = []
for line in block.strip().split("\n"):
line = list(map(int, line))
schematic.append(line)
schematic = np.array(schematic, dtype="int")
schematics.append(schematic)
locks = []
keys = []
for schematic in schematics:
r = schematic.sum(axis=1)
if r[0] == 5 and r[-1] == 0:
locks.append(schematic)
elif r[0] == 0 and r[-1] == 5:
keys.append(schematic)
return locks, keys
def part1(text: str):
locks, keys = parse_data(text)
counter = 0
for lock, key in product(locks, keys):
lock = lock.sum(axis=0)
key = key.sum(axis=0)
pair = lock + key
if all(map(lambda x: x <= 5+2, pair)):
counter += 1
return counter
if __name__ == "__main__":
text = open(INPUT_DATA_PATH, "r").read().strip()
part1_result = part1(text)
print(part1_result)