part1 (1867B)
1--- Day 5: Alchemical Reduction --- 2 3You've managed to sneak in to the prototype suit manufacturing lab. The Elves are making decent 4progress, but are still struggling with the suit's size reduction capabilities. 5 6While the very latest in 1518 alchemical technology might have solved their problem eventually, you 7can do better. You scan the chemical composition of the suit's material and discover that it is 8formed by extremely long polymers (one of which is available as your puzzle input). 9 10The polymer is formed by smaller [1m[97munits[0m which, when triggered, react with each other such that two 11adjacent units of the same type and opposite polarity are destroyed. Units' types are represented by 12letters; units' polarity is represented by capitalization. For instance, r and R are units with the 13same type but opposite polarity, whereas r and s are entirely different types and do not react. 14 15For example: 16 17 18 - In aA, a and A react, leaving nothing behind. 19 20 - In abBA, bB destroys itself, leaving aA. As above, this then destroys itself, leaving nothing. 21 22 - In abAB, no two adjacent units are of the same type, and so nothing happens. 23 24 - In aabAAB, even though aa and AA are of the same type, their polarities match, and so nothing 25happens. 26 27 28Now, consider a larger example, dabAcCaCBAcCcaDA: 29 30dabA[1m[97mcC[0maCBAcCcaDA The first 'cC' is removed. 31dab[1m[97mAa[0mCBAcCcaDA This creates 'Aa', which is removed. 32dabCBA[1m[97mcCc[0maDA Either 'cC' or 'Cc' are removed (the result is the same). 33dabCBAcaDA No further actions can be taken. 34 35After all possible reactions, the resulting polymer contains [1m[97m10 units[0m. 36 37[1m[97mHow many units remain after fully reacting the polymer you scanned?[0m (Note: in this puzzle and 38others, the input is large; if you copy/paste your input, make sure you get the whole thing.) 39 40