Skip to content

Instantly share code, notes, and snippets.

@synio-wesley
Created December 5, 2018 09:23
Show Gist options
  • Save synio-wesley/21626505b5708bff4219462199c78de2 to your computer and use it in GitHub Desktop.
Save synio-wesley/21626505b5708bff4219462199c78de2 to your computer and use it in GitHub Desktop.
AOC 2018 - Day 5 - Python
# Input
polymer = [unit for unit in ''.join([line.strip() for line in open('day5.txt')])]
# Part 1 (+ function also used for part 2)
def react(polymer):
i = 0
while i + 1 < len(polymer):
unit1 = polymer[i]
unit2 = polymer[i + 1]
if unit1 != unit2 and unit1.lower() == unit2.lower():
del polymer[i]
del polymer[i]
i -= 1
else:
i += 1
return len(polymer)
print(react(polymer))
# Part 2
units = set([unit.lower() for unit in polymer])
minLength = len(polymer)
for unit in units:
newPolymer = list(filter(lambda u: u.lower() != unit, polymer))
minLength = min(minLength, react(newPolymer))
print(minLength)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment