DEV Community

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

Collapse
 
kudostoy0u profile image
Kudos Beluga • Edited

Javascript answer, Part 1 and 2 in one script, change the part2 boolean to false if you want a part 1 answer.
Looking at other people's responses, mine feels very inefficient :(

let fs = require("fs"), part2 = false, n = 0;
fs.readFile("input.txt","utf8",(err,data) => {
if (err) throw err;
let veryuppercount = 0;
data.split(/\n\s/gi).map(e => {
  if (part2) {
  let uppercount = 0;
 e = e.split(/\n/g);
 e[0].split("").map(f => {
   let counter = 0;
   e.slice(1,e.length).map(g => {
   g.split("").map(h => {
     if (h == f) counter++
   })})
   if (counter == e.length-1) uppercount++
 })
  veryuppercount += uppercount;
  } else {
  e = e.replace(/\n/g,"")
  if (e) {
 let noduplicate = [...new Set(e.split(""))];
 n += noduplicate.length
  }}})
if (part2) console.log(veryuppercount)
else console.log(n)
})
Enter fullscreen mode Exit fullscreen mode