From 752f1efe317601a7435cb6dd499480ba82bdc88b Mon Sep 17 00:00:00 2001 From: Seth Etter Date: Sun, 13 Dec 2015 10:02:13 -0600 Subject: [PATCH] Day 5 part 2 --- index.js | 9 ++++++--- lib/day5.js | 42 ++++++++++++++++++++++++++++++++++----- spec/day5.spec.js | 50 +++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 89 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index d624afa..eaa1b0f 100644 --- a/index.js +++ b/index.js @@ -30,8 +30,11 @@ console.log('Day 4, Part 2:', day4Part2Answer); */ var Day5 = require('./lib/day5'); var day5Input = fs.readFileSync('./input/day5.txt', 'utf8'); -var niceStringCount = 0; +var niceStringCount1 = 0; +var niceStringCount2 = 0; day5Input.split('\n').forEach(function(str) { - if (Day5.isStringNice(str)) niceStringCount++; + if (Day5.isStringNice1(str)) niceStringCount1++; + if (Day5.isStringNice2(str)) niceStringCount2++; }); -console.log('Day 5, Part 1:', niceStringCount); +console.log('Day 5, Part 1:', niceStringCount1); +console.log('Day 5, Part 2:', niceStringCount2); diff --git a/lib/day5.js b/lib/day5.js index 570ee38..7f4adb4 100644 --- a/lib/day5.js +++ b/lib/day5.js @@ -2,11 +2,18 @@ var VOWELS = ['a', 'e', 'i', 'o', 'u']; var BAD_COMBOS = ['ab', 'cd', 'pq', 'xy']; module.exports = { - isStringNice: function(str) { + isStringNice1: function(str) { return ( - this.hasThreeVowels(str) - && this.hasDoubleLetter(str) - && !this.hasBadCombos(str) + this.hasThreeVowels(str) && + this.hasDoubleLetter(str) && + !this.hasBadCombos(str) + ); + }, + + isStringNice2: function(str) { + return ( + this.hasRepeatWithBreak(str) && + this.hasMatchingPairs(str) ); }, @@ -28,7 +35,7 @@ module.exports = { if (chars[i] === lastChar) return true; lastChar = chars[i]; } - + return false; }, @@ -36,6 +43,31 @@ module.exports = { for (var i = 0; i < BAD_COMBOS.length; i++) { if (str.indexOf(BAD_COMBOS[i]) !== -1) return true; } + return false; + }, + + hasMatchingPairs: function(str) { + var currentPair = null; + + for (var i = 0; i < str.length; i++) { + var remainderOfStr = str.substring(i + 2, str.length); + currentPair = str[i] + str[i + 1]; + if (remainderOfStr.indexOf(currentPair) !== -1) { + return true; + } + } + + return false; + }, + + hasRepeatWithBreak: function(str) { + var currentChar = null; + + for (var i = 0; i < str.length; i++) { + currentChar = str[i]; + if (str[i+2] == currentChar) return true; + } + return false; } }; diff --git a/spec/day5.spec.js b/spec/day5.spec.js index 5f8d27b..c16ac1c 100644 --- a/spec/day5.spec.js +++ b/spec/day5.spec.js @@ -37,17 +37,59 @@ describe('Day5', function() { }); }); - describe('#isStringNice', function() { - it('checks for all three nice conditions', function() { + describe('#hasMatchingPairs', function() { + it('finds matching pairs of any two letters', function() { + var goodStr = 'abasdofijabfji'; + var badStr = 'absdoifjojsf'; + expect(Day5.hasMatchingPairs(goodStr)).to.equal(true); + expect(Day5.hasMatchingPairs(badStr)).to.equal(false); + }); + + it('does not count pairs that overlap', function() { + var badStr = 'jiosdfaaabxo'; + expect(Day5.hasMatchingPairs(badStr)).to.equal(false); + }); + }); + + describe('#hasRepeatWithBreak', function() { + it('finds a repeated letter with one letter in between', function() { + var goodStrings = ['xnsabaf', 'fbdaaa', 'dsoinfnsa']; + var badStrings = ['fajsdol', 'aosidjf', 'fjdosiap']; + goodStrings.forEach(function(str) { + expect(Day5.hasRepeatWithBreak(str)).to.equal(true); + }) + badStrings.forEach(function(str) { + expect(Day5.hasRepeatWithBreak(str)).to.equal(false); + }) + }); + }); + + describe('#isStringNice1', function() { + it('checks hasThreeVowels, hasDoubleLetter and !hasBadCombos', function() { var goodStrings = ['aeibb', 'oouidd', 'lkajlsdffie']; var badStrings = ['bbbbbbb', 'abbei', 'aeiobx']; goodStrings.forEach(function(str) { - expect(Day5.isStringNice(str)).to.equal(true); + expect(Day5.isStringNice1(str)).to.equal(true); + }); + + badStrings.forEach(function(str) { + expect(Day5.isStringNice1(str)).to.equal(false); + }); + }); + }); + + describe('#isStringNice2', function() { + it('checks hasRepeatWithBreak and hasMatchingPairs', function() { + var goodStrings = ['abfjdsioaba', 'djojdj', 'aspobibsp']; + var badStrings = ['asdaf', 'basdgf', 'poiqhbo']; + + goodStrings.forEach(function(str) { + expect(Day5.isStringNice2(str)).to.equal(true); }); badStrings.forEach(function(str) { - expect(Day5.isStringNice(str)).to.equal(false); + expect(Day5.isStringNice2(str)).to.equal(false); }); }); }); -- 2.51.2