
Yan Cui
I help clients go faster for less using serverless technologies.
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.
type Direction = Left | Right | |
type Instruction = | |
| SwapPos of int * int | |
| SwapLetter of char * char | |
| Rotate of Direction * int | |
| RotateBasedOn of char | |
| Reverse of int * int | |
| Move of int * int |
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.
open System.IO | |
open System.Text.RegularExpressions | |
let input = File.ReadAllLines(__SOURCE_DIRECTORY__ + "/Day21Input.txt") | |
let (|Regex|_|) pattern input = | |
let m = Regex.Match(input, pattern) | |
if m.Success then Some(List.tail [ for g in m.Groups -> g.Value ]) | |
else None | |
let instructions = | |
input | |
|> Array.map (function | |
| Regex "swap position (\d*) with position (\d*)" [ src; dest ] -> | |
SwapPos (int src, int dest) | |
| Regex "swap letter (\w) with letter (\w)" [ x; y ] -> | |
SwapLetter (x.[0], y.[0]) | |
| Regex "rotate right (\d*) step" [ n ] -> | |
Rotate (Right, int n) | |
| Regex "rotate left (\d*) step" [ n ] -> | |
Rotate (Left, int n) | |
| Regex "rotate based on position of letter (\w)" [ x ] -> | |
RotateBasedOn x.[0] | |
| Regex "reverse positions (\d*) through (\d*)" [ x; y ] -> | |
Reverse (int x, int y) | |
| Regex "move position (\d*) to position (\d*)" [ src; dest ] -> | |
Move (int src, int dest) | |
) |
Now let’s add a few functions to do the actual scrambling:
let swapPos src dest (input : char[]) = | |
Array.init input.Length (fun idx -> | |
if idx = src then input.[dest] | |
elif idx = dest then input.[src] | |
else input.[idx]) | |
let swapLetter lf rt (input : char[]) = | |
input |> Array.map (fun x -> | |
if x = lf then rt | |
elif x = rt then lf | |
else x) | |
let rotate dir n (input : char[]) = | |
let n = n % input.Length | |
let f = | |
if dir = Right | |
then fun idx -> (idx + input.Length - n) % input.Length | |
else fun idx -> (idx + n) % input.Length | |
Array.init input.Length (fun idx -> input.[f idx]) | |
let rotateBasedOn char (input : char[]) = | |
let idx = input |> Array.findIndex ((=) char) | |
let n = 1 + idx + (if idx >= 4 then 1 else 0) | |
rotate Right n input | |
let reverse startIdx endIdx (input : char[]) = | |
Array.init input.Length (fun idx -> | |
if idx < startIdx || idx > endIdx | |
then input.[idx] | |
else input.[endIdx - (idx - startIdx)]) | |
let move src dest (input : char[]) = | |
let output = ResizeArray input | |
let char = output.[src] | |
output.RemoveAt(src) | |
output.Insert(dest, char) | |
output.ToArray() | |
let apply instructions input = | |
instructions | |
|> Array.fold (fun input inst -> | |
match inst with | |
| SwapPos (src, dest) -> swapPos src dest input | |
| SwapLetter (x, y) -> swapLetter x y input | |
| Rotate (dir, n) -> rotate dir n input | |
| RotateBasedOn (x) -> rotateBasedOn x input | |
| Reverse (x, y) -> reverse x y input | |
| Move (src, dest) -> move src dest input) input | |
|> fun chars -> new String(chars) |
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”.
let rec insertions x = function | |
| [] -> [[x]] | |
| (y :: ys) as l -> (x::l)::(List.map (fun x -> y::x) (insertions x ys)) | |
let rec permutations = function | |
| [] -> seq [ [] ] | |
| x :: xs -> Seq.concat (Seq.map (insertions x) (permutations xs)) | |
let part2 = | |
permutations [ 'a'..'h' ] | |
|> Seq.map List.toArray | |
|> Seq.pick (fun pwd -> | |
match pwd |> apply instructions with | |
| "fbgdceah" -> Some <| new String(pwd) | |
| _ -> None) |
(ps. if you’re interested in how the first approach would look, I decided to implement it as well just for fun)
let inverseRotateBasedOn char (input : char[]) = | |
{ 1..input.Length } | |
|> Seq.map (fun n -> rotate Left n input) | |
|> Seq.filter (fun original -> rotateBasedOn char original = input) | |
|> Seq.head | |
let applyInverse instructions input = | |
instructions | |
|> Array.rev | |
|> Array.fold (fun input inst -> | |
match inst with | |
| SwapPos (src, dest) -> swapPos src dest input | |
| SwapLetter (x, y) -> swapLetter x y input | |
| Rotate (Left, n) -> rotate Right n input | |
| Rotate (Right, n) -> rotate Left n input | |
| RotateBasedOn (x) -> inverseRotateBasedOn x input | |
| Reverse (x, y) -> reverse x y input | |
| Move (src, dest) -> move dest src input) input | |
|> fun chars -> new String(chars) | |
let part2 = "fbgdceah".ToCharArray() |> applyInverse instructions |
Links
- Day 21 challenge description
- Advent of Code 2015
- Solution for Day 20
- All my F# solutions for Advent of Code
- Github repo
Whenever you’re ready, here are 3 ways I can help you:
- 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.
- 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.
- Join my community on Discord, ask questions, and join the discussion on all things AWS and Serverless.
Pingback: F# Weekly #52, 2016 – Sergey Tihon's Blog