ps. look out for all my other solutions for Advent of Code challenges here.
See details of the challenge here.
Today’s input looks like this:
cpy a d
cpy 7 c
cpy 362 b
jnz b -2
At last, we’re at the end of this year’s Advent of Code challenges, and today’s challenge is another twist of the assembunny code we’ve seen a few times this year, most recently on Day 23.
Ad before, let’s define the instruction set as a union type and make short work of the parsing the input file.
One notable difference to today’s challenge though is that we’re no longer interested in the value of the registers but the output from executing the program.
So instead of returning the registers we’ll use a sequence comprehension to return the output values as an infinite, and lazy sequence instead.
(ps. I initially wrote the execute function with a nested recursive function, but on second thought the problem feels more concisely expressed as a while loop so I rewrote it as such)
To wrap up today’s challenge (part 2 just requires you to click a link to wrap up your AOC journey for another year) we need to find the first value of n that will yield pairs of [ 0, 1 ] indefinitely. Of course you can’t be sure that the pattern repeats indefinitely without deeper understanding of the pattern of change in the register values (perhaps it’d be a better approach rather than the trial-and-error route I have gone with). I have gone with 1000 values all following that pattern = repeating indefinitely, but turns out the answer is the same even when you use Seq.take 10.
So there it is folks, another year and another set of wonderfully enjoyable challenges. With you all a happy new year and see you soon!
- Day 25 challenge description
- Advent of Code 2015
- Solution for Day 24
- Solution for Day 23
- All my F# solutions for Advent of Code
- Github repo
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.
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