Advent of Code F# – Day 20

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.


Once you under­stood what this chal­lenge is real­ly ask­ing the solu­tion becomes real­ly sim­ple:


If you read the ques­tion care­ful­ly, what it’s real­ly say­ing is:

For every house num­ber N, only elves whose num­ber M that is a divi­sor of N will vis­it this house and deliv­er M * 10 gifts. Find the small­est N where Sum(M * 10) >= 29000000.

So our first task is to find all divi­sors for any giv­en num­ber N, and since each divi­sor has a oppo­site (as in, for a divi­sor M, N/M is also a divi­sor) so you only need to iter­ate up to Sqrt(N).

And since my input is such a high num­ber, we can safe­ly start from a rel­a­tive­ly high num­ber for N too (100K in this case) and use Seq.unfold to gen­er­ate an infi­nite sequence of num­bers.


Part 2

Part 2 adds a small twist to pro­ceed­ings — only divi­sors M that are <= N/50 will be con­sid­ered, and the mul­ti­pli­er is now 11 instead of 10.


Notice that since we’re not look­ing for ‘all’ divi­sors any­more, so I also took the lib­er­ty to rename the func­tion to find­Di­vi­sors so it more accu­rate­ly describes what it does now.

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