codebuddies / DailyAlgorithms

Do a problem. Create (or find) your problem in the issues. Paste a link to your solution. See others' solutions of the same problem.
12 stars 1 forks source link

[Advent of Code] Day 5: Alchemical Reduction #6

Open billglover opened 5 years ago

billglover commented 5 years ago

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 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:

Now, consider a larger example, dabAcCaCBAcCcaDA:

  1. dabAcCaCBAcCcaDA The first cC is removed.
  2. dabAaCBAcCcaDA This creates Aa, which is removed.
  3. dabCBAcCcaDA Either cC or Cc are removed (the result is the same).
  4. 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:

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?

billglover commented 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:

Part One

$ time go run main.go
Result: xxxxx

real    0m0.369s
user    0m0.317s
sys     0m0.164s

Part Two

$ time go run main.go -partB
Result: xxxxx

real    0m0.929s
user    0m0.380s
sys     0m0.192s
tcarobruce commented 5 years ago

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.

https://github.com/tcarobruce/aoc2018/blob/master/p05.py

billglover commented 5 years ago

Haven’t yet come across dqueues in Python, but that’s some performance improvement.

stain88 commented 5 years ago
$ 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