Advent of Code F# – Day 23

The source code for this post (both Part 1 and Part 2) is avail­able here and you can click here to see my solu­tions for the oth­er Advent of Code chal­lenges.

Descrip­tion for today’s chal­lenge is here.

 

First, let’s define the 6 instruc­tions on the com­put­er:

day23_01

and parse the input file (see exam­ple below) into the DU type above

jio a, +16

inc a

tpl a

inc a

jmp +23

day23_02

Then we add a func­tion to iter­ate through the instruc­tions, a sim­ple (tail) recur­sive loop func­tion would do the trick. The below should be pret­ty self-explana­to­ry, two tricky things to keep in mind:

  • that jio stood for “jump if one” rather than the more intu­itive “jump if odd” giv­en that jie is “jump if even”…
  • the prob­lem spec­i­fies the reg­is­ter val­ue to be non-neg­a­tive, and just in case we’ll over­flow, use an unsigned long

day23_03

and all that’s left is to call the func­tion with the instruc­tions we have parsed from the input and ask for the val­ue of the b reg­is­ter at the end.

day23_04

 

Part 2

For Part 2, we just need to change the ini­tial reg­is­ter val­ues we pass into the loop:

day23_05