Skip to content

Instantly share code, notes, and snippets.

@tom-wagner
Last active January 8, 2019 19:23
Show Gist options
  • Save tom-wagner/7614354996c131fde98dd4c0d8127338 to your computer and use it in GitHub Desktop.
Save tom-wagner/7614354996c131fde98dd4c0d8127338 to your computer and use it in GitHub Desktop.
Advent of Code
/* ----------------------- PART 1 ----------------------- */
const countLetters = (string) => {
const seen = {};
const letters = string.split('');
_.forEach(letters, (char) => {
if (!seen[char]) {
seen[char] = 1;
} else {
seen[char]++;
}
})
return seen;
};
const checkSum = (arr) => {
const twoCount = 0;
const threeCount = 0;
let objects = _.map(arr, countLetters);
_.forEach(objects, obj => {
if (_.some(obj, letterCount => letterCount === 2)) {
twoCount++;
}
if (_.some(obj, letterCount => letterCount === 3)) {
threeCount++;
}
});
return twoCount * threeCount;
};
checkSum(arr); // 7105
/* ----------------------- PART 2 ----------------------- */
const removeChars = (arr) => {
let arrOfChars = arr[0].split('');
let answer = null;
for (let i = 0; i < arrOfChars.length; i++) {
let seen = {};
_.each(arr, (string) => {
let modifiedString = string.slice(0, i) + string.slice(i + 1);
if (!seen[modifiedString]) {
seen[modifiedString] = true;
} else {
answer = modifiedString;
return false;
}
});
if (answer) break;
}
return answer;
};
removeChars(arr); // 'omlvgdokxfncvqyersasjziup'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment