Advent of Code F# — Day 23

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


Day 23

See details of the chal­lenge here.

Today’s input looks like this:

cpy a b
dec b
cpy a d
cpy 0 a
cpy b c
inc a


Today’s chal­lenge is an exten­sion to Day 12, where we have intro­duced a new instruc­tion for tog­gle (tgl). To make tog­gling log­ic sim­pler let’s intro­duce a union type to rep­re­sent the dif­fer­ent instruc­tions we can receive and write a func­tion parse them.

Next, let’s add a tog­gle func­tion to tog­gle a giv­en instruc­tion and mod­i­fy the exe­cute func­tion from Day 12 to work with the union type and sup­port the new tgl instruc­tion as well.

Now we can solve part 1.

let part1 = (exe­cute [ “a”, 7 ] inputs).[“a”]


Part 2

The safe doesn’t open, but it does make sev­er­al angry nois­es to express its frus­tra­tion.

You’re quite sure your log­ic is work­ing cor­rect­ly, so the only oth­er thing is… you check the paint­ing again. As it turns out, col­ored eggs are still eggs. Now you count 12.

As you run the pro­gram with this new input, the pro­to­type com­put­er begins to over­heat. You won­der what’s tak­ing so long, and whether the lack of any instruc­tion more pow­er­ful than “add one” has any­thing to do with it. Don’t bun­nies usu­al­ly mul­ti­ply?

Any­way, what val­ue should actu­al­ly be sent to the safe?

As the descrip­tion eludes to, if we use 12 as ini­tial input it might take a while to run.. and the naive approach of just exe­cut­ing the log­ic as before, ie

let part2 = (exe­cute [ “a”, 12 ] inputs).[“a”]

took a good 4 min­utes to run on my 2015 MBP.

If you add a few printfn state­ments and you’ll a few blocks of instruc­tions that repeat for many cycles, eg.

cpy b c
inc a
dec c
jnz c -2
dec d
jnz d -5

you can add a short-cir­cuit when you see this hap­pen and replace it with the equiv­a­lent of a = b * d (as the descrip­tion elud­ed to).



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