Skip to content

Instantly share code, notes, and snippets.

@FOLLGAD
Last active May 15, 2022 10:35
Show Gist options
  • Select an option

  • Save FOLLGAD/9fe55cd8995e1e32187833850069cb33 to your computer and use it in GitHub Desktop.

Select an option

Save FOLLGAD/9fe55cd8995e1e32187833850069cb33 to your computer and use it in GitHub Desktop.
Bitburner hamming code solver
// translates dynamic-length hamming codes into raw data
function hamming(n) {
const s = n.length
let o = ""
let errors = []
for (let i = 1; i < s; i++) {
const pw2 = !( i & (i - 1) ) // is power of two
if (!pw2) {
o += n[i]
} else {
let sum = 0
for (let a = i; a < s; a++) {
if (a & i) {
sum += n[a] === "1" ? 1 : 0;
}
}
if (sum %2 !== parseInt(n[i])) {
console.log("ERROR at ", i)
errors.push(i)
}
}
}
if (errors.length) {
const errorPos = errors.reduce((a,b) => a | b)
n[errorPos] = n[errorPos] === "1" ? "0" : "1";
let realout = ""
for (let i = 1; i < s; i++) {
if (i & (i - 1)) {
realout += n[i]
}
}
console.log("Fixed error at", errorPos)
return realout
}
return o
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment