|
| 1 | +/* eslint-env mocha */ |
| 2 | +const { expect } = require('chai') |
| 3 | +const { getMostCommon, getLeastCommon, getEpsilon, getGamma, getO2, getCO2, calcPowerConsumption, calcLifeSupport } = require('./engineDiagnostics') |
| 4 | + |
| 5 | +const testData = [ |
| 6 | + '00100', |
| 7 | + '11110', |
| 8 | + '10110', |
| 9 | + '10111', |
| 10 | + '10101', |
| 11 | + '01111', |
| 12 | + '00111', |
| 13 | + '11100', |
| 14 | + '10000', |
| 15 | + '11001', |
| 16 | + '00010', |
| 17 | + '01010' |
| 18 | +] |
| 19 | + |
| 20 | +describe('--- Day 3: Binary Diagnostic ---', () => { |
| 21 | + describe('Part 1', () => { |
| 22 | + describe('getMostCommon()', () => { |
| 23 | + it('finds the most common bit at a position', () => { |
| 24 | + expect(getMostCommon(testData, 0)).to.equal('1') |
| 25 | + expect(getMostCommon(testData, 1)).to.equal('0') |
| 26 | + expect(getMostCommon(testData, 2)).to.equal('1') |
| 27 | + expect(getMostCommon(testData, 3)).to.equal('1') |
| 28 | + expect(getMostCommon(testData, 4)).to.equal('0') |
| 29 | + }) |
| 30 | + }) |
| 31 | + describe('getLeastCommon()', () => { |
| 32 | + it('finds the least common bit at a position', () => { |
| 33 | + expect(getLeastCommon(testData, 0)).to.equal('0') |
| 34 | + expect(getLeastCommon(testData, 1)).to.equal('1') |
| 35 | + expect(getLeastCommon(testData, 2)).to.equal('0') |
| 36 | + expect(getLeastCommon(testData, 3)).to.equal('0') |
| 37 | + expect(getLeastCommon(testData, 4)).to.equal('1') |
| 38 | + }) |
| 39 | + }) |
| 40 | + describe('getGamma()', () => { |
| 41 | + it('finds the gamma rate from the provided data using the most common bits in each position', () => { |
| 42 | + expect(getGamma(testData)).to.equal('10110') |
| 43 | + }) |
| 44 | + }) |
| 45 | + describe('getEpsilon()', () => { |
| 46 | + it('finds the epsilon rate from the provided data using the least common bits in each position', () => { |
| 47 | + expect(getEpsilon(testData)).to.equal('01001') |
| 48 | + }) |
| 49 | + }) |
| 50 | + describe('calcPowerConsumption', () => { |
| 51 | + it('calculates the power consumption by multiplying the gamma and epsilon rates as decimals', () => { |
| 52 | + expect(calcPowerConsumption('10110', '01001')).to.equal(198) |
| 53 | + }) |
| 54 | + }) |
| 55 | + }) |
| 56 | + describe('Part 2', () => { |
| 57 | + describe('getO2()', () => { |
| 58 | + it('calculates the oxygen generator rating from the provided data', () => { |
| 59 | + expect(getO2(testData)).to.equal('10111') |
| 60 | + }) |
| 61 | + }) |
| 62 | + describe('getCO2()', () => { |
| 63 | + it('calculates the carbon dioxide scrubber rating from the provided data', () => { |
| 64 | + expect(getCO2(testData)).to.equal('01010') |
| 65 | + }) |
| 66 | + }) |
| 67 | + describe('calcLifeSupport', () => { |
| 68 | + it('calculates the life support rating by multiplying the O2 and C02 rates rates as decimals', () => { |
| 69 | + expect(calcLifeSupport('10110', '01001')).to.equal(198) |
| 70 | + }) |
| 71 | + }) |
| 72 | + }) |
| 73 | +}) |
0 commit comments