# Advent of Code F# – Day 21

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 Instruction type.

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, SwapPos and SwapLetter instructions are themselves; the inverse of Rotate(Left, 3) is Rotate(Right, 3) and vice versa; the inverse of Move(1, 3) is 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)

# Advent of Code F# – Day 20

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

## Day 20

See details of the challenge here.

Today’s input looks like this:

1873924193-1879728099
2042754084-2076891856
4112318198-4113899685
1039794493-1057170966
3791841434-3797494664
1518668516-1518748952
1946127596-1953926346
4058215215-4086224696
3429681642-3455096313
2599576643-2604275147
1800210010-1801990849
1761160149-1766904471
2774395403-2774748831
1520470679-1542287000
2343327790-2346083217

First, let’s read the input into a list of tuples representing the lower-upper bound ranges.

Notice that I’ve also gone through the trouble of sorting the input by the lower bounds. This way, as we iterate through the ranges we can build up an overal range where:

• the lower bound is 0
• the upper bound is the highest upper bound value we have processed so far

and where there are gaps between the current upper bound and the lower bound in the next range, those are the allowed IPs values.

eg. for my input the first 3 ranges are:

(0, 1888888)

(1888889, 1904062)

(1900859, 2087697)

1. after processing (0, 1888888) the overall range is 0-1888888
2. (1888889, 1904062) forms a continuous range after 1888888, so the overall range is now 0-1904062
3. (1900859, 2087697) overlaps with the overall range on the lower bound, that’s ok, the overall range is now 0-2087697

no gaps were found in the 3 ranges so far.

Suppose the next range is (2087700, 2100000), then a gap would appear and we will have 2 allowed IPs: 2087698 and 2087699.

To put that into code, here’s a findAllowedIPs function that will process all the sorted list of ranges and yield all the allowed IPs as a lazy sequence. ps. for part 2 where we need to find the no. of allowed IPs this is a O(n) solution where n is the no. of blacklist ranges.

To solve part 1, we need the first allowed IP.

let part1 = findAllowedIPs input |> Seq.head

### Part 2

How many IPs are allowed by the blacklist?

let part2 = findAllowedIPs input |> Seq.length

# Advent of Code F# – Day 19

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

## Day 19

See details of the challenge here.

I initially approached today’s challenge as a dynamic programming exercise, but it quickly transpired that there’s a much better way to do it once I realised that part 1 is in fact the Josephus Problem and there’s a simple solution to it.

To understand the above, watch the YouTube video in the links section below.

### Part 2

Realizing the folly of their present-exchange rules, the Elves agree to instead
steal presents from the Elf directly across the circle. If two Elves are across
the circle, the one on the left (from the perspective of the stealer) is stolen
from. The other rules remain unchanged: Elves with no presents are removed from
the circle entirely, and the other elves move in slightly to keep the circle
evenly spaced.

For example, with five Elves (again numbered 1 to 5):

• The Elves sit in a circle; Elf 1 goes first:
``````  1
5   2
4 3``````
• Elves 3 and 4 are across the circle; Elf 3’s present is stolen, being the one to
the left. Elf 3 leaves the circle, and the rest of the Elves move in:
``````  1           1
5   2  -->  5   2
4 -          4``````
• Elf 2 steals from the Elf directly across the circle, Elf 5:
``````  1         1
-   2  -->     2
4         4 ``````
• Next is Elf 4 who, choosing between Elves 1 and 2, steals from Elf 1:
`````` -          2
2  -->
4          4``````
• Finally, Elf 2 steals from Elf 4:
`````` 2
-->  2
-``````

So, with five Elves, the Elf that sits starting in position 2 gets all the
presents.

With the number of Elves given in your puzzle input, which Elf now gets all the
presents?

I’m not aware of this variation to the Josephus Problem, but I’d wager that there would be some pattern to the results similar to part 1, so I put together a dynamic programming solution to get some outputs. (ps. the solution is not good enough for the input as it’ll take too long to return)

With the help of this I can see a pattern emerging:

1 : 1
2 : 2
3 : 3
4 : 1
5 : 2
6 : 3
7 : 5
8 : 7
9 : 9
10 : 1
11 : 2
12 : 3
13 : 4
14 : 5
15 : 6
16 : 7
17 : 8
18 : 9
19 : 11
20 : 13
21 : 15
22 : 17
23 : 19
24 : 21
25 : 23
26 : 25
27 : 27
28 : 1
29 : 2
30 : 3

• where n is a power of 3 then the answer is itself
• else n can be expressed as m + l where m is a power of 3
• where l <= m (eg. n = 5 = 3 + 2 where m = 3 and l = 2) then the answer is just l
• else the answer is m + (l – m) * 2 (eg. n = 7 = 3 + 4 where m = 3 and l = 4 and m + (l – m) * 2 = 5)

# Advent of Code F# – Day 18

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

## Day 18

See details of the challenge here.

To solve both part 1 and 2:

# Advent of Code F# – Day 17

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

## Day 17

See details of the challenge here.

First, let’s add a hash function (that returns the MD5 as a hexadecimal string as we have done so often this year).

Then, add a step function that’ll take a (x, y) position and the path (eg. “hijklDU”) leading up to it and returns the a tuple of new position and path if we’re not going to move off grid. We can then use this to construct the more useful functions to take a step in the up, down, left and right directions.

Now we can put everything together and write a function to find all the paths from (0, 0) to (3, 3) in the grid.

One minor detail you might have noticed on ln 5 above is that we’re short-circuiting paths that have reached the target, this is in accordance with the further details revealed in part 2 below.

For each of the positions (and the path that lead to it), we use the aforementioned up, down, left, and right functions to find the next positions we can reach from here (provided the right doors are open as per determined by the MD5 hash value).

And yes, it’s the Level Order Tree Traveral problem again! Funny we see different reincarnations of this particular problem over and over during this year’s AOC event.

One more detail to look out for in the code snippet above – that it’s possible for there to not be a path (as per the example in the description of the problem). Hence why we terminate the Seq.unfold when none of the current positions yields a possible next move.

With this, we can now answer part 1 really easily.

### Part 2

You’re curious how robust this security solution really is, and so you decide to
find longer and longer paths which still provide access to the vault. You
remember that paths always end the first time they reach the bottom-right room
(that is, they can never pass through it, only end in it).

For example:

If your passcode were ihgpwlah, the longest path would take 370 steps.
With kglvqrro, the longest path would be 492 steps long.
With ulqzkmiv, the longest path would be 830 steps long.
What is the length of the longest path that reaches the vault?