Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ghaiklor
Last active January 5, 2016 16:14
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/0ef327b1fb95383f09f9 to your computer and use it in GitHub Desktop.
Save ghaiklor/0ef327b1fb95383f09f9 to your computer and use it in GitHub Desktop.
Advent of Code (Day 5 Part 1)
const fs = require('fs');
const INPUT = fs.readFileSync('./input.txt', 'utf-8').split('\n');
// Dictionary of letters that need to be checked against the rules
const VOWELS = ['a', 'e', 'i', 'o', 'u'];
const DOUBLE_LETTERS = 'abcdefghijklmnopqrstuvwxyz'.split('').map(item => item + item);
const RESTRICTED_LETTERS = ['ab', 'cd', 'pq', 'xy'];
// Methods to check the rules
const isContainThreeVowels = string => string.split('').reduce((vowels, char) => VOWELS.indexOf(char) === -1 ? vowels : ++vowels, 0) >= 3;
const isContainDoubleLetter = string => DOUBLE_LETTERS.some(item => string.indexOf(item) !== -1);
const isContainRestrictedLetters = string => RESTRICTED_LETTERS.some(item => string.indexOf(item) !== -1);
// Composition of all methods above
const isNiceString = string => !!(isContainThreeVowels(string) && isContainDoubleLetter(string) && !isContainRestrictedLetters(string));
const result = INPUT.reduce((total, string) => isNiceString(string) ? ++total : total, 0);
console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment