Oxygen System
Tags: adventofcode, algorithms, computerscience, programming
Published: 2022-05-14 16:54:00

## [Advent of Code 2019 Day 15](https://adventofcode.com/2019/day/15)

## [Try the simulator using your puzzle input!](https://aocoxygensystem.rmion.repl.co/)
![Simulator of Part 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d273zd1n4ah62sms6m60.gif)

## Task: Solve for X where...
### Part 1
```
X = the fewest number of movement commands required to move the repair droid from its starting position to the location of the oxygen system
```
### Part 2
```
X = the number of minutes it will take to fill the space with oxygen
```

## No example input
- Instead, several example input instructions and resulting output commands are simulated

## Part 1
1. Intcode computer: Round 7!
2. Forget the algorithm: straight to building a simulator
3. The simulator revealed the path!
4. I painstakingly counted, and got the correct answer

### Intcode computer: Round 7!
- Similar to Round 5 (Day 11), I must move a robot
- Unlike Round 5, the robot's path depends on my instructions, not just the state of the ground upon which it stands

Input rules:
> Only four movement commands are understood: north (1), south (2), west (3), and east (4)

Output rules:
- A single output: one of `0,1,2`
- `0`: Wall. Didn't move.
- `1`: Open. Moved.
- `2`: Moved. At oxygen system!

Mission:
> Direct the repair droid to the oxygen system and fix the problem

### Forget the algorithm: straight to building a simulator
- I have no idea how I would algorithmically solve this puzzle
- But I do know how to build a simulator where I can listen for the next direction, run the program until it outputs another value, and update the robot's position and state of the area based on the status code

Off I go to try. Be back soon!

Ok, I'm back!

### The simulator revealed the path!
- I built the simulator assuming a 50x50 square area, with the droid starting in the middle
- Just like in the instructions, I replace the contents of a cell with the correct object based on the status code returned after each attempted move
- I mapped each keyboard arrow key to the four input directions to make it delightfully interactive

Here's an animation of how the simulator revealed the path
![A recreation of discovering the general path](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1zqf6x9ecnw70dxvwlt8.gif)

### I painstakingly counted, and got the correct answer
After some back-tracking to identify all the walls, I had what I needed to count the minimum steps necessary:
![Three frames showing the path](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sjrklqjvvxt064ppv9pa.gif)

Here's a closer look at the shortest path:
![Shortest path from droid to oxygen](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f8vq5izf0af043w3c1kp.png)

## Part 2
1. Forget the algorithm: I'll make a GIF!
2. To my delight, it was the correct answer!

### Forget the algorithm: I'll make a GIF!
- 313 frames
- Each crafted individually
- Using letters from the alphabet to keep myself on track

It runs in about as many seconds as the hours it took to create:
![Simulation of area filling with oxygen](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l1fmd53ccgopcvhw282l.gif)

### To my delight, it was the correct answer!
- I'm so glad I used the alphabet and named my frames to match the number of minutes I was on

## I did it!!!
- I solved both parts!
- I created my most complex GIF thus far in the series...and thankfully got the correct answer out of the tedious exercise!
- I built an [interactive robot-maze-traversing simulator](https://aocoxygensystem.rmion.repl.co/) that helped me reveal the full maze, including the location of the oxygen system
- I've now completed 7/7 Intcode puzzles
- I've now built 7 Intcode computer simulators

The goal is always to solve, not necessarily with an algorithm.

And in my opinion, building a game is always a great way to solve these puzzles!