DEV Community

Cover image for Sonar Sweep
Robert Mion
Robert Mion

Posted on

Sonar Sweep

Advent of Code 2021 Day 1

Try the simulator!

Depth simulator

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Part 3: Visualizing the plunge in depth

This input:

199
200
208
210
200
207
240
269
260
263
Enter fullscreen mode Exit fullscreen mode

Looks like this:

 \
  \
   \/\
      \
       \/\
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Try the simulator!

Head's up: the second drawn line is very faint.
Depth simulator

Year in review

2021 final score card

Top comments (0)