Advent of Code F# — Day 25

ps. look out for all my oth­er solu­tions for Advent of Code chal­lenges here.


Day 25

See details of the chal­lenge here.

Today’s input looks like this:

cpy a d
cpy 7 c
cpy 362 b
inc d
dec b
jnz b -2

At last, we’re at the end of this year’s Advent of Code chal­lenges, and today’s chal­lenge is anoth­er twist of the assem­bun­ny code we’ve seen a few times this year, most recent­ly on Day 23.

Ad before, let’s define the instruc­tion set as a union type and make short work of the pars­ing the input file.

One notable dif­fer­ence to today’s chal­lenge though is that we’re no longer inter­est­ed in the val­ue of the reg­is­ters but the out­put from exe­cut­ing the pro­gram.

So instead of return­ing the reg­is­ters we’ll use a sequence com­pre­hen­sion to return the out­put val­ues as an infi­nite, and lazy sequence instead.

(ps. I ini­tial­ly wrote the exe­cute func­tion with a nest­ed recur­sive func­tion, but on sec­ond thought the prob­lem feels more con­cise­ly expressed as a while loop so I rewrote it as such)

To wrap up today’s chal­lenge (part 2 just requires you to click a link to wrap up your AOC jour­ney for anoth­er year) we need to find the first val­ue of n that will yield pairs of [ 0, 1 ] indef­i­nite­ly. Of course you can’t be sure that the pat­tern repeats indef­i­nite­ly with­out deep­er under­stand­ing of the pat­tern of change in the reg­is­ter val­ues (per­haps it’d be a bet­ter approach rather than the tri­al-and-error route I have gone with). I have gone with 1000 val­ues all fol­low­ing that pat­tern = repeat­ing indef­i­nite­ly, but turns out the answer is the same even when you use Seq.take 10.

So there it is folks, anoth­er year and anoth­er set of won­der­ful­ly enjoy­able chal­lenges. With you all a hap­py new year and see you soon!



Liked this post? Why not support me on Patreon and help me get rid of the ads!