2018 day 5 part 2
This commit is contained in:
parent
5b48791b96
commit
56fb20e704
|
@ -1,19 +1,35 @@
|
|||
#!/usr/bin/env python3
|
||||
from multiprocessing import Pool
|
||||
from string import ascii_lowercase, ascii_uppercase
|
||||
import fileinput
|
||||
import re
|
||||
|
||||
REACT_SET = set(map(''.join, zip(ascii_lowercase, ascii_uppercase)))
|
||||
REACT_SET = REACT_SET.union({r[::-1] for r in REACT_SET})
|
||||
REACT_REGEX = re.compile(r'({})'.format('|'.join(REACT_SET)))
|
||||
|
||||
def main():
|
||||
removals = set(map(''.join, zip(ascii_lowercase, ascii_uppercase)))
|
||||
removals = removals.union({r[::-1] for r in removals})
|
||||
remove_regex = re.compile('({})'.format('|'.join(removals)))
|
||||
line = next(fileinput.input()).strip()
|
||||
|
||||
def react(polymer):
|
||||
count = 1
|
||||
while count > 0:
|
||||
line, count = remove_regex.subn('', line)
|
||||
return len(line)
|
||||
polymer, count = REACT_REGEX.subn('', polymer)
|
||||
return len(polymer)
|
||||
|
||||
|
||||
def react_without(polymer, letter):
|
||||
print('Reacting without letter {}'.format(letter))
|
||||
return react(polymer.replace(letter, '').replace(letter.upper(), ''))
|
||||
|
||||
|
||||
def main():
|
||||
line = next(fileinput.input()).strip()
|
||||
|
||||
print('Reacting full line')
|
||||
print(react(line))
|
||||
|
||||
with Pool(4) as p:
|
||||
print(min(p.starmap(react_without, ((line, letter) for letter in ascii_lowercase))))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print(main())
|
||||
main()
|
||||
|
|
Loading…
Reference in New Issue