Advent of Code F# – Day 11

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 encap­su­late the three pass­word require­ments:

1) Pass­words must include one increas­ing straight of at least three let­ters, like abc, bcd, cde, and so on, up to xyz. They can­not skip let­ters; abd doesn’t count.


2) Pass­words may not con­tain the let­ters i, o, or l, as these let­ters can be mis­tak­en for oth­er char­ac­ters and are there­fore con­fus­ing.


3) Pass­words must con­tain at least two dif­fer­ent, non-over­lap­ping pairs of let­ters, like aa, bb, or zz.


Any valid pass­word would need to sat­is­fy all three require­ments:


Now that’s tak­en care of, let’s focus on the mat­ter of increas­ing the pass­word:


here we have bro­ken up the log­ic into two small­er chunks:

  • incr­Char is respon­si­ble for return the incre­ment­ed char­ac­ters, and loop­ing from ‘z’ to ‘a’
  • incrAt incre­ments the char at the spec­i­fied index, and recurs­es to incre­ment the next index if the afore­men­tioned char loops around

Final­ly, let’s gen­er­ate the sequence of all future pass­words with an unfold, and find the first one that is a valid pass­word:



Part 2

Almost iden­ti­cal to Part 1, the only change is to skip the first valid pass­word after the ini­tial input: