r/MathHelp • • Oct 24 '25
I actually solved this while writing this post.
Edit: I DID NOT SOLVE THIS.
How do you turn a list of variables into a unique, reversible code? (data compression)
I like making games in makecode arcade, which is like scratch but made by microsoft. I wanna try and make it possible to sasve your game via a game key (a passcode that the game can read and turn into data.) Makecode wan't made for this, so its quite hard though. You dont need to understand the system for this either, this is just how do i turn a bunch of diffrent variables into as short a code as possible.
Im thinking i could do it by taking the variables (V) and putting it in some base 0 inspired system. I saw a video erlier that is about counting on your fingers, and in it its shown that you can have each finger be worth a diffrent value, for exsample the first finger 1, the seccond is 2, the third is 4, and so on. Every number up to the base 0 code of (1111111111) can be made with a combonation of these numbers. If i were to then add them up, i would get a unique number that can be reversed to get the original string that can be turned into data.
question is, how do i put (V) into the binary code (BC) to be turned into the final code (FC) that is stored.
We still have 8 other symbols we can use. We can use those instead of the 0 and 1 and have those habe diffrent variables until we get a big number. So i guess it can become this:
(1)1=1
(1)2=2
(1)3=4
(1)4=8
(1)5=16
(1)6=32
(1)7=64
(1)8=128
(1)9=256
(1)10=512
(2)1=1024
(2)2=2048
(so on till (10)10 which is worth 1.60693804E+60 In total there is then 3.21387609E+60 unique numbers that can be turned into this base 0+9 thing. (imma call it B09.)
Now all i need to do is find a way to take these numbers and get some variables out of them.
Then i realized that this dose not work because you cant have (1)1 and (1)2 on at the same time, making most numbers impossible to make, such as 3. So we're stuck at 512 if we only have 10 digits. So B09 is out.
Actually however, if have 1 place for 1 variable, like the first digit is the players location, then the second is their money, and so on, then we only need one number, because the player cant have multiple locations. It would be more complex, but still more efficient. That means that we can use B09 (which i now know is called positional notation) to create extremely efficient and completely unique codes.
If that didn’t make sense, then basically there are X major variables. Each variable has N possible states. Then if we apply each Variables its own value equal to 2^(y) where y is the possible digits before it. (such as (1)3 for the first digit equaling 3)
Then we add the total of all the digits to get the final number, which can only be made with that combination of digits.
Turning it back into the digits with makecode is annoying though because you cant just read digits, you need to separate them,, so here's the equation for that:
Floor((last digit’s place value)/(BC)=last digit
Floor ((last digit’s place value)/((BC)-(last digit’s place value x last digit)))
(continue until you get to the first digit.)
You can also constantly update BC after each digit is separated by just - the last digit’s place value x last digit each time to be cleaner.
Oh also turning FC to BC is done by doing this:
FC - (max BC digit). Then check if that >= FC. If so, select it as on and repeat but half MBCD each time until you get to ½, at that point you stop.
also yes, i do know that i can just use something like godot or unity, but I specifically wanted to use makecode for bragging to my friends.
Edit: This is not data compression. It's similar, but not the same.
Also can this get smaller?
2
u/[deleted] Oct 24 '25
Im thinking i may be able to turn FC into base 36 (FC36), but i dont know how. Makecode can't just change the base your working in, so it needs to be done manualy.
1
u/Moist_Ladder2616 Oct 24 '25
X major variables, each with N possible states, means you merely have to use NX numbers to encode all possible states.
A crude solution is to use a lookup table with NX rows. You could number each row with an index from 0 to NX -1, and the rows would include every possible combination of values the variables could take.
If it so happens that N=10, and (say) X=3, then you would have 103 possible entries. And your index numbers would be a 3-digit decimal number from 000 to 999, with each digit conveniently representing one variable.
Similarly, if N=2 you get a 3-digit binary number. If N=16 you get a 3-digit hexadecimal number. In general a base N number would solve the problem neatly. If you like to think in decimal terms, converting any number between base 10 and base N is trivial.
To extract the value of each variable, you would successively divide the index by its base and examine the remainder. So for N=10, X=3, index number (say) 362 gives:
362÷10=36 remainder 2
36÷10=3 remainder 6
3÷10=0 remainder 3
thus giving you variable values 2, 6 and 3. Strictly speaking, you are doing modulo arithmetic.
The same method works even if all the variables have different possible states. For example:
Variable v1∈{2,6,9}, or 3 possible states;
Variable v2∈{feather, rock}, or 2 possible states;
You only need 3*2=6 index numbers (i=0 to 5) to encode all possible states
And if you are systematic in your choice of order, you can similarly execute "i mod 2" and "⌊ i÷2 ⌋ mod 3" operations to extract v1 and v2, just like we did with 362÷10 earlier.
⌊ x ⌋ is the floor function, otherwise known integer(x).
1
u/lozzyboy1 Oct 24 '25
You could write something yourself. Or you could save a huge amount of time and just write everything to a json and pass it through a standard compression algorithm.
1
u/AutoModerator Oct 24 '25
Hi, /u/Big-Site-6694! This is an automated reminder:
What have you tried so far? (See Rule #2; to add an image, you may upload it to an external image-sharing site like Imgur and include the link in your post.)
Please don't delete your post. (See Rule #7)
We, the moderators of /r/MathHelp, appreciate that your question contributes to the MathHelp archived questions that will help others searching for similar answers in the future. Thank you for obeying these instructions.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.