Created
December 5, 2018 09:23
-
-
Save synio-wesley/21626505b5708bff4219462199c78de2 to your computer and use it in GitHub Desktop.
AOC 2018 - Day 5 - Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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