Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Sonic 1 + Sonic & Knuckles/Blue Spheres - Code Validator
//Sonic 1 + Sonic & Knuckles/Blue Spheres - Code Validator print(codtolev(readline())); // helper function function div(a, b) { return Math.floor(a / b); } // this function returns the level number (1 - 134217728) for a given level password, or "Invalid code" if the password is invalid function codtolev(cod) { // cod is a password string in the format "1234 1234 1234" var split = parseInt(cod.substring(0, 4) + cod.substring(5, 9) + cod.substring(10)); var cx = new Array(39); var cz = new Array(28); // create array of cod's binary digits for (i = 0; i < 39; i++) { cx[i] = split % 2; split = div(split, 2); } // invert the bits at odd indices for (i = 1; i < 39; i += 2) { cx[i] = 1 - cx[i]; } // create array from the bits of cx in a certain order cz[27] = 0; for (i = 0; i < 6; i++) { cz[i] = cx[i + 26]; } for (i = 6; i < 27; i++) { cz[i] = cx[i - 6]; } // convert the binary number represented by cz to decimal var sum = 0; for (i = 26; i > -1; i--) { sum += (cz[i] * Math.pow(2, i)); } // subtract a constant, then fiddle with the number a bit sum -= 19088742; if (sum < 1 && sum > -19088743) { sum = 134217728 + sum; } // sum should now be the level number; now we need to make sure the rest of the bit pattern is valid var tmp = sum - 1; // is this bit is 0, the code was generated with the "not sonic 1 cart" flag, so to validate we carry out the extra operation that entails if (cx[38] == 0) { tmp += 0x07654321; tmp &= 0x07FFFFFF; } // create array of tmp's binary digits for (i = 0; i <= 27; i++) { cz[i] = tmp % 2; tmp = div(tmp, 2); } // validate the bit pattern if (cx[37] == (1 + cz[6] + cz[23]) % 2 && cx[36] == (1 + cz[5] + cz[22] + cz[17] + cz[0]) % 2 && cx[35] == (1 + cz[4] + cz[21] + cz[16]) % 2 && cx[34] == (1 + cz[3] + cz[20]) % 2 && cx[33] == (1 + cz[2] + cz[19]) % 2 && cx[32] == (1 + cz[1] + cz[18]) % 2 && cx[25] == (0 + cz[11] + cz[16]) % 2 && cx[24] == (0 + cz[10] ) % 2 && cx[23] == (0 + cz[9] + cz[26]) % 2 && cx[22] == (0 + cz[8] + cz[25]) % 2 && cx[21] == (0 + cz[7] + cz[24]) % 2) { // in game, the "sonic 1 cart" flag should be set here according to the value of cx[38] return sum; } else { return "Invalid code"; } }
run
|
edit
|
history
|
help
0
Nth Fibonacci Number
Javascript Basics
random3
tt
Triangle
123141242342
ECMAScript5 - js
Functions
#include
class Node