Sonar Sweep
Tags: adventofcode, programming, algorithms, computerscience
Published: 2022-02-21 16:38:44

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

## [Try the simulator!](https://aocsonarsweep.rmion.repl.co/)
![Depth simulator](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q7tvch220la85mvbd7z8.gif)

## The task at hand: Solve for X where
`X = the number of times N depth measurements increase`

1. N = 1
2. N = Sum of each subsequent three in a row

### Input is
- A multi-line string
- Each contains an positive integer, likely starting between 100-200 and ending close to 10000

### It represents
- Measurements of depth on the ocean's floor

## Part 1
```
Split the input into an array of strings
  Coerce each string into a number
Initialize a count to 0
For each number in the array, where the first is already accumulated and the current item starts with the second number
  If the current number is greater than the accumulating number
    Increment the count by 1
  Update the accumulating value to reference the current number
Return count
```

## Part 2
```
Split the input into an array of strings
  Coerce each string into a number
Initialize a count to 0
Initialize a sum from the first three numbers in the array
For each number in the array, starting at the second, and ending at the third-to-last
  Calculate the sum of three numbers: the one at the current location, the one after, and the one after that
  If the sum of these three numbers is greater than the previously initialized sum
    Increment count by 1
  Update the number in the sum outside this loop to reflect the sum calculated inside this loop
Return count
```

## Part 3: Visualizing the plunge in depth
This input:
```
199
200
208
210
200
207
240
269
260
263
```
Looks like this:
```
 \
  \
   \/\
      \
       \/\
```
How could I write an algorithm to generate this map for use in a simulator tool?

```
Split the input into an array of strings
  Coerce each string into a number
Create an array, slopes, to store a series of coordinates and characters
For each number in the array, where the first is already accumulated and the current item starts with the second number
  If the current number is greater than the previous - accumulated - number
    If the slopes array isn't empty, and it's last item has the / character in it
      Add as the new last item in slopes an array with two items:
        1. An array with two numbers: 0 and 1
        2. The \ character
    Else
      Add as the new last item in slopes an array with two items:
        1. An array with two numbers: 1 and 1
        2. The \ character
  Else, if the current number is less than the previous - accumulated - number
    Add as the new last item in slopes an array with two items:
      1. An array with two numbers: 0 and 1
      2. The / character
Create a 2D array with the following dimensions and contents:
  The array should have a length - number of items - equal to the amount of elements in the slopes array where the number in the first index of the nested array is 1
  Each item in the array should have a length - number of items, also the width - equal to the number of items in slopes, since each new step represents a move forward
  The initial character at all indices in the 2D array should be a space character
Initialize a starting coordinate, current, to [-1,-1]
For each array item in the slopes array
  Update each number in current to equal the corresponding numbers in the nested array in the first index of the current slopes item
  Update the space character in the 2D array at the location stored in current - the first number being the row, the second number being the column - to reflect the character in the second index of the slopes item: either \ or /
Print the 2D array as a rectangular board of \ or / or space characters by concatenating each character together in each nested array, then concatenating each new string of characters together, each one separated by a new line character
```
### [Try the simulator!](https://aocsonarsweep.rmion.repl.co/)
Head's up: the second drawn line is very faint.
![Depth simulator](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q7tvch220la85mvbd7z8.gif)

## Year in review
- [25 days attempted and documented](https://dev.to/rmion/series/16285)
- 34/50 stars earned
- [15 simulators crafted](https://aocsimulators.rmion.repl.co/)
- Countless GIFs designed
- Several `A-ha!` moments encountered
- Plenty of theory to study...some day

![2021 final score card](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ghtf5t0kb8cr760zly2.png)