Beaver Code Decryption
Credit
This challenge is taken with permission from https://www.mysterytwisterc3.org/en/challenges/level-1/beaver-code
Description
The encryption method is as follows:
The plaintext is divided into two halves, odd positions and even positions.
Example: 'CRYPTO' -> ['CYT','RPO']
This is then applied recursively to both halves, until each part is two letters or less. The parts are then merged.
'CRYPTO' -> ['CYT','RPO'] -> [['CT','Y'], ['RO','P']] -> 'CTYROP'
Challenge
Given a string encrypted with this method, decrypt it.
Valid input formats: ['CTYROP'], 'CTYROP'
Test cases
CTYROP -> CRYPTO
EOYCTNNPRI -> ENCRYPTION
RUOENFSEAFMRDHT -> RANDOMSTUFFHERE
Note: the encryption of the last test case is the same as the decryption, make sure you're decrypting!
BQN, 24 bytesSBCS ``` {g⊔⁼𝕊¨ …
3y ago
Solutions by ngn https://ch …
3y ago
[Jelly], 9 bytes ŒHß¹Ḋ? …
3y ago
APL(Dyalog Unicode), 34 29 byt …
3y ago
APL(Dyalog Extended), 32 bytes …
3y ago
[Husk], 8 bytes ΣTm?I₀ε …
3y ago
6 answers
Solutions by ngn
https://chat.stackexchange.com/transcript/message/57690032#57690032
APL 17: {⍵[⍋⍋⍉⊖2⊥⍣¯1⍳≢⍵]}
ngn/k 13: {x@<<+|2\!#x}
These are too good not to post.
BQN, 24 bytesSBCS
{g⊔⁼𝕊¨⍟(1<≠)𝕩⊔˜∧g←2|↕≠𝕩}
{g⊔⁼𝕊¨⍟(1<≠)𝕩⊔˜∧g←2|↕≠𝕩}
{ } # Block function 𝕊 with argument 𝕩
≠𝕩 # Length of 𝕩
↕ # Range: 0,1,…,n
g←2| # Mod 2: 0,1,0,…
∧ # Sort: 0,0,…,1,1,…
𝕩⊔˜ # Group 𝕩 by the above, splitting it in two
⍟(1<≠) # If 1 is less than the length...
𝕊¨ # ...call this function on each element
g⊔⁼ # Ungroup as if grouped by 0,1,0,1,…
BQN doesn't directly offer a "zip" function for interleaving. However, its Undo (⁼
) modifier can combine with Group to reverse an uninterleaving. ⊔⁼
isn't required by the spec and dzaima/BQN doesn't support it; another 24-byte solution is {(⍋∘⍋⊏·∾○𝕊´𝕩⊔˜∧)2|↕≠𝕩}⎊⊢
.
Using Undo means that the decoding program is similar to the encoding program given in the online link. The reason why decoding uses both normal and undone Group while encoding uses it only once is that for encoding the groups do not need to be mixed together, so Join (∾
) can be used instead.
0 comment threads
Jelly, 9 bytes
ŒHß¹Ḋ?€ZẎ
How it works
ŒHß¹Ḋ?€ZẎ - Main link f(S). Takes a string S on the left
ŒH - Split S into two halves
€ - Over each half H:
? - If:
Ḋ - Condition: H has more than 1 element
ß - Then: yield f(H)
¹ - Else: yield H
Z - Transpose the pair
Ẏ - Dump inner lists
0 comment threads
APL(Dyalog Unicode), 34 29 bytes SBCS
Saved 5 bytes thanks to Razetime!
{2>s←⌈2÷⍨≢⍵:⍵⋄,⍉↑∇¨s(↑,⍥⊂↓)⍵}
{2>s←⌈2÷⍨≢⍵:⍵⋄,⍉↑∇¨s(↑,⍥⊂↓)⍵}
{ } ⍝ Define a dfn taking argument ⍵
s→ ⍝ s will be the size of one half of ⍵
≢⍵ ⍝ Length of ⍵
2÷⍨ ⍝ Divided by two
⌈ ⍝ Rounded up
2> ⍝ If s is 1 (⍵ has 1 or 2 elements):
:⍵ ⍝ Just return ⍵
⋄ ⍝ Otherwise
s(↑ ↓)⍵ ⍝ Train with first s elements of ⍵ on left
⍝ And second half of ⍵ on right
⍥ ⍝ For both halves
⊂ ⍝ Box them
, ⍝ Join into vector
∇¨ ⍝ Run this function again on each half
↑ ⍝ Turn into a character matrix
⍉ ⍝ Transpose
, ⍝ Concatenate to interleave them together
Husk, 8 bytes
ΣTm?I₀ε½
It's jelly but reversed. 1 byte lesser due to a single byte halve.
0 comment threads
APL(Dyalog Extended), 32 bytes SBCS
{1=≢⍵:⍵⋄0~⍨,⍉↑∇¨↓2 ¯1⍴⍵,0/⍨2|≢⍵}
A dfn submission which takes a string as input.
I thought this would be much shorter, but it's only a small improvement over unicode.
1 comment thread