-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoc5.2.py
executable file
·59 lines (43 loc) · 1.58 KB
/
aoc5.2.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
#!/usr/bin/env python3
import sys
def are_opposing_case(a, b):
if a.lower() != b.lower():
return False
return a.isupper() != b.isupper()
def react(polymer):
front = [polymer[0]]
# Store the later values backwards to allow fast access to their original
# front, now at the end of the list
back = list(reversed(polymer[1:]))
while back:
if are_opposing_case(front[-1], back[-1]):
front.pop()
back.pop()
if len(front) > 1:
back.append(front.pop())
elif len(front) == 0:
front.append(back.pop())
else:
front.append(back.pop())
return front
def main():
fname = sys.argv[1]
# Keep track of which unit types we have tried to remove
removed_units = set()
shortest_type = None
shortest = None
with open(fname) as f:
polymer = list(f.read().rstrip())
for unit in polymer:
unit_type = unit.lower()
if unit_type not in removed_units:
removed_units.add(unit_type)
# Find the length of the reacted polymer with unit_type removed:
reacted = react([unit for unit in polymer if unit.lower() != unit_type])
if shortest_type is None or len(reacted) < shortest:
shortest_type = unit_type
shortest = len(reacted)
print("Shorted resulting polymer is {} units long, with unit type {}/{} removed."
.format(shortest, shortest_type.upper(), shortest_type))
if __name__ == '__main__':
main()