Notes on Day 2 of 2023 AdventJS Challenge

Day 2 surprised me because it continued the Christmas theme and the gift-making premise of the first one and I wouldn’t have it any other way. I am now expecting the rest of the challenges to build on top of this.

Table of Contents

Challenge

Today’s topic is related to the previous day’s string manipulation problem wherein given a random string of alphabet characters or “materials”, we need to determine if a “gift” or an actual word (which are Spanish en esto caso hoy) can be spelled using them.

Solution

My solution was simple: split each gift, sort alphabetically and compare with the characters of materials that is also split alphabetically. If all characters of said gift are found, then it is doable in Santa’s workshop.

function manufacture(gifts: Array<string>, materials: string) {
  const materialsArr = materials.split("").sort();

  const results: Array<string> = [];
  for (let i = 0; i < gifts.length; i++) {
    const gift = gifts[i].split("").sort();
    if (gift.every((letter) => materialsArr.includes(letter))) {
      results.push(gifts[i]);
    }
  }

  return results;
}

Lessons Learned

I had to revisit some array prototypes today which I think are good presents for a developer!

Array.prototype.concat()

I am always using push to add elements into the end arrays and looked up other ways to do it out of curiosity. concat does the same, essentially a typical stack push, but it returns a new array without changing the original array.

Array.prototype.every()

The comparison array protoypes every and some are really helpful in some cases in daily work and in today’s problem, I used the first to keep track if all characters of a gift exist in the materials.

Array.prototype.split()

We all use split in our coding challenges’ early array problems and today is not an exception, at least for my solution to the problem.

Declaring export {}

I have been using TypeScript a lot at work and I like the level of “strictness” it gives the developer and so I decided to implement my solutions in TS.

I encountered the following error in my Day 2 TS file when declaring the tests variable:

Cannot redeclare block-scoped variable ‘tests’.ts(2451)

This is because I also have the same variable declared in the Day 1 file and what solved it is adding an empty export object at the end of each TS file.

I was only running each TS file in the terminal so I believe that’s simple and perfect enough for my case aside from the other compiler instructions available.

Using interface and type in TypeScript

In addition to my TS adventures, I wanted to declare a type (or an interface?) for today’s inputs and remembered which between type and interface is better at least for my simple file?

Hochel’s extensive explanation stated what I needed: type is a unique entity while interface can be overriden with the same name.

I know in the end its based on preferences and consistency in the codebase but it’s nice to read back on the idiosyncrasies of reserved words we use when programming.

References

Twitter, LinkedIn