How the encoding works
Given a list of bits:
- Hold a prime (starting with
- Have a list
- For each bit in the input
- If it's the same as the previous bit, add the prime you're holding to the list
- If it's different, hold the next prime and add that to the list
- Return the product of all the numbers in your list
- For the first bit, assume the previous bit was
Note: these steps are for illustration purposes only, you are not required to follow them.
Input: 001 hold 2 0: add 2 to the list 0: add 2 to the list 1: hold 3, add 3 to the list list: 2,2,3 Output: 12
Input: 1101 hold 2 1: hold 3, add 3 to the list 1: add 3 to the list 0: hold 5, add 5 to the list 1: hold 7, add 7 to the list list: 3,3,5,7 Output: 315
Some more examples:
000000000 -> 512 111111111 -> 19683 010101010 -> 223092870 101010101 -> 3234846615 011101101 -> 1891890 000101101010010000 -> 3847834029582062520
Write an encoder and a decoder for this encoding method.
(The decoder reverses the process of the encoder).
Input / Output
The encoder can take input in any reasonable format
The encoder must output either an integer or a string
The decoder must take input in the same format that the encoder utputs
The decoder must output the same format that the encoder takes as input
In other words
decoder( encoder( input ) ) === input
- The decoder may assume that its input is decodable
- Your answer only has to deal with integers that your language can natively support without using (
bigInt, etc.), be reasonable, if you language only supports ints up to 1, maybe reconsider posting an answer
Your score is the sum of the lengths in bytes of the encoder and decoder.
If you need to import a module, the import can be counted only once provided that your encoder and decoder can coexist in the same file and be reused (like functions).
Default loopholes are forbidden.
This is code-golf so the shortest score for every language wins.