Created
December 3, 2021 12:09
-
-
Save chidiwilliams/53f59ec7f88f0c70ad744f16c2a4b288 to your computer and use it in GitHub Desktop.
binary-diagnostic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getProductOfOxyAndCo2(list) { | |
const len = list[0].length; | |
let oxy = [...list]; | |
let co2 = [...list]; | |
while (true) { | |
let oxyDone = false; | |
let co2Done = false; | |
for (let i = 0; i < len && (!oxyDone || !co2Done); i++) { | |
if (!oxyDone) { | |
let numZeroes = 0; | |
let numOnes = 0; | |
for (let j = 0; j < oxy.length; j++) { | |
if (oxy[j][i] === '0') { | |
numZeroes++; | |
} else { | |
numOnes++; | |
} | |
} | |
const mcb = +(numOnes >= numZeroes); | |
oxy = oxy.filter((item) => item[i] === String(mcb)); | |
if (oxy.length === 1) { | |
oxyDone = true; | |
} | |
} | |
if (!co2Done) { | |
let numZeroes = 0; | |
let numOnes = 0; | |
for (let j = 0; j < co2.length; j++) { | |
if (co2[j][i] === '0') { | |
numZeroes++; | |
} else { | |
numOnes++; | |
} | |
} | |
const lcb = +(numZeroes > numOnes); | |
co2 = co2.filter((item) => item[i] === String(lcb)); | |
if (co2.length === 1) { | |
co2Done = true; | |
} | |
} | |
} | |
if (oxyDone && co2Done) { | |
break; | |
} | |
} | |
return parseInt(oxy[0], 2) * parseInt(co2[0], 2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment