Yan Cui

I help clients go faster for less using serverless technologies.

**This article is brought to you by**

**Is your CI build step taking too long?** Try Depot for free today and experience up to **40x faster build speed**!

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

## Day 11

See details of the challenge here.

Today is perhaps the first AOC challenge that is really challenging and required a lot of thought, and I really enjoyed it!

First, let’s model the problem domain.

and we’ll need a way to determine if microchips are fried or we have succeeded in our mission after a move.

At its core the problem here is similar to the Level Order Tree Traversal problem we saw yesterday, where from each move you have a no. of follow-up moves you can make:

- given the floor the elevator lands at, you can take 1 or 2 items from the floor in the next move
- given the floor the elevator lands at, you can move up or down a floor

For instance, given this state:

```
F4 . . . . .
F3 E HG HM LG .
F2 . . . . .
F1 . . . . LM
```

Here are the 6 possible combinations of items that can be loaded onto the elevator for the next move:

- HG
- HM
- LG
- HG, HM
- HG, LG
- HM, LG

additionally, you can also move the elevator up or down, leading to a total of 12 possible moves from this point.

The trick, however, is to ensure you don’t make a move that will results in a state that you had already been previously. This optimization alone is sufficient for you to solve part 1 of the challenge in under a minute, but it wasn’t quite enough for part 2…

The key realization for the second optimization is that, the elements themselves don’t matter, ie. all three of these configurations represent the same problem:

```
F4 . . . . .
F3 . . . LG .
F2 . HG . . .
F1 E . HM . LM
```

```
F4 . . . . .
F3 . . . HG .
F2 . LG . . .
F1 E . LM . HM
```

```
F4 . . . . .
F3 . HG . . .
F2 . . . LG .
F1 E . LM . HM
```

because the essence of the problem have not changed:

- the elevators is at floor 1
- there’s an element whose Generator is on floor 3 and microchip is on floor 1
- there’s an element whose Generator is on floor 2 and microchip is on floor 1

So if we can represent states in this way then we can identify equivalent states and avoid repeating them.

With that in mind, let’s override the * State* type’s

*method so that a state like this*

**ToString**```
F4 . . . . .
F3 E HG HM LG .
F2 . . . . .
F1 . . . . LM
```

is represented as “3-(3, 1)(3, 3)” where the tuples represents the (generator floor, microchip floor) for each element and the ordering is determined only by the floor values.

So now, we can drill into the meat of the solution.

There are couple of things to note here:

- I’m using a mutable
instead of F#’s immutable**HashSet**for performance reasons, but not exposing that mutability to the outside**Set** function returns all possible combinations of items that can be loaded onto the elevator, it’s doing extra work to generate duplicates (and then filtering them out with**itemCombos***Seq.distinct*) but can be easily rectified away- for every combination of items, we generate 1 or 2 new states depending on the ways the elevator can move
- each new state is added to the cache using its string representation above
- those states that lead to fried microchips are ignored and not used to generate even more states

The solution to part 1 looks like this:

### Part 2

You step into the cleanroom separating the lobby from the isolated area and put

on the hazmat suit.

Upon entering the isolated containment area, however, you notice some extra

parts on the first floor that weren’t listed on the record outside:

An elerium generator.

An elerium-compatible microchip.

A dilithium generator.

A dilithium-compatible microchip.

These work just like the other generators and microchips. You’ll have to get

them up to assembly as well.

What is the minimum number of steps required to bring all of the objects,

including these four new ones, to the fourth floor?

With both optimizations in place, part 1 took **1s** and part 2 took **8s** on my MBP, pretty sweet!

## Links

- Day 11 challenge description
- Advent of Code 2015
- Solution for Day 10
- Level Order Tree Traversal in F#
- All my F# solutions for Advent of Code
- Github repo

**Whenever you’re ready, here are 4 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**.- 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. - 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 #51, 2016 – Sergey Tihon's Blog