Skip to content

feat(2021-day-03) #201

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions 2021/day-03/engineDiagnostics.js
Original file line number Diff line number Diff line change
@@ -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
}
73 changes: 73 additions & 0 deletions 2021/day-03/engineDiagnostics.test.js
Original file line number Diff line number Diff line change
@@ -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)
})
})
})
})
3 changes: 3 additions & 0 deletions 2021/day-03/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// eslint-disable-next-line no-unused-vars
const console = require('../helpers')
require('./solution')
Loading