-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday05_polymer.py
45 lines (33 loc) · 1.32 KB
/
day05_polymer.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
TEST_INPUT = "dabAcCaCBAcCcaDA"
# ascii difference between large and small is
ASCII_DIFFERENCE = abs(ord("A") - ord("a"))
def reaction(polymer: str) -> int:
reduced_polymer = polymer
start_loc = 0
while True:
for i, j in zip(reduced_polymer[start_loc:], reduced_polymer[start_loc + 1:]):
if abs(ord(i) - ord(j)) == ASCII_DIFFERENCE:
loc = reduced_polymer.find(i + j)
reduced_polymer = reduced_polymer[:loc] + reduced_polymer[loc + 2:]
start_loc = loc - 1 if loc >= 1 else 0
break
else:
break
return len(reduced_polymer)
assert reaction(TEST_INPUT) == 10
def remove_unit_reaction(polymer: str) -> int:
min_length = float("inf")
for ascii_char in range(ord("A"), ord("Z") + 1):
ucase = chr(ascii_char)
lcase = chr(ascii_char + ASCII_DIFFERENCE)
reduced_polymer = polymer.replace(ucase, "").replace(lcase, "")
reduced_length = reaction(reduced_polymer)
if reduced_length < min_length:
min_length = reduced_length
return min_length
assert remove_unit_reaction(TEST_INPUT) == 4
if __name__ == "__main__":
with open("data/day05_input.txt", "r") as f:
line = f.readline().split("\n")[0]
print(reaction(line))
print(remove_unit_reaction(line))