mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-21 08:42:53 +02:00
Merge pull request #3778 from s2ks/hamming
CODINGCONTRACT: HammingCodes description and implementation fixes
This commit is contained in:
@@ -2,7 +2,7 @@ import { getRandomInt } from "../utils/helpers/getRandomInt";
|
||||
import { MinHeap } from "../utils/Heap";
|
||||
|
||||
import { comprGenChar, comprLZGenerate, comprLZEncode, comprLZDecode } from "../utils/CompressionContracts";
|
||||
import { HammingEncode, HammingDecode } from "../utils/HammingCodeTools";
|
||||
import { HammingEncode, HammingDecode, HammingEncodeProperly } from "../utils/HammingCodeTools";
|
||||
/* tslint:disable:completed-docs no-magic-numbers arrow-return-shorthand */
|
||||
|
||||
/* Function that generates a valid 'data' for a contract type */
|
||||
@@ -1289,16 +1289,17 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
|
||||
return [
|
||||
"You are given the following decimal Value: \n",
|
||||
`${n} \n`,
|
||||
"Convert it into a binary string and encode it as a 'Hamming-Code'. eg:\n ",
|
||||
"Value 8 will result into binary '1000', which will be encoded",
|
||||
"with the pattern 'pppdpddd', where p is a paritybit and d a databit,\n",
|
||||
"or '10101' (Value 21) will result into (pppdpdddpd) '1001101011'.\n\n",
|
||||
"NOTE: You need an parity Bit on Index 0 as an 'overall'-paritybit. \n",
|
||||
"NOTE 2: You should watch the HammingCode-video from 3Blue1Brown, which explains the 'rule' of encoding,",
|
||||
"including the first Index parity-bit mentioned on the first note.\n\n",
|
||||
"Now the only one rule for this encoding:\n",
|
||||
" It's not allowed to add additional leading '0's to the binary value\n",
|
||||
"That means, the binary value has to be encoded as it is",
|
||||
"Convert it to a binary representation and encode it as an 'extended Hamming code'. Eg:\n ",
|
||||
"Value 8 is expressed in binary as '1000', which will be encoded",
|
||||
"with the pattern 'pppdpddd', where p is a parity bit and d a data bit,\n",
|
||||
"or '10101' (Value 21) will result into (pppdpdddpd) '1001101011'.\n",
|
||||
"The answer should be given as a string containing only 1s and 0s.\n",
|
||||
"NOTE: the endianness of the data bits is reversed in relation to the endianness of the parity bits.\n",
|
||||
"NOTE: The bit at index zero is the overall parity bit, this should be set last.\n",
|
||||
"NOTE 2: You should watch the Hamming Code video from 3Blue1Brown, which explains the 'rule' of encoding,",
|
||||
"including the first index parity bit mentioned in the previous note.\n\n",
|
||||
"Extra rule for encoding:\n",
|
||||
"There should be no leading zeros in the 'data bit' section",
|
||||
].join(" ");
|
||||
},
|
||||
gen: (): number => {
|
||||
@@ -1316,19 +1317,21 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
|
||||
desc: (n: unknown): string => {
|
||||
return [
|
||||
"You are given the following encoded binary string: \n",
|
||||
`'${n}' \n`,
|
||||
"The string is a Hamming code with 1 'possible' error on a random index.\n",
|
||||
"If there is an error, find the bit that is an error and fix it.\n",
|
||||
"Extract the encoded decimal value and return a string with that value.\n\n",
|
||||
"NOTE: The length of the binary string is dynamic.\n",
|
||||
"NOTE 2: Index 0 is an 'overall' parity bit. Watch the Hamming code video from 3Blue1Brown for more information.\n",
|
||||
"NOTE 3: There's approximately a 55% chance for an altered bit. So... MAYBE there is an altered bit 😉\n",
|
||||
"NOTE 4: Return the decimal value as a string.",
|
||||
`'${n}' \n\n`,
|
||||
"Treat it as an extended Hamming code with 1 'possible' error at a random index.\n",
|
||||
"Find the 'possible' wrong bit, fix it and extract the decimal value, which is hidden inside the string.\n\n",
|
||||
"Note: The length of the binary string is dynamic, but it's encoding/decoding follows Hamming's 'rule'\n",
|
||||
"Note 2: Index 0 is an 'overall' parity bit. Watch the Hamming code video from 3Blue1Brown for more information\n",
|
||||
"Note 3: There's a ~55% chance for an altered Bit. So... MAYBE there is an altered Bit 😉\n",
|
||||
"Note: The endianness of the encoded decimal value is reversed in relation to the endianness of the Hamming code. Where",
|
||||
"the Hamming code is expressed as little-endian (LSB at index 0), the decimal value encoded in it is expressed as big-endian",
|
||||
"(MSB at index 0).\n",
|
||||
"Extra note for automation: return the decimal value as a string",
|
||||
].join(" ");
|
||||
},
|
||||
gen: (): string => {
|
||||
const _alteredBit = Math.round(Math.random());
|
||||
const _buildArray: Array<string> = HammingEncode(
|
||||
const _buildArray: Array<string> = HammingEncodeProperly(
|
||||
getRandomInt(Math.pow(2, 4), Math.pow(2, getRandomInt(1, 57))),
|
||||
).split("");
|
||||
if (_alteredBit) {
|
||||
|
||||
Reference in New Issue
Block a user