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

## Day 21

See details of the challenge here.

Today’s input looks like this:

swap position 2 with position 7

swap letter f with letter a

swap letter c with letter a

rotate based on position of letter g

rotate based on position of letter f

rotate based on position of letter b

swap position 3 with position 6

swap letter e with letter c

swap letter f with letter h…

First, let’s model the different instructions we’ll need to process with union types.

Then, we can use the same * Regex* active pattern we used in Day 10 to help us parse the input file into the

*type.*

**Instruction**Now let’s add a few functions to do the actual scrambling:

Most of these are straight forward, but there is one thing I’d like to touch on as it might not be obvious – the first line of the * reverse* function :

*.*

**let n = n % input.Length**Suppose the length of the input array is 4 (eg. *[| ‘a’, ‘b’, ‘c’, ‘d’ |]*) then shifting left or right by 5 spaces is the same as shifting by 1.

To solve part 1 we just need to apply the instructions we parsed earlier on our input.

**let part1 = “abcdefgh”.ToCharArray() |> apply instructions**

### Part 2

You scrambled the password correctly, but you discover that you can’t actually

modify the password file on the system. You’ll need to un-scramble one of the

existing passwords by reversing the scrambling process.

What is the un-scrambled version of the scrambled password fbgdceah?

On first thought, it seemed we could reverse the scrambling process by finding the inverse of each of the steps and apply the input in reverse order. For example, the inverse of * Reverse*,

*and*

**SwapPos***instructions are themselves; the inverse of*

**SwapLetter***is*

**Rotate(Left, 3)***and vice versa; the inverse of*

**Rotate(Right, 3)***is*

**Move(1, 3)***.*

**Move(3, 1)**But… how do we find the inverse of * RotateBasedOn*? We will need to know the exact no. of spaces to shift left, there’s no easy way to do it based on the character and the right-shifted result alone. You could, however, go at it from a different angle and try out all possible inputs that would have given us the output that we have – by shifting

*Left*by 1 to

*input.Length*no. of spaces.

That said, given the length of the password we need to unscramble, there are only 40320 permutations of the letters *abcdefgh*. A brute force approach is feasible and possibly easier in this case, and that’s the approach I decided to go with. For that, I need a function to return all permutations of the letters *abcdefgh*.

I found a nice implementation of such function on fssnip courtesy of *Rick Minerich* and to solve part 2 we just need to find the permutation that gives us the scrambled password *“fbgdceah”*.

(ps. if you’re interested in how the first approach would look, I decided to implement it as well just for fun)

## Links

- Day 21 challenge description
- Advent of Code 2015
- Solution for Day 20
- All my F# solutions for Advent of Code
- Github repo

**Yan Cui**

I’m an **AWS Serverless Hero** and the author of **Production-Ready Serverless**. I have run production workload at scale in AWS for nearly 10 years and I have been an architect or principal engineer with a variety of industries ranging from banking, e-commerce, sports streaming to mobile gaming. I currently work as an independent consultant focused on AWS and serverless.

Further reading

Here is a complete list of all my posts on serverless and AWS Lambda. In the meantime, here are a few of my most popular blog posts.

- Lambda optimization tip – enable HTTP keep-alive
- You are thinking about serverless costs all wrong
- Many faced threats to Serverless security
- We can do better than percentile latencies
- I’m afraid you’re thinking about AWS Lambda cold starts all wrong
- Yubl’s road to Serverless
- AWS Lambda – should you have few monolithic functions or many single-purposed functions?
- AWS Lambda – compare coldstart time with different languages, memory and code sizes
- Guys, we’re doing pagination wrong