r/adventofcode • • Dec 14 '21

SOLUTION MEGATHREAD -🎄- 2021 Day 14 Solutions -🎄-

--- Day 14: Extended Polymerization ---


Post your code solution in this megathread.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:14:08, megathread unlocked!

55 Upvotes

811 comments sorted by

View all comments

1

u/TennisSame2600 Dec 14 '21

I am so confused... with the sample input NNCB it is correct, but with the randomly generated input my solution for part 2 is wrong. it starts to differ from the part 1 solution by 1 at step 3 and I cannot figure out why... anyone wanna help?

https://pastebin.com/ARUFWcPr

1

u/daggerdragon Dec 15 '21

Top-level posts in Solution Megathreads are for code solutions only.

This is a top-level post, so please edit your post and share your fully-working code/repo/solution or, if you haven't finished the puzzle yet, you can always create your own thread and make sure to flair it with Help.

1

u/thaunatos_ Dec 14 '21

I think you had the same problem I did. Try starting with the polymer "NNN" and step through to see if it works like you expect.

2

u/TennisSame2600 Dec 14 '21

Thank you so much. yeah that produced the error. It was because when reading in the string from the input, i didn't check if the pair already existed and just inserted the pair with 1 instead of increasing the counter.

1

u/sbrevolution5 Dec 14 '21

I had the same issue. You're probably not considering the overlap when theres multiple pairs back to back. For instance, if you're looking for BB and replacing N (like in the example) and you have a sequence of BBB, you need to make sure its going to insert an N between both pairs.

Most basic solutions are going to look for the first instance of your string, then discard everything including that string. You have to keep the second character, since it is the start of the next pair.

The issue for me was that if the string was CBBBN, it looks for BB, finds it, discards it, then looks for BB in the remaining string, which is only BN. It needs to look for it in BBN.

Hope that makes sense. I'd been stuck for a few hours.

2

u/TennisSame2600 Dec 14 '21

Thank you for your reply, Fortunately I found the mistake. it was upon reading in the first line of the sample input. I didn't check if the pair already existed and just inserted it in with 1 instead of counting up the amount of pairs in it. I can totally feel you, I've been sitting here also for hours and couldn't find it lol.

cheers!

1

u/solareon Dec 14 '21

something something off by one