Cleaned up 2018 day 5 and fixed quantum annihilation bug

This commit is contained in:
Luke Hubmayer-Werner 2018-12-05 17:26:54 +10:30
parent d324068bcf
commit 581fa49dd5
1 changed files with 16 additions and 54 deletions

View File

@ -1,59 +1,21 @@
with open('day5-input', 'r') as file: with open('day5-input', 'r') as file:
data = [l.strip('\n') for l in file] data = [l.strip('\n') for l in file]
# import numpy as np polymer = [c for c in data[0]]
polymer = [c for c in reversed(data[0])]
# polymer = [c for c in 'dabAcCaCBAcCcaDA'] # polymer = [c for c in 'dabAcCaCBAcCcaDA']
i = 0 def react(poly):
print(len(polymer))
while i < len(polymer)-1:
if polymer[i] == polymer[i+1].swapcase():
polymer.pop(i)
polymer.pop(i)
i -= 2
i += 1
print(len(polymer)) # Not 11196?!
while i < len(polymer) - 1:
if polymer[i] == polymer[i + 1].swapcase():
polymer.pop(i)
polymer.pop(i)
i -= 2
i += 1
print(len(polymer)) # Not 11196?!
while i < len(polymer) - 1:
if polymer[i] == polymer[i + 1].swapcase():
polymer.pop(i)
polymer.pop(i)
i = -1
i += 1
print(len(polymer)) # Part 1
remaining_letters = {}
for c in polymer:
c = c.lower()
if c in remaining_letters:
remaining_letters[c] += 1
else:
remaining_letters[c] = 1
print(max(remaining_letters.items(), key=lambda x: x[1]))
def react(letter):
polymer_2 = [c for c in data[0] if c.lower() != letter]
i = 0 i = 0
while i < len(polymer_2)-1: while i < len(poly)-1:
if polymer_2[i] == polymer_2[i+1].swapcase(): if poly[i] == poly[i+1].swapcase():
polymer_2.pop(i) poly.pop(i+1)
polymer_2.pop(i) poly.pop(i)
i -= 2 i = max(i-1, 0)
else:
i += 1 i += 1
while i < len(polymer_2) - 1: return poly
if polymer_2[i] == polymer_2[i + 1].swapcase():
polymer_2.pop(i) print(len(react(list(polymer)))) # Part 1
polymer_2.pop(i)
i = -1 remaining_letters = set(c.lower() for c in polymer)
i += 1 removals = [len(react([c for c in polymer if c.lower() != i])) for i in remaining_letters]
return len(polymer_2) print(min(removals)) # Part 2
# print(len(polymer_2)) # Not 10658?!
removals = [react(i) for i in remaining_letters.keys()]
print(max(removals)) # Part 2