This commit is contained in:
dave 2018-12-04 22:23:10 -08:00
parent 0c4ae2b4b8
commit 8dc1a047a7
3 changed files with 82 additions and 0 deletions

36
5/a.py Executable file
View File

@ -0,0 +1,36 @@
#!/usr/bin/env python3
def is_react(A, B):
"""
Return true if A and B's case differs but letter matches
"""
return A.lower() == B.lower() and B != A
def react_poly(poly):
lasti = 0
while True:
reacted = False
for i in range(lasti, len(poly) - 1):
if is_react(poly[i], poly[i + 1]):
poly = poly[0:i] + poly[i + 2:]
reacted = True
lasti = i - 1 if i > 0 else i
break
if not reacted:
break
return poly
def main():
with open("input.txt") as f:
poly = f.read().strip()
poly = react_poly(poly)
print(len(poly))
if __name__ == '__main__':
main()

45
5/b.py Executable file
View File

@ -0,0 +1,45 @@
#!/usr/bin/env python3
from a import is_react
def react_poly2(poly, ignore=[]):
lasti = 0
while True:
reacted = False
for i in range(lasti, len(poly) - 1):
if poly[i] in ignore:
reacted = True
poly = poly[0:i] + poly[i + 1:]
lasti = i - 1 if i > 0 else i
break
if is_react(poly[i], poly[i + 1]):
poly = poly[0:i] + poly[i + 2:]
reacted = True
lasti = i - 1 if i > 0 else i
break
if not reacted:
break
return poly
def main():
with open("input.txt") as f:
poly = f.read().strip()
unique_chars = set(poly.lower())
bestlen = 99999999
bestchar = None
for char in unique_chars:
chainlen = len(react_poly2(poly, [char, char.upper()]))
if chainlen < bestlen:
bestlen = chainlen
bestchar = char
print(bestlen, "by removing", bestchar)
if __name__ == '__main__':
main()

1
5/input.txt Normal file

File diff suppressed because one or more lines are too long