Description for today’s challenge is here.
The input for Day 19 looks like the following:
Al => ThF
Al => ThRnFAr
B => BCa
B => TiB
and it contains a series of replacement rules, as well as the molecule for a medicine.
So we have two kinds of inputs in the same file:
and for each line in the file we will get one or the other:
Since we know there’ll only be one molecule in the input file, we can again cheat a little and hard-wire a single element array pattern against the input:
Finally, to answer the challenge, we’ll use each replacement rule to generate a set of new molecules (one for each replaceable string in the current molecule) and count the unique strings that can be generated:
After getting stuck on this for the longest time and not able to come up with a solution that solves a problem within a reasonable amount of time (brute force approach just doesn’t work here), I eventually caved in and consulted the AoC subreddit to see how others have solved this problem, spoiler alert and the solution from askalski is soooo elegant.
(whitespace intended in case you don’t want to see the F# implementation of askalski‘s solution)
OK, from here on, I assume you’ve read the subreddit thread with the description of the solution.
So basically, the strings “Rn”, “Y”, “Ar” translates to “(“, “,” and “)”, and everything else are tokens (i.e. the left-hand sides of the replacement rules, e.g. “Ca => SiTh” means “Ca” is a token).
What’s more, we need to process the molecule from right-to-left, so we’ll reverse both the tokens as well as the molecule string itself:
Next, we’ll recursively count the:
- total number of tokens (this includes “Rn”, “Y” and “Ar”)
- number of parentheses (i.e. “Rn” and “Ar”)
- number of commas (i.e. “Y”)
and then finally apply askalski‘s formula to arrive at the answer:
Enjoy what you’re reading? Subscribe to my newsletter and get more content on AWS and serverless technologies delivered straight to your inbox.
I’m an AWS Serverless Hero and the author of Production-Ready Serverless. I have run production workload at scale in AWS for nearly 10 years and I have been an architect or principal engineer with a variety of industries ranging from banking, e-commerce, sports streaming to mobile gaming. I currently work as an independent consultant focused on AWS and serverless.
In this course, we’ll cover everything you need to know to use AWS Step Functions service effectively. Including basic concepts, HTTP and event triggers, activities, design patterns and best practices.
Here is a complete list of all my posts on serverless and AWS Lambda. In the meantime, here are a few of my most popular blog posts.
- Lambda optimization tip – enable HTTP keep-alive
- You are thinking about serverless costs all wrong
- Many faced threats to Serverless security
- We can do better than percentile latencies
- I’m afraid you’re thinking about AWS Lambda cold starts all wrong
- Yubl’s road to Serverless
- AWS Lambda – should you have few monolithic functions or many single-purposed functions?
- AWS Lambda – compare coldstart time with different languages, memory and code sizes
- Guys, we’re doing pagination wrong