diff --git a/2021/day-03/engineDiagnostics.js b/2021/day-03/engineDiagnostics.js new file mode 100644 index 0000000..68748d7 --- /dev/null +++ b/2021/day-03/engineDiagnostics.js @@ -0,0 +1,86 @@ +/** + * Find the most common bit at a position + * @param [str] data + * @param int position + */ +const getMostCommon = (data, position) => { + const offs = data.filter((reading) => { + return reading[position] === '0' + }).length + // there can only be 2 values, so easy to check which has more as being above 50% + return (offs > data.length / 2) ? '0' : '1' +} + +/** + * Find the least common bit at a position + * @param [str] data + * @param int position + */ +const getLeastCommon = (data, position) => { + return (getMostCommon(data, position) === '0') ? '1' : '0' +} + +/** + * Calc the gamma value of the data set + * @param [str] data + */ +const getGamma = (data) => { + let gamma = '' + for (let x = 0; x < data[0].length; x++) { + gamma += getMostCommon(data, x) + } + return gamma +} + +const getEpsilon = (data) => { + let epsilon = '' + for (let x = 0; x < data[0].length; x++) { + epsilon += getLeastCommon(data, x) + } + return epsilon +} + +const getO2 = (data) => { + return getAir(data, getMostCommon) +} + +const getCO2 = (data) => { + return getAir(data, getLeastCommon) +} + +const getAir = (data, filterMethod) => { + let dataset = data + // Loop through each digit, find the most common bit for that digit, and filter + // out any readings that don't share that digit + // + // TODO: Probably faster with bitmap math, but .... ehh... runs fast enough + for (let x = 0; x < data[0].length; x++) { + if (dataset.length > 1) { + const bit = filterMethod(dataset, x) + dataset = dataset.filter((reading) => { + return reading[x] === bit + }) + } + } + + if (dataset.length > 1) { + throw new Error(`Found too many results ${dataset}`) + } + + return dataset[0] +} + +const calcPowerConsumption = (gamma, epsilon) => { + return parseInt(gamma, 2) * parseInt(epsilon, 2) +} + +module.exports = { + getGamma, + getEpsilon, + getMostCommon, + getLeastCommon, + getO2, + getCO2, + calcPowerConsumption, + calcLifeSupport: calcPowerConsumption +} diff --git a/2021/day-03/engineDiagnostics.test.js b/2021/day-03/engineDiagnostics.test.js new file mode 100644 index 0000000..af0f85b --- /dev/null +++ b/2021/day-03/engineDiagnostics.test.js @@ -0,0 +1,73 @@ +/* eslint-env mocha */ +const { expect } = require('chai') +const { getMostCommon, getLeastCommon, getEpsilon, getGamma, getO2, getCO2, calcPowerConsumption, calcLifeSupport } = require('./engineDiagnostics') + +const testData = [ + '00100', + '11110', + '10110', + '10111', + '10101', + '01111', + '00111', + '11100', + '10000', + '11001', + '00010', + '01010' +] + +describe('--- Day 3: Binary Diagnostic ---', () => { + describe('Part 1', () => { + describe('getMostCommon()', () => { + it('finds the most common bit at a position', () => { + expect(getMostCommon(testData, 0)).to.equal('1') + expect(getMostCommon(testData, 1)).to.equal('0') + expect(getMostCommon(testData, 2)).to.equal('1') + expect(getMostCommon(testData, 3)).to.equal('1') + expect(getMostCommon(testData, 4)).to.equal('0') + }) + }) + describe('getLeastCommon()', () => { + it('finds the least common bit at a position', () => { + expect(getLeastCommon(testData, 0)).to.equal('0') + expect(getLeastCommon(testData, 1)).to.equal('1') + expect(getLeastCommon(testData, 2)).to.equal('0') + expect(getLeastCommon(testData, 3)).to.equal('0') + expect(getLeastCommon(testData, 4)).to.equal('1') + }) + }) + describe('getGamma()', () => { + it('finds the gamma rate from the provided data using the most common bits in each position', () => { + expect(getGamma(testData)).to.equal('10110') + }) + }) + describe('getEpsilon()', () => { + it('finds the epsilon rate from the provided data using the least common bits in each position', () => { + expect(getEpsilon(testData)).to.equal('01001') + }) + }) + describe('calcPowerConsumption', () => { + it('calculates the power consumption by multiplying the gamma and epsilon rates as decimals', () => { + expect(calcPowerConsumption('10110', '01001')).to.equal(198) + }) + }) + }) + describe('Part 2', () => { + describe('getO2()', () => { + it('calculates the oxygen generator rating from the provided data', () => { + expect(getO2(testData)).to.equal('10111') + }) + }) + describe('getCO2()', () => { + it('calculates the carbon dioxide scrubber rating from the provided data', () => { + expect(getCO2(testData)).to.equal('01010') + }) + }) + describe('calcLifeSupport', () => { + it('calculates the life support rating by multiplying the O2 and C02 rates rates as decimals', () => { + expect(calcLifeSupport('10110', '01001')).to.equal(198) + }) + }) + }) +}) diff --git a/2021/day-03/index.js b/2021/day-03/index.js new file mode 100644 index 0000000..af7e035 --- /dev/null +++ b/2021/day-03/index.js @@ -0,0 +1,3 @@ +// eslint-disable-next-line no-unused-vars +const console = require('../helpers') +require('./solution') diff --git a/2021/day-03/input.txt b/2021/day-03/input.txt new file mode 100644 index 0000000..79a8fb8 --- /dev/null +++ b/2021/day-03/input.txt @@ -0,0 +1,1000 @@ +101011111001 +111111011110 +010000111100 +101000010000 +011101100010 +010111000101 +011111011011 +011010011111 +000111000010 +000010011001 +000000100101 +000110000010 +011001010110 +011010000101 +010001000100 +111101010101 +000111110011 +010111010000 +010010000011 +101011110011 +010010010010 +010110100101 +101011010001 +101011101100 +100111011001 +100111101001 +111000111010 +100111010101 +101011100011 +110011001010 +111100011011 +111101110001 +101100000011 +000011000100 +001001000110 +001011001010 +010111101001 +100011000111 +111100010101 +101100000010 +001011111011 +101100111001 +101110110010 +011111001010 +001111001001 +110111101100 +011111001110 +111000111110 +011100101110 +000001101011 +110101001011 +111000111001 +010110011000 +000101100101 +101010110011 +111100001000 +100101000010 +110000111111 +011110001011 +000110100110 +110010001010 +001110110011 +000010000001 +100010011111 +001100001011 +110001111001 +100010011101 +010100000111 +110101000100 +011110010111 +100011010111 +001011011111 +110110010000 +101110110000 +101001001011 +011110100000 +100000000101 +100101011100 +111101101101 +101100110110 +000000011100 +101111000111 +110001000101 +000110000110 +001010001101 +101101111111 +011000011010 +101111010000 +001000101101 +010100001111 +101110011010 +011110010000 +001011011010 +100100010000 +111100011111 +111101000110 +010101101110 +010001010010 +100100000100 +101010111010 +001010001011 +000011101110 +011100110001 +000001111111 +100101000001 +101110000100 +111111100101 +111010111100 +111100000011 +000111001110 +010111001000 +010010110000 +100101000110 +100111001111 +101001110011 +001110011000 +111000001000 +010110011001 +000011001011 +000100001101 +100101110010 +110111110011 +011100000100 +111011000010 +110011010010 +011011101110 +010010111011 +110100001000 +100100111000 +111001011011 +110111000111 +011010111110 +010110011110 +111111011111 +101100110000 +011000011100 +001111111010 +011110111111 +101110110110 +100100000011 +001001100001 +010010001001 +101001100100 +110100011001 +000010101100 +001010110001 +001110011110 +111000110011 +110100001010 +110110011110 +111010100110 +100110000000 +001111010000 +110001011010 +000000101111 +100110110101 +011011100101 +001011100100 +100010111111 +010011010011 +010011100001 +011001100110 +000111101010 +111101100010 +111011101111 +111001111001 +001000001111 +010100001001 +000000011010 +111011101110 +100001000111 +010010010101 +011001100000 +100101110011 +010001011011 +110011010000 +111011001110 +001111010111 +110010110100 +101111110000 +000111011111 +111111011001 +010011001011 +101000110011 +001110101100 +001110000110 +001001110100 +000101011001 +101001111001 +001111111001 +100111111100 +010100001010 +011101111100 +011100011011 +111100110111 +000110011000 +100001111001 +000111111111 +111010010100 +110011111011 +010000001000 +000010110101 +000011111010 +010011001110 +011111000001 +110011001110 +100111110010 +011111011110 +011110110111 +001011010010 +110101010001 +100000100010 +000110100111 +101100101010 +001110111101 +010000101101 +001101011100 +101101010111 +101101010000 +111000100010 +011100110000 +001111110111 +001011000101 +111100111000 +001000001110 +101010110101 +010100110110 +101110111000 +110010000111 +111100111010 +100010111010 +010001011111 +010101011101 +010011110011 +000110110111 +000001111011 +110100010001 +001000010100 +101010011001 +010011100110 +110110011011 +110111000100 +101001110100 +010001111110 +011100001100 +011011010101 +110010010000 +101001011101 +101010010000 +111011110100 +011101110001 +111000011110 +100000100011 +010110101001 +110111111101 +010101011011 +000001110100 +000000000010 +111100101011 +111100001100 +110111000011 +011000011001 +110000011011 +100010001011 +110010011100 +001010001000 +011010000000 +110111100001 +000011001101 +001010001001 +000011101001 +011010010111 +111011011100 +110001111011 +001000111011 +110001111111 +111110011111 +011000111101 +001111101110 +111111001011 +010101110010 +000011100000 +100101101110 +001100000100 +101111001110 +001000110101 +111001011111 +011111110101 +010101011010 +111000101100 +011100000010 +011101011110 +011001101011 +000100100100 +100100010010 +010011000100 +111111100001 +010000100000 +111010001101 +000010010110 +100011101000 +100110011010 +000110100101 +011111100101 +111001101010 +101110001011 +101110101010 +011000010110 +101110011110 +101011001000 +001101000100 +111100111001 +011011101001 +100000010100 +100110101010 +010110001100 +010100100010 +001001011101 +110101101011 +111110011001 +011001011010 +000101000100 +100010111011 +000111110010 +011010001000 +001110010000 +100011001001 +010010010000 +110000010001 +000000110110 +011010000001 +010100010101 +000101011110 +101100010111 +010001111000 +100010001100 +100100100001 +011011000010 +111001001101 +111001010101 +111011011011 +001101100010 +010001000010 +111111011101 +011101101001 +010011100011 +000001000000 +100100010110 +010011000010 +010110010001 +111001001001 +010110001110 +000101110111 +001011101001 +001011111000 +111110111100 +000110111110 +001100111100 +001011000001 +110000100111 +000111001111 +101101111001 +111110100111 +111111111111 +111011010111 +001101001011 +011001011001 +010110111101 +111111000001 +001001100100 +111111101011 +011111100011 +010010110101 +011010111011 +010001010111 +001011000011 +100100001100 +110011011010 +000111111011 +011101010011 +010100010000 +110100010011 +101001110110 +111101110010 +000100001001 +111001101110 +111010110010 +010011000001 +110111011111 +111101111000 +100011010110 +001001010001 +010101100010 +110101110100 +000111101110 +111011110110 +010011011001 +100000011000 +111001110100 +011110010100 +110011011100 +011011010100 +101010101000 +000000111111 +001111110000 +110000011100 +100010100000 +101110000000 +011010011110 +110101010110 +011011100110 +111000101000 +100011101010 +000001100001 +101010011111 +111100001010 +111101001110 +011010111010 +001000011000 +101011010010 +101110001010 +101110011000 +000110000111 +110001101111 +100111000010 +011000011101 +100011000001 +010001100100 +111001100011 +111111100000 +111100101110 +001101111101 +110111101111 +100001011110 +111110100001 +011110100001 +110001001001 +001111010010 +011100100001 +010101100011 +011111011111 +010100101111 +100010001010 +010101111000 +000000001101 +000011011010 +100000010011 +001111010011 +101110110101 +000100000000 +001100101100 +100111110100 +001011100111 +110110100000 +111000000000 +110011001101 +010100011011 +100011010100 +000101011101 +110111110001 +010010100011 +011101000000 +100001001111 +101010110100 +110001101110 +100011111001 +011111011001 +000101000010 +001110100110 +100011011111 +100000000011 +110100000101 +001110110100 +101101001110 +000011010100 +110000001100 +110010110101 +110110101010 +001010001110 +111000010100 +011111100110 +000100011110 +010000010011 +010010100001 +000111010111 +110001001101 +010010110110 +010111010011 +101101011000 +100001110111 +101110001110 +010001110000 +101001111111 +100000001100 +000010000011 +011110010011 +110001000100 +001101110111 +110000110110 +100010000100 +100011111110 +001010000101 +111011010101 +111100000010 +110110010111 +000100111011 +011110111000 +001100001000 +101010100101 +110000111100 +010110100111 +111111111011 +110111011100 +111101110000 +111010000100 +001000110000 +101010101100 +100011100011 +110011110010 +001010111110 +100001010001 +010010111000 +000011001110 +001100010000 +011100110111 +001011101100 +011000111110 +011101001111 +011010000111 +111000010000 +001111001111 +011001100111 +011000110101 +111100110010 +000101000111 +101100110001 +110010110000 +000011011111 +110010011001 +010011000101 +010000111110 +000100101000 +101101100000 +110011101110 +011100100100 +111011100100 +100101010111 +001000010111 +011011110001 +011100010000 +001011110010 +111111100011 +001011111100 +110011101100 +100111000111 +010011011111 +100100110001 +101111101001 +101001110010 +000010110111 +111011111111 +100011111010 +101010111001 +101111100001 +000000011001 +011111000011 +000100001110 +001011110100 +000110111111 +001101110101 +000000010001 +010111001100 +001010100110 +110110000101 +110001000010 +100010000011 +001011001000 +101111011110 +111011111100 +011000011000 +101010101010 +111010110011 +110101000101 +010101000110 +000000010011 +101000001010 +100010101110 +110110000001 +011100010101 +101000110111 +010101000001 +000011011100 +110010100010 +011110110011 +010111111000 +110100101101 +111011111101 +011111111101 +100101001011 +010111000001 +000011011001 +000011100010 +100101111101 +000000101001 +111111000111 +111011000110 +101000001100 +000110000001 +001111011100 +000100010000 +100111101011 +111011011010 +011110010110 +001010010101 +100100110110 +000101111001 +000100011010 +011010010010 +001101001000 +001011011110 +000011101011 +001100011001 +110101100011 +100010000101 +011011100001 +011011110000 +100101100011 +010110111000 +001001001011 +000010111110 +000011010101 +110110011111 +001111000101 +110101111011 +011010010100 +000100110011 +110011110110 +010011101011 +100100001101 +110010000101 +010111100111 +100111111000 +011001001011 +011010100011 +110101111000 +011000111010 +011101000100 +001100110000 +010101111011 +101010010010 +010001000110 +000101111111 +110111001000 +110101111010 +100110010100 +110001000001 +011110001001 +010111110011 +010001010110 +011100111001 +110000010110 +010000010110 +010111111011 +000101101110 +100000001111 +010111111010 +000010001111 +100101110100 +000111100010 +000000101101 +010001011010 +010110111010 +011110111010 +110100000100 +101110000011 +110110100100 +110111110111 +110101011100 +100010010101 +001010110100 +110101001101 +000100110010 +111111111010 +001001000001 +000011110001 +010110111011 +001111101011 +010100100101 +001110010111 +100010010111 +100011110101 +001100001100 +010000000100 +011010110011 +100011101110 +010000000001 +100010010011 +110100111001 +000000110001 +000001000101 +011011001011 +000100101001 +010100001110 +000110001111 +011110000110 +110000000110 +010101001000 +000110111010 +011111100010 +111111010101 +101011000001 +001010101101 +110101111111 +001110110101 +010100101001 +110101100111 +010000101000 +011011110110 +111111110110 +010000100111 +000011010001 +101000011010 +001110000000 +111010110110 +011110011110 +100101001101 +101000000010 +001111101111 +011100001011 +011000011111 +001001001010 +100010011010 +100010100011 +000110010011 +100001110100 +010000011110 +011111000100 +001010101111 +111111001100 +010101110011 +110100110000 +010110010101 +111011000111 +001000111001 +010110110101 +110001011111 +010010101100 +110000101101 +100010000001 +111111110011 +110000010010 +111110000001 +100101011000 +010101010111 +100111101100 +100110101100 +110111000110 +000011111110 +100101100010 +000010010001 +111100100011 +001001111100 +100011110110 +111111011100 +110001100011 +110010111110 +000100010001 +000101100110 +100100000110 +010001101001 +011010010110 +100010110000 +001000001000 +101101011100 +111100111110 +001001011011 +010111101000 +100001100010 +000101111101 +101111110101 +001110000111 +010011010010 +100100110101 +101011011111 +111100010010 +110111010000 +111001101011 +100001001101 +101100000101 +011100101000 +000000100011 +000000111110 +001010001100 +100001110101 +100111011100 +110111100000 +101010101011 +001011000000 +011011111110 +001000101111 +101111101100 +001010011001 +100110101001 +101110010001 +110000011101 +001110000001 +000001100110 +011001101100 +000100000101 +110111010001 +101101100100 +010110101000 +001100001001 +000111001001 +010010101000 +101000001101 +110010000001 +110110010101 +110110001111 +101100110010 +000101110101 +011011101011 +000100100111 +110000111011 +101011011010 +000010101001 +110101101010 +111100101111 +101001010111 +110101100010 +001100100010 +101110111110 +110011110101 +000101111010 +011000111001 +110101001000 +101000100010 +111111001000 +111001101100 +110010100111 +011010110100 +100101111010 +000110010101 +101110000001 +001011110011 +011111110001 +101000101001 +011011001111 +100010001110 +111111110111 +010101111100 +100001000001 +010011110100 +001000010110 +001011101111 +010100011000 +011110111101 +111100110100 +100111011011 +000011001000 +001110100000 +010100011001 +101110001101 +100011001010 +111101011001 +000011100011 +110101011110 +111110001010 +010100010010 +000110110010 +001001001001 +100010110001 +110000110101 +111000101101 +011110010101 +010001000000 +000110001001 +011001111110 +010100111111 +001101110001 +011110000001 +110111111011 +011101000111 +110000111101 +010001110100 +010111011001 +110111010101 +001101000001 +011010000010 +110011010100 +111000110110 +001000000100 +111011001111 +010101010000 +001011101010 +010011010110 +111101100101 +111011011000 +001000100010 +111010000000 +101111011111 +100000111111 +110010000000 +100000011111 +110011001011 +001001111111 +010101101101 +111100011010 +100101100101 +001110001011 +001110111111 +101110011001 +100100110111 +110100000000 +111010110111 +111100110001 +111010100000 +010001100101 +011110100010 +100110010010 +001010110010 +011000100111 +010011011000 +110011010110 +010000001110 +011100111111 +011011000100 +101111001101 +111111011010 +011111010101 +111011101101 +011010000110 +011111111011 +010000011001 +001010001111 +100001010011 +101011001101 +011100111100 +110100011111 +011111011000 +110010111001 +111100100000 +101111100010 +100001101001 +110010111011 +010010001011 +001000011110 +000101010111 +011011111001 +101101000101 +000101100111 +101111111110 +000100110111 +111000011010 +010110111100 +111100001110 +111000100001 +010100000011 +010100110111 +110111010100 +011001001101 +100100101010 +111110110000 +110101110001 +110010100000 +001010011101 +111101001101 +010010000000 +111010111011 +011011110011 +111010001111 +101001111101 +100110000110 +000000000000 +010001110011 +100000111000 +101010100010 +010101100100 +110000110000 +000010011101 +011111001000 +111110011010 +010101001001 +011001111010 +101111000011 +110010111111 +110110001101 +000001010110 +101011100001 +100111100001 +010100100000 +100011101100 +010001100011 +110011000111 +000000000101 +101111110001 +010110011101 +111101000001 +101101011011 +100011101011 diff --git a/2021/day-03/solution.js b/2021/day-03/solution.js new file mode 100644 index 0000000..6ec52eb --- /dev/null +++ b/2021/day-03/solution.js @@ -0,0 +1,40 @@ +const fs = require('fs') +const path = require('path') +const filePath = path.join(__dirname, 'input.txt') +const { inputToArray } = require('../../2018/inputParser') +const { getGamma, getEpsilon, calcPowerConsumption, calcLifeSupport, getO2, getCO2 } = require('./engineDiagnostics') + +fs.readFile(filePath, { encoding: 'utf8' }, (err, initData) => { + if (err) throw err + + initData = inputToArray(initData.trim()) + + const resetInput = () => { + // Deep copy to ensure we aren't mutating the original data + return JSON.parse(JSON.stringify(initData)) + } + + const part1 = () => { + const data = resetInput() + return calcPowerConsumption( + getGamma(data), + getEpsilon(data) + ) + } + + const part2 = () => { + const data = resetInput() + return calcLifeSupport( + getO2(data), + getCO2(data) + ) + } + const answers = [] + answers.push(part1()) + answers.push(part2()) + + answers.forEach((ans, idx) => { + console.info(`-- Part ${idx + 1} --`) + console.info(`Answer: ${ans}`) + }) +}) diff --git a/index.js b/index.js index fe32819..b752549 100644 --- a/index.js +++ b/index.js @@ -1 +1 @@ -require('./2021/day-02/solution') +require('./2021/day-03/solution')