day 5
This commit is contained in:
parent
0c4ae2b4b8
commit
8dc1a047a7
|
@ -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()
|
|
@ -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()
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue