|
| 1 | +/* eslint-env mocha */ |
| 2 | +const expect = require('chai').expect |
| 3 | +const { |
| 4 | + calculateXAfterY, |
| 5 | + loopRecipesForElves, |
| 6 | + Recipes, |
| 7 | + totalDigitsInArray |
| 8 | +} = require('./recipes') |
| 9 | + |
| 10 | +describe('--- Day 14: Chocolate Charts ---', () => { |
| 11 | + describe('Part 1:', () => { |
| 12 | + describe('new Recipes()', () => { |
| 13 | + it('builds a linked list', () => { |
| 14 | + const recipes = new Recipes(0) |
| 15 | + for (let x = 1; x <= 5; x++) { |
| 16 | + recipes.addRecipe(x) |
| 17 | + } |
| 18 | + expect(recipes.length).to.equal(6) |
| 19 | + expect(recipes.head.value).to.equal(5) |
| 20 | + expect(recipes.tail.value).to.equal(0) |
| 21 | + expect(recipes.tail.prev).to.equal(recipes.head) // circular linked list for prev |
| 22 | + expect(recipes.head.next).to.equal(recipes.tail) // circular linked list for next |
| 23 | + }) |
| 24 | + describe('scoreRecipes()', () => { |
| 25 | + it('adds new recipes based on the provided score', () => { |
| 26 | + const recipes = new Recipes(0) |
| 27 | + for (let x = 1; x <= 5; x++) { |
| 28 | + recipes.addRecipe(x) |
| 29 | + } |
| 30 | + recipes.scoreRecipes(37) |
| 31 | + expect(recipes.head.value).to.equal(7) |
| 32 | + expect(recipes.head.prev.value).to.equal(3) |
| 33 | + expect(recipes.head.prev.prev.value).to.equal(5) |
| 34 | + expect(recipes.head.next).to.equal(recipes.tail) |
| 35 | + }) |
| 36 | + }) |
| 37 | + }) |
| 38 | + describe('totalDigitsInArray()', () => { |
| 39 | + it('calculates the total value of all the digits of all the numbers in the provided array', () => { |
| 40 | + const expected = 34 |
| 41 | + const test = [1, 5, 13, 22, 3, 0, 971] |
| 42 | + const actual = totalDigitsInArray(test) |
| 43 | + expect(actual).to.equal(expected) |
| 44 | + }) |
| 45 | + }) |
| 46 | + describe('loopRecipeForEleves()', () => { |
| 47 | + it('loops through the recipe object for the specified elves the specified number of times', () => { |
| 48 | + const expected = '37101012451589167792' // list of recipe values in the last iteration of the example |
| 49 | + const elves = [3, 7] |
| 50 | + const recipes = new Recipes(elves[0]) |
| 51 | + let actual = '' |
| 52 | + |
| 53 | + elves.forEach((elf, idx) => { |
| 54 | + if (idx === 0) { |
| 55 | + elves[0] = recipes.head |
| 56 | + } else { |
| 57 | + elves[idx] = recipes.addRecipe(elf) |
| 58 | + } |
| 59 | + }) |
| 60 | + |
| 61 | + loopRecipesForElves(elves, recipes, 15) |
| 62 | + |
| 63 | + let iterator = recipes.tail.next |
| 64 | + actual += recipes.tail.value.toString() |
| 65 | + while (iterator !== recipes.tail) { |
| 66 | + actual += iterator.value.toString() |
| 67 | + iterator = iterator.next |
| 68 | + } |
| 69 | + |
| 70 | + expect(expected).to.equal(actual) |
| 71 | + }) |
| 72 | + }) |
| 73 | + describe('calculateXAfterY(x, y, recipe, elves)', () => { |
| 74 | + it('predicts the next X results after the elves have executed Y', () => { |
| 75 | + const elves = [3, 7] |
| 76 | + const recipes = new Recipes(elves[0]) |
| 77 | + let actual = '' |
| 78 | + |
| 79 | + elves.forEach((elf, idx) => { |
| 80 | + if (idx === 0) { |
| 81 | + elves[0] = recipes.head |
| 82 | + } else { |
| 83 | + elves[idx] = recipes.addRecipe(elf) |
| 84 | + } |
| 85 | + }) |
| 86 | + |
| 87 | + actual = calculateXAfterY(10, 9, recipes, elves) |
| 88 | + expect(actual).to.equal('5158916779') |
| 89 | + }) |
| 90 | + it('predicts the next X results after the elves have executed Y', () => { |
| 91 | + const elves = [3, 7] |
| 92 | + const recipes = new Recipes(elves[0]) |
| 93 | + let actual = '' |
| 94 | + |
| 95 | + elves.forEach((elf, idx) => { |
| 96 | + if (idx === 0) { |
| 97 | + elves[0] = recipes.head |
| 98 | + } else { |
| 99 | + elves[idx] = recipes.addRecipe(elf) |
| 100 | + } |
| 101 | + }) |
| 102 | + |
| 103 | + actual = calculateXAfterY(10, 5, recipes, elves) |
| 104 | + expect(actual).to.equal('0124515891') |
| 105 | + }) |
| 106 | + it('predicts the next X results after the elves have executed Y', () => { |
| 107 | + const elves = [3, 7] |
| 108 | + const recipes = new Recipes(elves[0]) |
| 109 | + let actual = '' |
| 110 | + |
| 111 | + elves.forEach((elf, idx) => { |
| 112 | + if (idx === 0) { |
| 113 | + elves[0] = recipes.head |
| 114 | + } else { |
| 115 | + elves[idx] = recipes.addRecipe(elf) |
| 116 | + } |
| 117 | + }) |
| 118 | + |
| 119 | + actual = calculateXAfterY(10, 18, recipes, elves) |
| 120 | + expect(actual).to.equal('9251071085') |
| 121 | + }) |
| 122 | + it('predicts the next X results after the elves have executed Y', () => { |
| 123 | + const elves = [3, 7] |
| 124 | + const recipes = new Recipes(elves[0]) |
| 125 | + let actual = '' |
| 126 | + |
| 127 | + elves.forEach((elf, idx) => { |
| 128 | + if (idx === 0) { |
| 129 | + elves[0] = recipes.head |
| 130 | + } else { |
| 131 | + elves[idx] = recipes.addRecipe(elf) |
| 132 | + } |
| 133 | + }) |
| 134 | + |
| 135 | + actual = calculateXAfterY(10, 2018, recipes, elves) |
| 136 | + expect(actual).to.equal('5941429882') |
| 137 | + }) |
| 138 | + }) |
| 139 | + }) |
| 140 | +}) |
0 commit comments