# Advent of Code F# – Day 5

Yan Cui

I help clients go faster for less using serverless technologies.

Table of Content

This article is brought to you by

The real-time data platform that empowers developers to build innovative products faster and more reliably than ever before.

Learn more

ps. look out for all my other solutions for Advent of Code challenges here.

## Day 5

See details of the challenge here.

Let’s start by adding a hash function that’ll take an input string and return the hexdecimal representation of its MD5 hash.

From there, we can create an infinite sequence of hash values generated by concatinating the input and an increasing index (as per the instruction). But, remember, we’re only interested in hash values that starts with 5 zeroes.

Once we have this sequence of interesting hash values, part 1 requires us to take the first 8 and make a password out of it.

### Part 2

As the door slides open, you are presented with a second door that uses a
slightly more inspired security mechanism. Clearly unimpressed by the last
version (in what movie is the password decrypted in order?!), the Easter Bunny
engineers have worked out a better solution.

Instead of simply filling in the password from left to right, the hash now also
indicates the position within the password to fill. You still look for hashes
that begin with five zeroes; however, now, the sixth character represents the
position (0-7), and the seventh character is the character to put in that position.

A hash result of 000001f means that f is the second character in the password.
Use only the first result for each position, and ignore invalid positions.

For example, if the Door ID is abc:

The first interesting hash is from abc3231929, which produces 0000015…; so, 5
goes in position 1: _5______.
In the previous method, 5017308 produced an interesting hash; however, it is
ignored, because it specifies an invalid position (8).
The second interesting hash is at index 5357525, which produces 000004e…; so,
e goes in position 4: _5__e___.
You almost choke on your popcorn as the final character falls into place,
producing the password 05ace8e3.

Given the actual Door ID and this new method, what is the password? Be extra
proud of your solution if it uses a cinematic “decrypting” animation.

We can reuse the inifinite sequence of interesting hash values we constructed earlier, but this time around we need to also filter out hash values whose 6th character is not 0-7.

Additionally, we need to track the first hash value for each position.

The first idea I had was to use a mutable Option<char>[] (length 8) to represent the password, and then iterate over the valid hash values until that array is filled with Some x. But this approach felt too imperative for my liking..

I couldn’t use a fold as it will fold over all the inputs.. however, I can use a scan which yields the intermediate results as a sequence – which I can then skip until I have 8 hash values.

I can also use an immutable Set to represent all the positions that are still unoccupied as I iterate through the hash values.

Whilst part 1 was not computationally light, it did complete in a matter of seconds on my laptop. Part 2 took more than a minute to finish so if anyone knows a good way to speed things up please leave a comment to this post.

## Links

Whenever you’re ready, here are 4 ways I can help you:

1. Production-Ready Serverless: Join 20+ AWS Heroes & Community Builders and 1000+ other students in levelling up your serverless game. This is your one-stop shop for quickly levelling up your serverless skills.
2. Do you want to know how to test serverless architectures with a fast dev & test loop? Check out my latest course, Testing Serverless Architectures and learn the smart way to test serverless.
3. I help clients launch product ideas, improve their development processes and upskill their teams. If you’d like to work together, then let’s get in touch.
4. Join my community on Discord, ask questions, and join the discussion on all things AWS and Serverless.

### 4 thoughts on “Advent of Code F# – Day 5”

1. thanks, glad you’re enjoying it :-)

I think if you go with the idiomatic way of writing F# you are very likely to end up with similar looking code for exercises like this, and that’s a good thing in my opinion, it means we should be able to understand each other’s code with relative ease.

2. I tried a few things, but everything was running slow on my machine. It took so long, I had to sleep on it.
In my simplest version, I tried a distinctBy with truncate. I think it only worked because of how specific they were being with the hex characters. Third byte should only be 0x00 to 0x07. That and if I didn’t do the truncate, it’d blow up on Seq overflowing int32. Not sure how it would run on your machine.

let firstNibble (b:byte) = (b &&& 0xF0uy) >>> 4
let md5 = MD5.Create()

let hash input i =
md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(input + i.ToString()))

let getPassword2 input =
Seq.initInfinite (hash input)
|> Seq.filter (fun hash -> 0uy = hash.[0]
&& 0uy = hash.[1]
&& hash.[2] Seq.distinctBy (fun hash-> hash.[2] )
|> Seq.truncate 8
|> Seq.sortBy (fun hash -> hash.[2] )
|> Seq.map (fun hash -> firstNibble hash.[3] |> sprintf “%x”)
|> String.concat “”

3. Good shout on checking for byte values rather than converting the whole string, and distinctBy (kicking myself for not think of that, so obvious rather than my silly Seq.scan!).

On my machine, your solution ran for about 2:35, which is a good deal faster than mine (which when I ran with #time on it actually took about 5 mins).

I guess one way to speed things up on these modern laptops is to parallellize – if you have 4 cores then you can do indices 0, 1, 2 and 3 in parallel, and when there are multiple matches for the same position then the associated index value can be used to decide on the order. I’ve not used it myself, but you might be able to use FSharp.Control.AsyncSeq for that.

Alternatively, maybe you can spawn a number of agents who each try multiples of 1, 2, 3, 4, … and so on and report back to the controller agent when it finds a an interesting hash value and let the controller agent decide whether or not to keep it (only the first one for an index is kept).