Day 5
parent
8d721d086f
commit
82f106b186
@ -0,0 +1,38 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
with open('input.txt', 'r') as f:
|
||||||
|
polymer = f.read()
|
||||||
|
|
||||||
|
print('Polymer length:', len(polymer))
|
||||||
|
|
||||||
|
modifications = True
|
||||||
|
rounds = 0
|
||||||
|
while modifications:
|
||||||
|
modifications = False
|
||||||
|
rounds += 1
|
||||||
|
mods = 0
|
||||||
|
|
||||||
|
new_polymer = []
|
||||||
|
skip = False
|
||||||
|
for i, el in enumerate(polymer[:-1]):
|
||||||
|
if skip:
|
||||||
|
skip = False
|
||||||
|
continue
|
||||||
|
next_el = polymer[i + 1]
|
||||||
|
if el != next_el and el.upper() == next_el.upper():
|
||||||
|
modifications = True
|
||||||
|
mods += 1
|
||||||
|
skip = True
|
||||||
|
continue
|
||||||
|
|
||||||
|
new_polymer.append(el[-1])
|
||||||
|
|
||||||
|
if not skip:
|
||||||
|
new_polymer.append(polymer[-1])
|
||||||
|
|
||||||
|
polymer = ''.join(new_polymer)
|
||||||
|
|
||||||
|
print(f'Round: {rounds}. Modifications found: {mods}. Polymer length: {len(polymer)}')
|
||||||
|
|
||||||
|
print(polymer)
|
||||||
|
print(len(polymer))
|
@ -0,0 +1,81 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
from pprint import pprint
|
||||||
|
|
||||||
|
with open('input.txt', 'r') as f:
|
||||||
|
polymer = f.read()
|
||||||
|
|
||||||
|
print('Polymer length:', len(polymer))
|
||||||
|
|
||||||
|
def reacts(el_1, el_2):
|
||||||
|
return el_1 != el_2 and el_1.upper() == el_2.upper()
|
||||||
|
|
||||||
|
def reduce_polymer_naive(polymer):
|
||||||
|
"""Faster but not correct solution. Can be used for first reduction of data
|
||||||
|
"""
|
||||||
|
poly = list(polymer)
|
||||||
|
|
||||||
|
i = 0
|
||||||
|
gap = 1
|
||||||
|
while i < len(poly):
|
||||||
|
if i < 0 or i + gap >= len(poly):
|
||||||
|
i += gap
|
||||||
|
gap = 1
|
||||||
|
continue
|
||||||
|
|
||||||
|
el = poly[i]
|
||||||
|
next_el = poly[i + gap]
|
||||||
|
if reacts(el, next_el):
|
||||||
|
poly[i] = '-'
|
||||||
|
poly[i + gap] = '-'
|
||||||
|
i -= 1
|
||||||
|
gap += 2
|
||||||
|
continue
|
||||||
|
|
||||||
|
i += gap
|
||||||
|
gap = 1
|
||||||
|
return ''.join([el for el in poly if el != '-'])
|
||||||
|
|
||||||
|
def reduce_polymer(polymer):
|
||||||
|
polymer = reduce_polymer_naive(polymer)
|
||||||
|
modifications = True
|
||||||
|
rounds = 0
|
||||||
|
while modifications:
|
||||||
|
modifications = False
|
||||||
|
rounds += 1
|
||||||
|
mods = 0
|
||||||
|
|
||||||
|
new_polymer = []
|
||||||
|
skip = False
|
||||||
|
for i, el in enumerate(polymer[:-1]):
|
||||||
|
if skip:
|
||||||
|
skip = False
|
||||||
|
continue
|
||||||
|
next_el = polymer[i + 1]
|
||||||
|
if el != next_el and el.upper() == next_el.upper():
|
||||||
|
modifications = True
|
||||||
|
mods += 1
|
||||||
|
skip = True
|
||||||
|
continue
|
||||||
|
|
||||||
|
new_polymer.append(el[-1])
|
||||||
|
|
||||||
|
if not skip:
|
||||||
|
new_polymer.append(polymer[-1])
|
||||||
|
|
||||||
|
polymer = ''.join(new_polymer)
|
||||||
|
return polymer
|
||||||
|
|
||||||
|
|
||||||
|
alphabet = 'abcdefghijklmnopqrstuvwxyz'
|
||||||
|
|
||||||
|
count_dict = dict()
|
||||||
|
|
||||||
|
for el in alphabet:
|
||||||
|
new_poly = ''.join([x for x in polymer if x not in (el, el.upper())])
|
||||||
|
count_dict[el] = len(reduce_polymer(new_poly))
|
||||||
|
print(f'Processed {el}, len: {count_dict[el]}')
|
||||||
|
|
||||||
|
pprint(count_dict)
|
||||||
|
min_idx = min(count_dict, key=count_dict.get)
|
||||||
|
print(min_idx, count_dict[min_idx])
|
@ -0,0 +1,49 @@
|
|||||||
|
--- Day 5: Alchemical Reduction ---
|
||||||
|
You've managed to sneak in to the prototype suit manufacturing lab. The Elves are making decent progress, but are still struggling with the suit's size reduction capabilities.
|
||||||
|
|
||||||
|
While the very latest in 1518 alchemical technology might have solved their problem eventually, you can do better. You scan the chemical composition of the suit's material and discover that it is formed by extremely long polymers (one of which is available as your puzzle input).
|
||||||
|
|
||||||
|
The polymer is formed by smaller units which, when triggered, react with each other such that two adjacent units of the same type and opposite polarity are destroyed. Units' types are represented by letters; units' polarity is represented by capitalization. For instance, r and R are units with the same type but opposite polarity, whereas r and s are entirely different types and do not react.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
In aA, a and A react, leaving nothing behind.
|
||||||
|
In abBA, bB destroys itself, leaving aA. As above, this then destroys itself, leaving nothing.
|
||||||
|
In abAB, no two adjacent units are of the same type, and so nothing happens.
|
||||||
|
In aabAAB, even though aa and AA are of the same type, their polarities match, and so nothing happens.
|
||||||
|
Now, consider a larger example, dabAcCaCBAcCcaDA:
|
||||||
|
|
||||||
|
dabAcCaCBAcCcaDA The first 'cC' is removed.
|
||||||
|
dabAaCBAcCcaDA This creates 'Aa', which is removed.
|
||||||
|
dabCBAcCcaDA Either 'cC' or 'Cc' are removed (the result is the same).
|
||||||
|
dabCBAcaDA No further actions can be taken.
|
||||||
|
After all possible reactions, the resulting polymer contains 10 units.
|
||||||
|
|
||||||
|
How many units remain after fully reacting the polymer you scanned? (Note: in this puzzle and others, the input is large; if you copy/paste your input, make sure you get the whole thing.)
|
||||||
|
|
||||||
|
Your puzzle answer was 9462.
|
||||||
|
|
||||||
|
--- Part Two ---
|
||||||
|
Time to improve the polymer.
|
||||||
|
|
||||||
|
One of the unit types is causing problems; it's preventing the polymer from collapsing as much as it should. Your goal is to figure out which unit type is causing the most problems, remove all instances of it (regardless of polarity), fully react the remaining polymer, and measure its length.
|
||||||
|
|
||||||
|
For example, again using the polymer dabAcCaCBAcCcaDA from above:
|
||||||
|
|
||||||
|
Removing all A/a units produces dbcCCBcCcD. Fully reacting this polymer produces dbCBcD, which has length 6.
|
||||||
|
Removing all B/b units produces daAcCaCAcCcaDA. Fully reacting this polymer produces daCAcaDA, which has length 8.
|
||||||
|
Removing all C/c units produces dabAaBAaDA. Fully reacting this polymer produces daDA, which has length 4.
|
||||||
|
Removing all D/d units produces abAcCaCBAcCcaA. Fully reacting this polymer produces abCBAc, which has length 6.
|
||||||
|
In this example, removing all C/c units was best, producing the answer 4.
|
||||||
|
|
||||||
|
What is the length of the shortest polymer you can produce by removing all units of exactly one type and fully reacting the result?
|
||||||
|
|
||||||
|
Your puzzle answer was 4952.
|
||||||
|
|
||||||
|
Both parts of this puzzle are complete! They provide two gold stars: **
|
||||||
|
|
||||||
|
At this point, you should return to your advent calendar and try another puzzle.
|
||||||
|
|
||||||
|
If you still want to see it, you can get your puzzle input.
|
||||||
|
|
||||||
|
You can also [Share] this puzzle.
|
File diff suppressed because one or more lines are too long
@ -0,0 +1,2 @@
|
|||||||
|
01: 9462
|
||||||
|
02: 4952
|
Loading…
Reference in New Issue