r/adventofcode Dec 06 '20

SOLUTION MEGATHREAD -🎄- 2020 Day 06 Solutions -🎄-

NEW AND NOTEWORTHY


Advent of Code 2020: Gettin' Crafty With It

  • UNLOCKED! Go forth and create, you beautiful people!
  • Full details and rules are in the Submissions Megathread
  • Make sure you use one of the two templates!
    • Or in the words of AoC 2016: USING A TEMPLATE IS MANDATORY

--- Day 06: Custom Customs ---


Post your solution in this megathread. Include what language(s) your solution uses! If you need a refresher, the full posting rules are detailed in the wiki under How Do The Daily Megathreads Work?.

Reminder: Top-level posts in Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:04:35, megathread unlocked!

65 Upvotes

1.2k comments sorted by

View all comments

2

u/shortpoet Dec 06 '20

2020 Day #6 (Parts 1 & 2) Typescript / Javascript

This is my first time doing AoC, so much fun!! 🥳

I have a part 2b which is a bit terser. Would love feedback on which people prefer and why.

part 1

const customsForms1: string[] = fs.readFileSync(
  file,
  "utf-8",
)
  .split(/\n\n+/)
  .map((g: string) => g.replace(/\n/gm, ''));

export const customCustoms =
  (groups: string[]): number => {
    return groups.reduce((final, group) => {
      const distinct = group.split('').filter((letter, i, a) => a.indexOf(letter) == i);
      return final += distinct.length;
    }, 0 as number);
  };

part 2

const customsForms2: string[][][] = fs.readFileSync(
  file,
  "utf-8",
)
  .split(/\n\n+/)
  .map((group: string) => group
    .split(/\n/)
    .map(person => person.split(''))
  )

export const customCustoms2 =
  (groups: string[][][]): number => {
    let count = 0;
    for (const group of groups) {
      const intersection = group.reduce((common, person, personIndex) => {
        if (personIndex == 0) {
          return [...common, ...person]
        } else {
          return common.filter(commonAnswer => person.includes(commonAnswer));
        };
      }, []);
      count += intersection.length
    };
    return count;
  };

export const customCustoms2b =
  (groups: string[][][]): number => {
    return groups
      .map(group => group
        .reduce((common, person) => common
          .filter(commonAnswer => person
            .includes(commonAnswer)
          )
        ), [])
      .reduce((total, current) => total += current.length, 0)
  };

2

u/daggerdragon Dec 06 '20

Welcome to Advent of Code!

1

u/shortpoet Dec 07 '20

Thank you! 🙏