barfooz.xyz

Advent of … — Day 3

Typescript

https://toot.cafe/@danbruegge/113587602136584312

I solved this one “early” in the morning. This one, was also a no-brainer.

Code

https://toot.cafe/@danbruegge/113588572520140901

This was a nice and fast one. I always enjoy playing with Regexp. Only Part 2 gave me a struggle, because of my thought process. I tend to do things much more complicated than they should be. :D

Here is a little example:

export function getDoMulCalls(input: string): any {
  const startRe = /^(.*?)don\'t\(\)/g;
  const middleRe = /do\(\)(.*?)don\'t\(\)/g;
  const endRe = /do\(\)(.*?)$/g;

  const startDos = Array.from(input.matchAll(startRe), match => match[1]);
  const middleDos = Array.from(input.matchAll(middleRe), match => match[1]);
  const endDos = Array.from(input.matchAll(endRe), match => match[1]).filter(endDo => !endDo.includes("don't()"));

  return [...startDos, ...middleDos, ...endDos].map(getMulCalls).flat();
}

This will not give you the solution to Part 2. I thought this was simple and clever at the same time. But I needed to start fresh and remove the cleverness from the code. Bonus, I've learned something new with JavaScript and Regexp. :)


47 of #100DaysToOffload

#log #adventOfCode #adventOfTypescript

Thoughts? Discuss...