Challenges

# Beaver Code Decryption

+6
−0

## 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!

Why does this post require moderator attention?
Why should this post be closed?

+5
−0

# BQN, 24 bytesSBCS

{g⊔⁼𝕊¨⍟(1<≠)𝕩⊔˜∧g←2|↕≠𝕩}


Run online!

{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.

Why does this post require moderator attention?

+3
−0

# Jelly, 9 bytes

ŒHß¹Ḋ?€ZẎ


Try it online!

## 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
Why does this post require moderator attention?

+3
−0

## Solutions by ngn

https://chat.stackexchange.com/transcript/message/57690032#57690032

APL 17: {⍵[⍋⍋⍉⊖2⊥⍣¯1⍳≢⍵]}

ngn/k 13: {[email protected]<<+|2\!#x}

These are too good not to post.

Why does this post require moderator attention?

These look awesome. Do you have explanations? user‭ about 2 months ago

The chat says it assumes input length is under some limit? Razetime‭ about 2 months ago

These don't assume that. The explanation is based on the fact this is a bit reversal permutation https://en.wikipedia.org/wiki/Bit-reversal_permutation rak1507‭ about 2 months ago

+2
−0

# APL(Dyalog Unicode), 3429 bytes SBCS

Saved 5 bytes thanks to Razetime!

{2>s←⌈2÷⍨≢⍵:⍵⋄,⍉↑∇¨s(↑,⍥⊂↓)⍵}


Try it on APLgolf!

{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

Why does this post require moderator attention?

#### 1 comment

Using Jelly idea: 29 bytes Razetime‭ about 2 months ago

+1
−0

# APL(Dyalog Extended), 32 bytes SBCS

{1=≢⍵:⍵⋄0~⍨,⍉↑∇¨↓2 ¯1⍴⍵,0/⍨2|≢⍵}


Try it on APLgolf!

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.

Why does this post require moderator attention?

+1
−0

# Husk, 8 bytes

ΣTm?I₀ε½


Try it online!

It's jelly but reversed. 1 byte lesser due to a single byte halve.

Why does this post require moderator attention?