Last active
January 5, 2016 16:14
-
-
Save ghaiklor/0ef327b1fb95383f09f9 to your computer and use it in GitHub Desktop.
Advent of Code (Day 5 Part 1)
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
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