← Back View problem

Day 6 — Tuning Trouble

Part 1

Let’s discuss day #6! Interestingly our input it one long string with no newlines, so we won’t need to do any initial splitting. This one is pretty easy compared to yesterday. Our strategy is to use an index, and continously check that index plus the next three characters and see if all four are unique. If not, we increment the index and try again. If we find a unique string part, we break out of the loop and return the index of the last character of that unique group, aka index + 4.

We will check for uniqueness by converting each four-part string into an array of characters, and using a function to check for uniqueness. Let’s have a look at that function first.

function hasDuplicates(array: string[]) {
  return new Set(array).size !== array.length;
}

Since items in a Set must be unique, we can check the size of the set against the length of the array. If they’re not equal, we know there are duplicates.

Now we split each four-character group of strings into an array, and then check them for uniqueness with our aforementioned function.

for (let i = 0; i < input.length; i++) {
  const partArray = input.slice(index, index + 4).split("");

  if (hasDuplicates(partArray)) {
    index++;
  } else {
    break;
  }
}

return index + 4;

A short one today! Our final answer is 1034 📻

Part 2

Another quick one! We’ll simply replace both occurences of 4 in our solution with 14. Voila! The answer is 2472 📡

← Back View problem