Skip to content

Instantly share code, notes, and snippets.

@Mellen
Created December 6, 2021 17:06
Show Gist options
  • Save Mellen/d25306e016b2afae8451ff9b06a11ada to your computer and use it in GitHub Desktop.
Save Mellen/d25306e016b2afae8451ff9b06a11ada to your computer and use it in GitHub Desktop.
(function(inp)
{
let oxbits = '';
let co2bits = '';
const width = 12;
let lineinp = inp.replaceAll('\n', '');
let lines = inp.split('\n');
for(let x = 0; x < width; x++)
{
let ones = 0;
let zeros = 0;
for(let index = x; index < lineinp.length; index += width)
{
if(lineinp[index] == '1')
{
ones++;
}
else
{
zeros++;
}
}
if(ones >= zeros)
{
oxbits += '1';
}
else
{
oxbits += '0';
}
lines = lines.filter(line => line.startsWith(oxbits));
if(lines.length == 1)
{
oxbits = lines[0];
break;
}
lineinp = lines.join('');
}
lineinp = inp.replaceAll('\n', '');
lines = inp.split('\n');
for(let x = 0; x < width; x++)
{
let ones = 0;
let zeros = 0;
for(let index = x; index < lineinp.length; index += width)
{
if(lineinp[index] == '1')
{
ones++;
}
else if(lineinp[index] == '0')
{
zeros++;
}
}
if(zeros > ones)
{
co2bits += '1';
}
else
{
co2bits += '0';
}
lines = lines.filter(line => line.startsWith(co2bits));
if(lines.length == 1)
{
co2bits = lines[0];
break;
}
lineinp = lines.join('');
}
let ox = parseInt(oxbits, 2);
let co2 = parseInt(co2bits, 2);
return ox*co2;
})(document.querySelector('pre').innerHTML);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment