-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10.py
52 lines (41 loc) · 1.42 KB
/
10.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
with open("input10-1.txt", "r") as f:
data = f.read().splitlines()
# 10-1
total = 0
valid_open, valid_close = "([{<", ")]}>"
close_map, open_map = {")": "(", "]": "[", "}": "{", ">": "<"}, {"(": ")", "[": "]", "{": "}", "<": ">"}
pts_map, score_map, corrupt_idx = {")": 3, "]": 57, "}": 1197, ">": 25137}, {")": 1, "]": 2, "}": 3, ">": 4}, []
for j, row in enumerate(data):
opn = []
for char in row:
if char in valid_open:
opn.append(char)
if char in valid_close:
lst_opn = opn.pop()
if close_map[char] != lst_opn:
#print(f"Row {j} corrupted. Removing from incomplete")
total += pts_map[char]
corrupt_idx.append(j)
print("Res 1=", total)
data = [row for i, row in enumerate(data) if i not in corrupt_idx] # incomplete row for part 2
all_remainers = []
for j, row in enumerate(data):
opn = []
remaining = []
for char in row:
if char in valid_open:
opn.append(char)
if char in valid_close:
lst_opn = opn.pop()
for char in opn[::-1]: # reverse
remaining.append(open_map[char])
all_remainers.append(remaining)
scores = []
for remainder in all_remainers:
score = 0
for char in remainder:
score = score * 5
score = score + score_map[char]
scores.append(score)
middleIdx = int((len(scores) - 1)/2)
print("Res 2=", sorted(scores)[middleIdx])