Last active
December 12, 2018 15:55
-
-
Save willisplummer/4239517085b3388da066c1b74fce40d8 to your computer and use it in GitHub Desktop.
Advent of Code, Day 2
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
import countBy from 'lodash/countBy' | |
const input = ["kbqwtcvzgumhpwelrnaxydpfuj", "kbqwtcvzgsmhpoelryaxydiqij", "kbqwpcvzssmhpoelgnaxydifuj"] // etc | |
// pt 1 | |
const charCounts = countBy(input.map(string => string.split(''), char => char)) | |
const {twos, threes} = charCounts.reduce( | |
(acc, countObj) => ({ | |
twos: Object.values(countObj).includes(2) ? acc.twos + 1 : acc.twos, | |
threes: Object.values(countObj).includes(3) ? acc.threes + 1 : acc.threes, | |
}), | |
{ | |
twos: 0, | |
threes: 0, | |
} | |
) | |
console.log(twos * threes) | |
// pt 2 | |
const splitStrings = input.map(string => string.split('')) | |
const collectDifferences = (stringArrA, stringArrB) => { | |
return stringArrA.reduce( | |
(acc, char, i) => char === stringArrB[i] ? acc : acc.concat([i]) | |
, [] | |
); | |
} | |
const checkAllForMatch = (stringArrA) => { | |
let winner; | |
let i = 0; | |
while (i < splitStrings.length) { | |
const result = collectDifferences(stringArrA, splitStrings[i]); | |
if (result.length === 1) { | |
winner = [stringArrA, result] | |
} | |
i++ | |
} | |
return winner | |
}; | |
const checkEachForWinners = () => { | |
let winner; | |
let i = 0; | |
while (!winner) { | |
const result = checkAllForMatch(splitStrings[i]) | |
if (result) { | |
winner = result; | |
} | |
i++; | |
} | |
return winner; | |
} | |
const results = checkEachForWinners(); | |
results[0].splice(results[1], 1) | |
console.log(results[0].join('')); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment