Open billglover opened 5 years ago
Solution: github.com/billglover/aoc/blob/master/2018/05/main.go
This one looks simple but has the potential for some horrendous execution times. I've seen reports of people getting run times of over 70 seconds for some solutions. I was determined not to try a recursive approach for this one. The rewards are:
$ time go run main.go
Result: xxxxx
real 0m0.369s
user 0m0.317s
sys 0m0.164s
$ time go run main.go -partB
Result: xxxxx
real 0m0.929s
user 0m0.380s
sys 0m0.192s
Yep, my initial (python) implementation took 50+ seconds, doing lots of work splicing and re-constructing lists. I switched to deques and now it's 100x faster.
Haven’t yet come across dqueues in Python, but that’s some performance improvement.
$ time ruby day5/day5.rb
"Part 1: xxx"
"Part 2: xxx"
ruby day5/day5.rb 0.29s user 0.04s system 96% cpu 0.338 total
https://github.com/stain88/advent-of-code-2018/blob/master/day5/day5.rb
Day 5: Alchemical Reduction
Part One
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
andR
are units with the same type but opposite polarity, whereasr
ands
are entirely different types and do not react.For example:
aA
,a
andA
react, leaving nothing behind.abBA
,bB
destroys itself, leavingaA
. As above, this then destroys itself, leaving nothing.abAB
, no two adjacent units are of the same type, and so nothing happens.aabAAB
, even thoughaa
andAA
are of the same type, their polarities match, and so nothing happens.Now, consider a larger example,
dabAcCaCBAcCcaDA
:dabAcCaCBAcCcaDA
The firstcC
is removed.dabAaCBAcCcaDA
This createsAa
, which is removed.dabCBAcCcaDA
EithercC
orCc
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.)
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:A
/a
units producesdbcCCBcCcD
. Fully reacting this polymer producesdbCBcD
, which has length 6.B
/b
units producesdaAcCaCAcCcaDA
. Fully reacting this polymer producesdaCAcaDA
, which has length 8.C
/c
units producesdabAaBAaDA
. Fully reacting this polymer producesdaDA
, which has length 4.D
/d
units producesabAcCaCBAcCcaA
. Fully reacting this polymer producesabCBAc
, 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?