Skip to content

Commit e7aa119

Browse files
feat(2021-day-03): calculate oxygen/carbon dioxide values for life support
Solves part 2
1 parent b5743d0 commit e7aa119

File tree

3 files changed

+59
-7
lines changed

3 files changed

+59
-7
lines changed

2021/day-03/engineDiagnostics.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ const getMostCommon = (data, position) => {
77
const offs = data.filter((reading) => {
88
return reading[position] === '0'
99
}).length
10-
// there can only be 2 values, so easy to check which has more as being 50% or above
11-
return (offs >= data.length / 2) ? '0' : '1'
10+
// there can only be 2 values, so easy to check which has more as being above 50%
11+
return (offs > data.length / 2) ? '0' : '1'
1212
}
1313

1414
/**
@@ -40,6 +40,36 @@ const getEpsilon = (data) => {
4040
return epsilon
4141
}
4242

43+
const getO2 = (data) => {
44+
return getAir(data, getMostCommon)
45+
}
46+
47+
const getCO2 = (data) => {
48+
return getAir(data, getLeastCommon)
49+
}
50+
51+
const getAir = (data, filterMethod) => {
52+
let dataset = data
53+
// Loop through each digit, find the most common bit for that digit, and filter
54+
// out any readings that don't share that digit
55+
//
56+
// TODO: Probably faster with bitmap math, but .... ehh... runs fast enough
57+
for (let x = 0; x < data[0].length; x++) {
58+
if (dataset.length > 1) {
59+
const bit = filterMethod(dataset, x)
60+
dataset = dataset.filter((reading) => {
61+
return reading[x] === bit
62+
})
63+
}
64+
}
65+
66+
if (dataset.length > 1) {
67+
throw new Error(`Found too many results ${dataset}`)
68+
}
69+
70+
return dataset[0]
71+
}
72+
4373
const calcPowerConsumption = (gamma, epsilon) => {
4474
return parseInt(gamma, 2) * parseInt(epsilon, 2)
4575
}
@@ -49,5 +79,8 @@ module.exports = {
4979
getEpsilon,
5080
getMostCommon,
5181
getLeastCommon,
52-
calcPowerConsumption
82+
getO2,
83+
getCO2,
84+
calcPowerConsumption,
85+
calcLifeSupport: calcPowerConsumption
5386
}

2021/day-03/engineDiagnostics.test.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-env mocha */
22
const { expect } = require('chai')
3-
const { getMostCommon, getLeastCommon, getEpsilon, getGamma, calcPowerConsumption } = require('./engineDiagnostics')
3+
const { getMostCommon, getLeastCommon, getEpsilon, getGamma, getO2, getCO2, calcPowerConsumption, calcLifeSupport } = require('./engineDiagnostics')
44

55
const testData = [
66
'00100',
@@ -53,4 +53,21 @@ describe('--- Day 3: Binary Diagnostic ---', () => {
5353
})
5454
})
5555
})
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+
})
5673
})

2021/day-03/solution.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const fs = require('fs')
22
const path = require('path')
33
const filePath = path.join(__dirname, 'input.txt')
44
const { inputToArray } = require('../../2018/inputParser')
5-
const { getGamma, getEpsilon, calcPowerConsumption } = require('./engineDiagnostics')
5+
const { getGamma, getEpsilon, calcPowerConsumption, calcLifeSupport, getO2, getCO2 } = require('./engineDiagnostics')
66

77
fs.readFile(filePath, { encoding: 'utf8' }, (err, initData) => {
88
if (err) throw err
@@ -24,8 +24,10 @@ fs.readFile(filePath, { encoding: 'utf8' }, (err, initData) => {
2424

2525
const part2 = () => {
2626
const data = resetInput()
27-
console.debug(data)
28-
return 'No answer yet'
27+
return calcLifeSupport(
28+
getO2(data),
29+
getCO2(data)
30+
)
2931
}
3032
const answers = []
3133
answers.push(part1())

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy