this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Day 5 part 2

+89 -12
+6 -3
index.js
··· 30 30 */ 31 31 var Day5 = require('./lib/day5'); 32 32 var day5Input = fs.readFileSync('./input/day5.txt', 'utf8'); 33 - var niceStringCount = 0; 33 + var niceStringCount1 = 0; 34 + var niceStringCount2 = 0; 34 35 day5Input.split('\n').forEach(function(str) { 35 - if (Day5.isStringNice(str)) niceStringCount++; 36 + if (Day5.isStringNice1(str)) niceStringCount1++; 37 + if (Day5.isStringNice2(str)) niceStringCount2++; 36 38 }); 37 - console.log('Day 5, Part 1:', niceStringCount); 39 + console.log('Day 5, Part 1:', niceStringCount1); 40 + console.log('Day 5, Part 2:', niceStringCount2);
+37 -5
lib/day5.js
··· 2 2 var BAD_COMBOS = ['ab', 'cd', 'pq', 'xy']; 3 3 4 4 module.exports = { 5 - isStringNice: function(str) { 5 + isStringNice1: function(str) { 6 + return ( 7 + this.hasThreeVowels(str) && 8 + this.hasDoubleLetter(str) && 9 + !this.hasBadCombos(str) 10 + ); 11 + }, 12 + 13 + isStringNice2: function(str) { 6 14 return ( 7 - this.hasThreeVowels(str) 8 - && this.hasDoubleLetter(str) 9 - && !this.hasBadCombos(str) 15 + this.hasRepeatWithBreak(str) && 16 + this.hasMatchingPairs(str) 10 17 ); 11 18 }, 12 19 ··· 28 35 if (chars[i] === lastChar) return true; 29 36 lastChar = chars[i]; 30 37 } 31 - 38 + 32 39 return false; 33 40 }, 34 41 ··· 36 43 for (var i = 0; i < BAD_COMBOS.length; i++) { 37 44 if (str.indexOf(BAD_COMBOS[i]) !== -1) return true; 38 45 } 46 + return false; 47 + }, 48 + 49 + hasMatchingPairs: function(str) { 50 + var currentPair = null; 51 + 52 + for (var i = 0; i < str.length; i++) { 53 + var remainderOfStr = str.substring(i + 2, str.length); 54 + currentPair = str[i] + str[i + 1]; 55 + if (remainderOfStr.indexOf(currentPair) !== -1) { 56 + return true; 57 + } 58 + } 59 + 60 + return false; 61 + }, 62 + 63 + hasRepeatWithBreak: function(str) { 64 + var currentChar = null; 65 + 66 + for (var i = 0; i < str.length; i++) { 67 + currentChar = str[i]; 68 + if (str[i+2] == currentChar) return true; 69 + } 70 + 39 71 return false; 40 72 } 41 73 };
+46 -4
spec/day5.spec.js
··· 37 37 }); 38 38 }); 39 39 40 - describe('#isStringNice', function() { 41 - it('checks for all three nice conditions', function() { 40 + describe('#hasMatchingPairs', function() { 41 + it('finds matching pairs of any two letters', function() { 42 + var goodStr = 'abasdofijabfji'; 43 + var badStr = 'absdoifjojsf'; 44 + expect(Day5.hasMatchingPairs(goodStr)).to.equal(true); 45 + expect(Day5.hasMatchingPairs(badStr)).to.equal(false); 46 + }); 47 + 48 + it('does not count pairs that overlap', function() { 49 + var badStr = 'jiosdfaaabxo'; 50 + expect(Day5.hasMatchingPairs(badStr)).to.equal(false); 51 + }); 52 + }); 53 + 54 + describe('#hasRepeatWithBreak', function() { 55 + it('finds a repeated letter with one letter in between', function() { 56 + var goodStrings = ['xnsabaf', 'fbdaaa', 'dsoinfnsa']; 57 + var badStrings = ['fajsdol', 'aosidjf', 'fjdosiap']; 58 + goodStrings.forEach(function(str) { 59 + expect(Day5.hasRepeatWithBreak(str)).to.equal(true); 60 + }) 61 + badStrings.forEach(function(str) { 62 + expect(Day5.hasRepeatWithBreak(str)).to.equal(false); 63 + }) 64 + }); 65 + }); 66 + 67 + describe('#isStringNice1', function() { 68 + it('checks hasThreeVowels, hasDoubleLetter and !hasBadCombos', function() { 42 69 var goodStrings = ['aeibb', 'oouidd', 'lkajlsdffie']; 43 70 var badStrings = ['bbbbbbb', 'abbei', 'aeiobx']; 44 71 45 72 goodStrings.forEach(function(str) { 46 - expect(Day5.isStringNice(str)).to.equal(true); 73 + expect(Day5.isStringNice1(str)).to.equal(true); 74 + }); 75 + 76 + badStrings.forEach(function(str) { 77 + expect(Day5.isStringNice1(str)).to.equal(false); 78 + }); 79 + }); 80 + }); 81 + 82 + describe('#isStringNice2', function() { 83 + it('checks hasRepeatWithBreak and hasMatchingPairs', function() { 84 + var goodStrings = ['abfjdsioaba', 'djojdj', 'aspobibsp']; 85 + var badStrings = ['asdaf', 'basdgf', 'poiqhbo']; 86 + 87 + goodStrings.forEach(function(str) { 88 + expect(Day5.isStringNice2(str)).to.equal(true); 47 89 }); 48 90 49 91 badStrings.forEach(function(str) { 50 - expect(Day5.isStringNice(str)).to.equal(false); 92 + expect(Day5.isStringNice2(str)).to.equal(false); 51 93 }); 52 94 }); 53 95 });