## Advent of Code 2021 Day 2

## Try the simulator!

## 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 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

- Generate an array of coordinates accounting for each directional movement
- Map each coordinate into the equivalent number of individual steps that accumulate toward that coordinate's final relative position
- Generate a 2D array that is the size of the area traversed
- 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 - 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.

## Top comments (0)