|
| 1 | +/* eslint-env mocha */ |
| 2 | +const expect = require('chai').expect |
| 3 | +const { |
| 4 | + loopRecipesForElves, |
| 5 | + Recipes, |
| 6 | + totalDigitsInArray |
| 7 | +} = require('./recipes') |
| 8 | + |
| 9 | +describe('--- Day 14: Chocolate Charts ---', () => { |
| 10 | + describe('Part 1:', () => { |
| 11 | + describe('new Recipes()', () => { |
| 12 | + it('builds a linked list', () => { |
| 13 | + const recipes = new Recipes(0) |
| 14 | + for (let x = 1; x <= 5; x++) { |
| 15 | + recipes.addRecipe(x) |
| 16 | + } |
| 17 | + expect(recipes.length).to.equal(6) |
| 18 | + expect(recipes.head.value).to.equal(5) |
| 19 | + expect(recipes.tail.value).to.equal(0) |
| 20 | + expect(recipes.tail.prev).to.equal(recipes.head) // circular linked list for prev |
| 21 | + expect(recipes.head.next).to.equal(recipes.tail) // circular linked list for next |
| 22 | + }) |
| 23 | + describe('scoreRecipes()', () => { |
| 24 | + it('adds new recipes based on the provided score', () => { |
| 25 | + const recipes = new Recipes(0) |
| 26 | + for (let x = 1; x <= 5; x++) { |
| 27 | + recipes.addRecipe(x) |
| 28 | + } |
| 29 | + recipes.scoreRecipes(37) |
| 30 | + expect(recipes.head.value).to.equal(7) |
| 31 | + expect(recipes.head.prev.value).to.equal(3) |
| 32 | + expect(recipes.head.prev.prev.value).to.equal(5) |
| 33 | + expect(recipes.head.next).to.equal(recipes.tail) |
| 34 | + }) |
| 35 | + }) |
| 36 | + }) |
| 37 | + describe('totalDigitsInArray()', () => { |
| 38 | + it('calculates the total value of all the digits of all the numbers in the provided array', () => { |
| 39 | + const expected = 34 |
| 40 | + const test = [1, 5, 13, 22, 3, 0, 971] |
| 41 | + const actual = totalDigitsInArray(test) |
| 42 | + expect(actual).to.equal(expected) |
| 43 | + }) |
| 44 | + }) |
| 45 | + describe('loopRecipeForEleves()', () => { |
| 46 | + it('loops through the recipe object for the specified elves the specified number of times', () => { |
| 47 | + const expected = '37101012451589167792' // list of recipe values in the last iteration of the example |
| 48 | + const elves = [3, 7] |
| 49 | + const recipes = new Recipes(elves[0]) |
| 50 | + let actual = '' |
| 51 | + |
| 52 | + elves.forEach((elf, idx) => { |
| 53 | + if (idx === 0) { |
| 54 | + elves[0] = recipes.head |
| 55 | + } else { |
| 56 | + elves[idx] = recipes.addRecipe(elf) |
| 57 | + } |
| 58 | + }) |
| 59 | + |
| 60 | + loopRecipesForElves(elves, recipes, 15) |
| 61 | + |
| 62 | + let iterator = recipes.tail.next |
| 63 | + actual += recipes.tail.value.toString() |
| 64 | + while (iterator !== recipes.tail) { |
| 65 | + actual += iterator.value.toString() |
| 66 | + iterator = iterator.next |
| 67 | + } |
| 68 | + |
| 69 | + expect(expected).to.equal(actual) |
| 70 | + }) |
| 71 | + }) |
| 72 | + }) |
| 73 | +}) |
0 commit comments