DEV Community

Discussion on: Advent of Code 2020 Solution Megathread - Day 6: Custom Customs

Collapse
 
mellen profile image
Matt Ellen

I spent a long time trying to get a regex to work for part 2, but I have given up for the time being. Got a short couple of answers for this:

function testScoresp1()
{
  const input = document.getElementsByTagName('pre')[0].innerHTML;
  const groups = input.split('\n\n');
  return groups.map(group => new Set(group.replaceAll('\n', ''))).reduce((sum, group) => sum + group.size, 0);
}

function testScoresp2()
{
  const input = document.getElementsByTagName('pre')[0].innerHTML;
  const groups = input.split('\n\n');
  const alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('');
  return alphabet.reduce((sum, letter) =>
  {
    return sum + groups.filter(group => group.trim().split('\n').every(line => line.indexOf(letter) > -1)).length;
  }, 0);
}
Enter fullscreen mode Exit fullscreen mode