Advent of Code F# — Day 8

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


Day 8

See details of the chal­lenge here.

The input for today’s chal­lenge looks like this:

rect 1x1
rotate row y=0 by 10
rect 1x1
rotate row y=0 by 10
rect 1x1
rotate row y=0 by 5
rotate col­umn x=43 by 1
rotate col­umn x=40 by 2
rotate col­umn x=38 by 1

So, our first task is to parse this input into some­thing more work­able (some­thing like the Cmd union type we’ve declared below).

Notice that I used active pat­terns instead of plain func­tions:

  • StartsWith returns the sub­string after the spec­i­fied pre­fix
  • Sep­By returns the 2 num­bers sep­a­rat­ed by either “x” or “by” (eg. “1x1”, “0 by 5”)

I find this style of pars­ing code eas­i­er to under­stand as it struc­tural­ly match­es the input line and men­tal­ly I don’t have to fol­low any branch­ing log­ic.


Next, let’s mod­el the screen and the dif­fer­ent oper­a­tions against it — rect, rotate row and rotate col­umn.

Here I’ve gone with an imper­a­tive approach for effi­cien­cy sake, dupli­cat­ing the 50x6 2D array repeat­ed­ly (at least once per com­mand) just doesn’t sit well with me.

Any­how, once we apply the com­mands parsed from the input file we need to count the no. of true ele­ments in our 2D array to answer part 1.


Part 2

You notice that the screen is only capa­ble of dis­play­ing cap­i­tal let­ters; in the
font it uses, each let­ter is 5 pix­els wide and 6 tall.

After you swipe your card, what code is the screen try­ing to dis­play?

To solve part 2, the eas­i­est way I could think of was to print it out and see what let­ters are spelled out.

which in my case spelled out:




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