another coding evening
This commit is contained in:
45
05/solve02.py
Executable file
45
05/solve02.py
Executable file
@ -0,0 +1,45 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
def read_input():
|
||||
with open('input.txt', 'r') as f:
|
||||
data = f.read().splitlines()
|
||||
return data[0]
|
||||
|
||||
def find_longest_match(polymer):
|
||||
result = 1
|
||||
for x in range(len(polymer)-1):
|
||||
if abs(ord(polymer[x]) - ord(polymer[x+1])) != 32:
|
||||
break
|
||||
result += 1
|
||||
return result
|
||||
|
||||
|
||||
def retract_polymer(polymer):
|
||||
parsing_pos = 0
|
||||
|
||||
while parsing_pos < len(polymer):
|
||||
lm = find_longest_match(polymer[parsing_pos:])
|
||||
if lm < 2:
|
||||
parsing_pos += 1
|
||||
continue
|
||||
|
||||
lm = 2
|
||||
if parsing_pos == 0:
|
||||
polymer = polymer[lm:]
|
||||
else:
|
||||
polymer = polymer[:parsing_pos] + polymer[lm+parsing_pos:]
|
||||
parsing_pos -= 1
|
||||
|
||||
return len(polymer)
|
||||
|
||||
l = []
|
||||
for c in range(ord('a'),ord('z')+1):
|
||||
p = read_input()
|
||||
if chr(c) not in p:
|
||||
continue
|
||||
p = p.replace(chr(c), '')
|
||||
p = p.replace(chr(c-32), '')
|
||||
print("removing characher: {}".format(chr(c)))
|
||||
l.append(retract_polymer(p))
|
||||
|
||||
print(min(l))
|
||||
Reference in New Issue
Block a user