-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoc5.1.py
executable file
·44 lines (32 loc) · 991 Bytes
/
aoc5.1.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
#!/usr/bin/env python3
import sys
import collections
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]
with open(fname) as f:
polymer = list(f.read().rstrip())
reacted = react(polymer)
# print(''.join(reacted))
print("Polymer units remaining: {}".format(len(reacted)))
if __name__ == '__main__':
main()