Skip to content

Instantly share code, notes, and snippets.

@ghaiklor
Last active January 5, 2016 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghaiklor/f1c6206fc5157a1ccd72 to your computer and use it in GitHub Desktop.
Save ghaiklor/f1c6206fc5157a1ccd72 to your computer and use it in GitHub Desktop.
Advent of Code (Day 11 Part 1)
const INPUT = 'cqjxjnds';
// Rules for correct password
const isContainStraightIncreasingSymbols = string => string.split('').map(char => char.charCodeAt(0)).some((char, index, arr) => arr[index] === arr[index + 1] - 1 && arr[index + 1] === arr[index + 2] - 1);
const isContainRestrictedSymbols = string => /i|o|l/.test(string);
const isContainPairs = string => /(\w)\1.*(\w)\2/.test(string);
// Increments one char
const incrementChar = char => char === 'z' ? 'a' : String.fromCharCode(char.charCodeAt(0) + 1);
// Increments the whole string by one char recursively
const incrementString = string => {
const nextChar = incrementChar(string.slice(-1));
return nextChar === 'a' ? incrementString(string.slice(0, -1)) + 'a' : string.slice(0, -1) + nextChar;
};
// Checks if password is valid (based on rules above)
const isValidPassword = string => isContainStraightIncreasingSymbols(string) && !isContainRestrictedSymbols(string) && isContainPairs(string);
let result = INPUT;
while (!isValidPassword(result)) result = incrementString(result);
console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment