Created
December 7, 2020 03:33
-
-
Save cranderveldt/7543e39861edbbd6629a49276e8ae8e0 to your computer and use it in GitHub Desktop.
Advent of Code Day 6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Pro tip: watch for google trying to auto-translate the puzzle input lol | |
const puzzleInput = `` | |
// Part 1 | |
const parseInput = (input) => { | |
return input.split(/\s\s/) | |
} | |
const groupCountAny = (group) => { | |
const set = new Set(group.match(/[a-z]/g)) | |
return set.size | |
} | |
const partOne = (input) => { | |
const groups = parseInput(input) | |
const totalCount = groups.reduce((acc, group) => acc + groupCountAny(group), 0) | |
console.log(totalCount) | |
} | |
partOne(puzzleInput) | |
// Part 2 | |
const groupCountAll = (group) => { | |
const members = group.split(/\n/).map(member => new Set(member.match(/[a-z]/g))) | |
const answers = members.reduce((acc, member) => [...acc, ...member], []) | |
const uniqueAnswers = [...(new Set(answers))] | |
const sharedAnswers = uniqueAnswers.filter(letter => answers.filter(x => x === letter).length === members.length) | |
return sharedAnswers.length | |
} | |
const partTwo = (input) => { | |
const groups = parseInput(input) | |
const totalCount = groups.reduce((acc, group) => acc + groupCountAll(group), 0) | |
console.log(totalCount) | |
} | |
partTwo(puzzleInput) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment