diff --git a/index.js b/index.js index e80a2c8..030464e 100644 --- a/index.js +++ b/index.js @@ -1,13 +1,25 @@ var fs = require('fs'); +/** + * Day 1! + * ---------------------------------- + */ +var Day1 = require('./lib/day1'); +var day1Input = fs.readFileSync('./input/day1.txt', 'utf8'); +var day1Part1Answer = require('./lib/day1').floorCount(day1Input); +var day1Part2Answer = require('./lib/day1').entersBasementAt(day1Input); +console.log('Day 1, Part 1:', day1Part1Answer); +console.log('Day 1, Part 2:', day1Part2Answer); + // TODO: Go back and redo days 1 - 3, for consistency :) /** * Day 4! * ---------------------------------- */ +var Day4 = require('./lib/day4'); var day4Input = fs.readFileSync('./input/day4.txt', 'utf8').replace(/[^A-z0-9]/g, ''); -var day4Part1Answer = require('./lib/day4').getTheAnswer(day4Input, '00000'); -var day4Part2Answer = require('./lib/day4').getTheAnswer(day4Input, '000000'); -console.log('Day 4, Part 1: ', day4Part1Answer); -console.log('Day 4, Part 2: ', day4Part2Answer); +var day4Part1Answer = Day4.adventCoinSecret(day4Input, '00000'); +var day4Part2Answer = Day4.adventCoinSecret(day4Input, '000000'); +console.log('Day 4, Part 1:', day4Part1Answer); +console.log('Day 4, Part 2:', day4Part2Answer); diff --git a/input/day1.txt b/input/day1.txt new file mode 100644 index 0000000..becde71 --- /dev/null +++ b/input/day1.txt @@ -0,0 +1 @@ +((((()(()(((((((()))(((()((((()())(())()(((()((((((()((()(()(((()(()((())))()((()()())))))))))()((((((())((()))(((((()(((((((((()()))((()(())()((())((()(()))((()))()))()(((((()(((()()))()())((()((((())()())()((((())()(()(()(((()(())(()(())(((((((())()()(((())(()(()(()(())))(()((((())((()))(((()(()()(((((()()(()(((()(((((())()))()((()(()))()((()((((())((((())(()(((())()()(()()()()()(())((((())((())(()()))()((((())))((((()())()((((())((()())((())(())(((((()((((()(((()((((())(()(((()()))()))((((((()((())()())))(((()(()))(()()(()(((()(()))((()()()())((()()()(((())())()())())())((()))(()(()))(((((()(()(())((()(())(())()((((()())()))((((())(())((())())((((()(((())(())((()()((((()((((((()(())()()(()(()()((((()))(())()())()))(())))(())))())()()(())(()))()((()(()(())()()))(()())))))(()))(()()))(())(((((()(()(()()((())()())))))((())())((())(()(())((()))(())(((()((((((((()()()(()))()()(((()))()((()()(())(())())()(()(())))(((((()(())(())(()))))())()))(()))()(()(((((((()((((())))())())())())()((((((((((((((()()((((((()()()())())()())())())(())(())))())((()())((()(()))))))()))))))))))))))))())((())((())()()))))))(((()((()(()()))((())(()()))()()())))(())))))))(()(((())))())()())))()()(())()))()(()))())((()()))))(()))))()))(()()(())))))))()(((()))))()(()))(())())))))()))((()))((()))())(())))))))))((((())()))()))()))())(())()()(())))())))(()())()))((()()(())))(())((((((()(())((()(((()(()()(())))()))))))()))()(()((()))()(()))(()(((())((((())())(())(()))))))))())))))))())())))))())))))()()(((())()(()))))))))())))))(())()()()))()))()))(()(())()()())())))))))())()(()(()))))()()()))))())(()))))()()))))()())))))(((())()()))(()))))))))))()()))))()()()))))(()())())()()())()(()))))()(()))(())))))))(((((())(())())()()))()()))(())))))()(()))))(())(()()))()())()))()))()))()))))())()()))())())))(()))(()))))))())()(((())()))))))))()))()())))())))())))()))))))))))()()))(()()))))))(())()(()))))())(()))))(()))))(()())))))())())()()))))())()))))))))(()))))()))))))()(()())))))))()))())))())))())))())))))))())(()()))))))(()())())))()())()))))))))))))))())))()(())))()))())()()(())(()()))(())))())()())(()(()(()))))())))))))))))())(()))()))()))))(())()())()())))))))))))()()))))))))))))())())))))(()())))))))))))())(())))()))))))))())())(()))()))(())))()))()()(())()))))))()((((())()))())())))))()))()))))((()())()))))())))(())))))))))))))))))()))))()()())()))()()))))())()))((()())))())))(()))(()())))))))()))()))))(())))))))(())))))())()()(()))())()))()()))))())()()))))())()))())))))))(()))))()())()))))))))(()))())))(()))()))))(())()))())())(())())())))))))((((())))))()))()))()())()(())))()))()))()())(()())()()(()())()))))())())))))(()))()))))())(()()(())))))(())()()((())())))))(())(())))))))())))))))))()(())))))))()())())())()(()))))))))(()))))))))())()()))()(()))))))()))))))())))))))(())))()()(())()())))))(((())))()((())()))())))(()()))())(())())))()(((()())))))()(()()())))()()(()()(()()))())()(()()()))())()()))()())(()))))())))))())))(())()()))))(()))))(())(()))(())))))()()))()))))())()))()()(())())))((()))())()))))))()()))))((()(()))))()()))))))())))))())(()((()())))))))))))()())())))()))(()))))))(()))(())()())))(()))))))))())()()()()))))(()())))))))((())))()))(()))(())(())()())()))))))))(())))())))(()))()()))(()()))(()))())))()(())))())((()((()(())))((())))()))))((((())())()())))(())))()))))))())(()()((())))())()(()())))))(()())()))())))))))((())())))))))(()(()))())()()(()()(((()(((()())))))()))))))()(())(()()((()()(())()()))())()())()))()())())())))))))(((())))))))()()))))))(((())()))(()()))(()()))))(()(()()((((())()())((()()))))(()(())))))()((()()()())()()((()((()()))(()))(((()()()))(((())))()(((())()))))))((()(())())))(()())(((((()(()))(()((()))(()())()))))(()(()))()(()))(())(((())(()()))))()()))(((()))))(()()()()))())))((()()()(())()))()))))()()))()))))))((((((()()()))))())((()()(((()))))(()(())(()()())())())))()(((()()))(())((())))(()))(()()()())((())())())(()))))()))()((()(())()(()()(())(()))(())()))(())(()))))(())(())())(()()(()((()()((())))((()))()((())))(((()()()()((((()))(()()))()()()(((())((())())(()()(()()()))()((())(())()))())(((()()(())))()((()()())()())(()(())())(((())(())())((())(())()(((()()))(())))((())(()())())(())((()()()((((((())))((()(((((())()))()))(())(()()))()))(())()()))(())((()()())()()(()))())()((())))()((()()())((((()())((())())())((()((()))()))((())((()()(()((()()(((())(()()))))((()((())()(((())(()((())())((())(()((((((())())()(()())()(())(((())((((((()(())(()((()()()((()()(()()()())))()()(((((()()))()((((((()))()(()(()(()(((()())((()))())()((()))(())))()))()()))())()()))())((((())(()(()))(((((((())(((()(((((()(((()()((((())(((())())))(()()()(()(()))()))((((((()))((()(((()(())((()((((()((((((())(((((())))(((()(()))))(((()(((())()((())(()((()))(((()()(((())((((()(()(((((()))(((()(((((((()(()()()(()(()(()()())(())(((((()(())())()())(()(()(()))()(()()()())(()()(()((()))()((())())()(()))((())(()))()(()))()(((()(()(()((((((()()()()())()(((((()()(((()()()((()(((((()))((((((((()()()(((((()))))))(()()()(())(()))(()()))))(())()))(((((()(((((()()(()(()())(((()))((((()((()(()(()((()(()((())))()(((()((()))((()))(((((((((()((()((()(())))()((((()((()()))((())(((()(((((()()(()(()()((()(()()()(((((((())())()())))))((((()()(()))()))(()((())()(()(((((((((()()(((()(()())(()((()())((())())((((()(((()(((()((((()((()((((()(()((((((())((((((((((((()()(()()((((((((((((((()((()()))()((((((((((((())((((()(()())((()(()(()))()(((((()()(((()()))()())(())((()(((((()((())(((((()((()(((((()))()()((((())()((((())(((((((((()(())(()(())))())(()((())(((())(())(())())(()(()(())()()((()((())()(((()(((((()(())))()(((()((())))((()()()(((()(((()((()(()(())(()((()())(()(()(((()(((((((((())(()((((()()))(()((((()()()()(((()((((((((()(()()((((((()(()()(()((()((((((((((()()(((((((()())(())))(((()()))(((((()((()()())(()()((((())((()((((()))))(())((()(()()(((()(()(((()((((()(((((()))())())(()((())()))(((()())((())((())((((()((()((((((())(()((((()()))((((((())()(()))((()(((())((((((((((()()(((((()(((((()((()()()((((())))(()))()((()(())()()((()((((((((((()((())(())(((((()(()(()()))((((()((((()()((()(((()(((((((((()(()((()((()))((((((()(((())()()((()(((((((()())))()()(()((()((()()(((()(()()()()((((()((())((((()(((((((((()(((()()(((()(()(((()(((()((())()(()((()(()(()(()))()(((()))(()((((()((())((((())((((((())(()))(()((((())((()(()((((((((()()((((((()(()(()()()(())((()((()()(((()(((((((()()((()(((((((()))(((((()(((()(()()()(()(((()((()()((())(()(((((((((()(()((()((((((()()((())()))(((((()((())()())()(((((((((((()))((((()()()()())(()()(()(()()))()))(()))(()(((()()))())(()(()))()()((())(()())()())()(()))()))(()()(()((((((())((()(((((((((((()(())()((()(()((()((()(()((()((((((((((()()())((())()(())))((())()())()(((((()(()())((((()((()(())(()))(((())()((()))(((((())(()))()()(()))(((())((((()((((()(())))(((((((()))))())()())(())((())()(()()((()(()))()(()()(()()((()())((())((()()))((((()))()()))(()()(())()()(((((()(())((()((((()))()))(()())())(((()()(()()))(())))))(()))((())(((((()((((()))()((((()))()((())(((())))(((()())))((()(()()(( diff --git a/lib/day1.js b/lib/day1.js new file mode 100644 index 0000000..90036de --- /dev/null +++ b/lib/day1.js @@ -0,0 +1,26 @@ +module.exports = { + floorCount: function(input) { + var floor = 0; + + input.split('').forEach((movement) => { + if (movement === '(') floor++; + if (movement === ')') floor--; + }); + + return floor; + }, + entersBasementAt: function(input) { + var floor = 0, + basementAt = null; + + input.split('').forEach((movement, idx) => { + if (movement === '(') floor++; + if (movement === ')') floor--; + if (!basementAt && floor === -1) { + basementAt = idx + 1; + } + }); + + return basementAt; + } +}; diff --git a/lib/day4.js b/lib/day4.js index 6593719..1752573 100644 --- a/lib/day4.js +++ b/lib/day4.js @@ -2,7 +2,7 @@ var crypto = require('crypto'); var fs = require('fs'); module.exports = { - getTheAnswer: function(input, frontPadding) { + adventCoinSecret: function(input, frontPadding) { var hash = '', answer = -1; diff --git a/spec/day1.spec.js b/spec/day1.spec.js new file mode 100644 index 0000000..879c9fe --- /dev/null +++ b/spec/day1.spec.js @@ -0,0 +1,19 @@ +var Day1 = require('../lib/day1'); + +var expect = require('chai').expect; + +describe('Day1', function() { + describe('#floorCount', function() { + it('returns appropriate floor count from input', function() { + expect(Day1.floorCount('(((())')).to.equal(2); + expect(Day1.floorCount('))))')).to.equal(-4); + }); + }); + + describe('#entersBasementAt', function() { + it('returns the char position when first entering basement', function() { + expect(Day1.entersBasementAt(')')).to.equal(1); + expect(Day1.entersBasementAt('((())))(()))')).to.equal(7); + }); + }); +}); diff --git a/spec/day4.spec.js b/spec/day4.spec.js index 31a9a86..30e59b4 100644 --- a/spec/day4.spec.js +++ b/spec/day4.spec.js @@ -2,8 +2,10 @@ var expect = require('chai').expect; var Day4 = require('../lib/day4'); -describe('getTheAnswer', function() { - it('returns the correct answer', function() { - expect(Day4.getTheAnswer('abcdef', '00000')).to.equal(609043); +describe('Day4', function() { + describe('#adventCoinSecret', function() { + it('returns the correct answer', function() { + expect(Day4.adventCoinSecret('abcdef', '00000')).to.equal(609043); + }); }); });