Skip to content

Instantly share code, notes, and snippets.

@bemijonathan
Created April 21, 2021 15:16
Show Gist options
  • Select an option

  • Save bemijonathan/31785760bb1b9ab99c31f23a7dd0ee39 to your computer and use it in GitHub Desktop.

Select an option

Save bemijonathan/31785760bb1b9ab99c31f23a7dd0ee39 to your computer and use it in GitHub Desktop.
"use strict"
const fs = require("fs");
const assert = require("assert");
const inputValues = []
//get all values from the file input;
fs.readFileSync("input.txt", "utf-8")
.split('\n\n')
.forEach((line) => {
inputValues.push(line.trim().split('\n'))
});
const uniqueValue = (x) => {
// console.log('filter')
return [...x].filter((value, index, self) => self.indexOf(value) === index);
}
// get all unique question in each group
const getUniqueValues = (group) => {
//convert nested array to string
const a = [...group.flat()].join("");
let unique = uniqueValue(a)
return unique.length;
};
const sumOfYesCount = (input) => {
const sum = [];
input.forEach((element) => {
const x = getUniqueValues(element);
sum.push(x);
});
return sum.reduce((a, b) => a + b);
};
const NumberOfQtnGotYes = (input) => {
const yesSum = []
// for Each of the input find the unique value by comparing each answer
input.forEach(group => {
if (group.length === 1) {
//if its only one group
yesSum.push(uniqueValue(group[0]).length);
} else {
let all = [];
uniqueValue(group).forEach(qtn => {
[...qtn].forEach(answer => {
//get a long string of word and if each answer is present in all
let t = group.flat().join("")
let w = [...t].filter(g => {
if (g === answer) return answer
})
if (w.length === group.length) all.push(w)
})
})
const c = uniqueValue(all.flat());
yesSum.push(c.length)
}
})
return yesSum.reduce((e, a) => e + a)
}
console.log("sum of yes count => ", sumOfYesCount(inputValues));
console.log("answers where everyone said yes => ", NumberOfQtnGotYes(inputValues) );
/**
* use your test cases here
*/
// try {
// assert.deepStrictEqual(11, sumOfYesCount(inputValues))
// assert.deepStrictEqual(6, NumberOfQtnGotYes(inputValues))
// console.log('passed assertion test πŸ‘ πŸ‘ πŸ‘ ')
// } catch (error) {
// console.log('failed assertion test πŸ˜’')
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment