Dive!
Tags: adventofcode, programming, algorithms, computerscience
Published: 2022-02-20 11:22:00

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

## [Try the simulator!](https://aocdive.rmion.repl.co/)
![Puzzle algorithm visualizer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/prje4oagdj0o8z8y290i.gif)

## The task at hand: Solve for X where
`X = the area - width times height - of the space traversed`

### Input is
- A multi-line string
- Each line consist of one of three instructions: `forward up down` and an integer between `1` and `10`

### It represents
- Movement commands for your submarine
- Either horizontal - always to the right - or vertical - up or down

## Part 1
```
Split the input string at each new line character to create an array of strings
  Split each string at the space character to create an array of two strings
For each item in the array of two-string arrays
  Accumulate an array with two numbers - both starting at 0 - according to the following instructions
    If the first string is 'forward'
      Increment the first number in the accumulating array by the second number-coerced string
    If the first string is 'down'
      Increment the second number in the accumulating array by the second number-coerced string
    If the first string is 'up'
      Decrement the second number in the accumulating array by the second number-coerced string
Return the product of both numbers in the accumulated array
```
### Here's an animation describing Part 1's algorithm
![Part 1 algorithm animation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jojjayfnnrqcscoe62go.gif)

## Part 2
```
Split the input string at each new line character to create an array of strings
  Split each string at the space character to create an array of two strings
For each item in the array of two-string arrays
  Accumulate an array with three numbers - each starting at 0 - according to the following instructions
    If the first string is 'forward'
      Increment the first number in the accumulating array by the second number-coerced string
      Increment the second number in the accumulating array by the product of the third number and the second number-coerced string
    If the first string is 'down'
      Increment the third number in the accumulating array by the second number-coerced string
    If the first string is 'up'
      Decrement the third number in the accumulating array by the second number-coerced string
Slice the returned array such that it only includes the first two numbers
Return the product of both numbers in the sliced, accumulated array
```

## Building the simulator was a far greater challenge than solving the puzzle
- The puzzle asked solely for an area
- To build the simulator, I needed to re-create each step in the submarine's movement

1. Generate an array of coordinates accounting for each directional movement
1. Map each coordinate into the equivalent number of individual steps that accumulate toward that coordinate's final relative position
1. Generate a 2D array that is the size of the area traversed
2. Update the elements in that array whose coordinates fall along the path with a value corresponding to the direction of movement: `>` for forward, `^` for up, `v` for down
3. Mathematically update the font size of the board to account for small and large input instruction sets

The result is a rewarding illustration of the submarine's path for my unique puzzle input.
![Unique path from puzzle input](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5u1y6fwd76jk3318lpg3.png)