---
# System prepended metadata

title: much. Coding Challenge 2

---

# much. Coding Challenge 2

## Supply Stack - The Challenge

### Description

Your customer today is `much. Logistic`, a company that operates on Invetory, Warehousing and Logistics. They have parcels being sent all around the world, their inventory department are responsible to coordinate the transfer of parcels and items with employees around ship ports. The latest transfer can depart as soon as the final crates have been loaded to the ships. Paracels are stored in stacks of marked crates, but due to a miscommunication some crates where put in the wrong position and need to be rearranged.

The ship has a giant cargo crane capable of moving crates between stacks, one at a time. To ensure none of the crates get crushed or fall over, the crane operator will rearrange them in a series of carefully-planned steps. After the crates are rearranged, the desired crates will be at the top of each stack.

### Data

To facilitate in the challenge two functions will be given to you. `read_file` which reads from the source input (but feel free to check the file `input.txt`) and the `get_data` that can process the crate order from the source input and transform it into a python dictionary. This dictionary will contain the amount of rows and the order on which the crates are stacked, where the first position is the last crate (the one that touches ground) and the last position is the first crate (the one that is on top of all crates). The data will look as follows:

```python
{
  1: ["Q", "S", "W", "C", "Z", "V", "F", "T"],
  2: ["Q", "R", "B"],
  3: ["B", "Z", "T", "Q", "P", "M", "S"],
  4: ["D", "V", "F", "R", "Q", "H"],
  5: ["J", "G", "L", "D", "B", "S", "T", "P"],
  6: ["W", "R", "T", "Z"],
  7: ["H", "Q", "M", "N", "S", "F", "R", "J"],
  8: ["R", "N", "F", "H", "W"],
  9: ["J", "Z", "T", "Q", "P", "R", "B"]
}
```

### Task 1

The crane operator have a drawing of the starting stacks of crates and the rearrangement procedure (your challenge input). For example:

```text
    [D]    
[N] [C]    
[Z] [M] [P]
 1   2   3 
```
```text
move 1 from 2 to 1
move 3 from 1 to 3
move 2 from 2 to 1
move 1 from 1 to 2
```

In this example, there are three stacks of crates. Stack 1 contains two crates: crate `Z` is on the bottom, and crate `N` is on top. Stack 2 contains three crates; from bottom to top, they are crates `M`, `C`, and `D`. Finally, stack 3 contains a single crate, `P`.

Then, the rearrangement procedure is given. In each step of the procedure, a quantity of crates is moved from one stack to a different stack. In the first step of the above rearrangement procedure, one crate is moved from stack 2 to stack 1, resulting in this configuration:

```text
[D]        
[N] [C]    
[Z] [M] [P]
 1   2   3
```

In the second step, three crates are moved from stack 1 to stack 3. Crates are moved one at a time, so the first crate to be moved (`D`) ends up below the second and third crates:

```
        [Z]
        [N]
    [C] [D]
    [M] [P]
 1   2   3
```

Then, both crates are moved from stack 2 to stack 1. Again, because crates are moved one at a time, crate `C` ends up below crate `M`:

```text
        [Z]
        [N]
[M]     [D]
[C]     [P]
 1   2   3
```

Finally, one crate is moved from stack 1 to stack 2:

```text
        [Z]
        [N]
        [D]
[C] [M] [P]
 1   2   3
```

The employee make the final verification of the shipment just need to know which crate will end up on top of each stack; in this example, the top crates are `C` in stack 1, `M` in stack 2, and `Z` in stack 3, so you should combine these together and give the employee the message `CMZ`.

After the rearrangement procedure completes, what crate ends up on top of each stack?

### Task 2

As you watch the crane operator expertly rearrange the crates, you notice the process isn't following your prediction.

Some mud was covering the writing on the side of the crane, and you quickly wipe it away. The crane isn't a CrateMover 9000 - it's a CrateMover 9001.

The CrateMover 9001 is notable for many new and exciting features, the most important being the ability to pick up and move multiple crates at once.

Again considering the example above, the crates begin in the same configuration:

```text
    [D]    
[N] [C]    
[Z] [M] [P]
 1   2   3 
```
```text
move 1 from 2 to 1
move 3 from 1 to 3
move 2 from 2 to 1
move 1 from 1 to 2
```

Moving a single crate from stack 2 to stack 1 behaves the same as before:

```text
[D]        
[N] [C]    
[Z] [M] [P]
 1   2   3 
```

However, the action of moving three crates from stack 1 to stack 3 means that those three moved crates stay in the same order, resulting in this new configuration:

```text
        [D]
        [N]
    [C] [Z]
    [M] [P]
 1   2   3
```

Next, as both crates are moved from stack 2 to stack 1, they retain their order as well:

```text
        [D]
        [N]
[C]     [Z]
[M]     [P]
 1   2   3
```

Finally, a single crate is still moved from stack 1 to stack 2, but now it's crate `C` that gets moved:

```text
        [D]
        [N]
        [Z]
[M] [C] [P]
 1   2   3
```
In this example, the CrateMover 9001 has put the crates in a totally different order: `MCD`.

Before the rearrangement process finishes, update your simulation so that the employee knows where they should stand to be ready to unload the final shipment. After the rearrangement procedure completes, what crate ends up on top of each stack?

### Source

Based on code question created by Eric Wastl.