JavaScript: Daily Routine Day 6

Elisa Capololo
4 min readOct 31, 2023
Adventure of Code Day 1 to 6

Hello, I’m here again, but this time to explain what it was like for me to navigate the exercise on day 6, which I can say to this day was the most difficult exercise to solve and it took the longest to find a solution.
I didn’t see the exercise on day 7, but I already know with each step the exercises get more difficult, and my goal is to reach the 25th, that’s the focus, and no matter the time, we’ll stay focused on reaching the goal.

Day 6

Input

Assuming you have already read the exercise and understood that to work with this input I need to recognize the text turn on, off, or toggle, and also recognize the coordinates. The way this input was presented was making me confused and I was asking myself how am I going to recognize and extract each of this data? I understood what the problem was talking about and had a small idea of how to solve it, but working with the inputs was the hard part. I asked my mentor how I could work with the input, and he answered my question with this question: “What are some ways you know of to search for something in a string?”. Then I understood the play.

To search for something in a string, we have:

String indexOf()
String lastIndexOf()
String search()
String match()
String matchAll()
String includes()
String startsWith()
String endsWith()

What did I do?

First I decided to divide my exercise into small parts. As you can see, the image above presents functions that allow you to collectively manipulate and analyze the state of lights in a given array based on specified instructions.

The turn-on function turns on lights in the specified range by setting their brightness to 1.

The turn-off function turns off lights in the specified range by setting their brightness to 0.

The toggle function toggles the lights in the specified range. If a light is on (brightness 1), it will be turned off (brightness 0), and if it’s off, it will be turned on.

The quantityOfLitLights function calculates the total quantity of lit lights by summing up the brightness values in the entire matrix.

Main function

For the code above first, we have the string containing instructions for turning on, turning off, or toggling lights in a grid. Each line represents a separate instruction, because we are using the template string it is more easy to work with the input.

We have a regular expression pattern used to extract start and end coordinates from each instruction. It captures two sets of coordinates in the format “x,y” and “x,y” separated by “ through “.

The Javascript Array.from() method is used to create a new array instance from a given array. In the case of a string, every alphabet of the string is converted to an element of the new array instance, and in the case of integer values, a new array instance simply takes the elements of the given array.

const matrix = Array.from({length: row}, () => Array(column).fill(0));

This creates a 2D array (matrix) with dimensions row x column, filled with zeros. Each element in the matrix represents the brightness of a light.

We have a loop that iterates through each instruction in the array. It uses the regular expression pattern to extract start and end coordinates from each instruction. The coordinates are then converted to numbers and assigned to variables for further use. Based on the type of instruction (turn on, turn off, toggle), it calls the corresponding function (turnOn, turnOff, toggle) with the extracted coordinates to update the matrix. If an instruction doesn’t match the expected pattern, it prints an error message to the console. This calls the quantityOfLitLights function to calculate the total brightness of all lights in the matrix. It prints the total number of lit lights to the console.

--

--

Elisa Capololo

Software Entrepreneur | Front-end Developer | Community Builder | MEST Africa Alumni | Writer in EN and PT