数字で暗号化!


12

問題:

2人の敵の秘密エージェントが素晴らしい(あなたのための)通信方法を考案しました!

暗号化プロセスの仕組みは次のとおりです。

1)各文字のASCII相当物を取ります。(スペース、数字、または句読点は送信されません)

2)メッセージ内の各文字について、それと同等のASCII文字とその後の文字(存在する場合、存在しない場合は0と見なされる)、乗算されます(この製品は配列/リストに格納されます)合計します(この番号は別のリストにも保存されます)。

3)2つのリスト(合計と製品)が結合され(合計リスト、次に倍数リスト、同じ配列に)、送信されます。

このプロセスを逆にし、この形式で送信されたメッセージを解読できる最小のプログラムを書く必要があります!

入力と出力のペアの例:

[173, 209, 216, 219, 198, 198, 225, 222, 208, 100, 7272, 10908, 11664, 11988, 9657, 9657, 12654, 12312, 10800, 0] -> “HelloWorld”
[131, 133, 164, 195, 197, 99, 4290, 4422, 6499, 9506, 9702, 0] -> “ABCabc”

これはであるため、バイト単位の最小のソリューションが優先されます。

エラーメッセージは許可されます。

提出物で指定する場合、プログラムにはlist / 1次元配列またはカンマ区切りの文字列を指定できます。デフォルトは配列/リストです。


1
倍数のリストがそこにあるのはなぜですか?合計だけで十分な情報です。
orlp

1
@orlpはゴルフの機会を増やすためでしょうか?:)
ジョナサンアラン

1
@orlpああ、あなたは楽しみを台無しにしました!
エリックアウトゴルファー

@JonathanAllanは部分的に正しいです。私は、2つのシークレットエージェントが「コード」に不要な部分を追加するように、非常に愚かに見えるようにしたかったのです。また、出てくる可能性のあるプログラムをいくつか追加します。
-iPhoenix

@orlp倍数だけでは不十分ですよね?
ericw31415

回答:


5

ハスク7 6バイト

mcĠ≠←½

オンラインでお試しください!文書によると、先行mは必要ないはずですが、現在バグがあるようです。

編集:Zgarbのおかげで-1バイト!

説明:

     ½ -- split input list into half
    ←  -- take first list
  Ġ≠   -- subtract each list element from the previous one
mc     -- convert list of code points to string

`-できると思いますc実際の動作はバグのように見えます。
ズガルブ

@Zgarbそれは不平等を実装する実用的な方法です。これはどこかに文書化されていますか?
ライコニ

それは上だセマンティクスページ殻のWikiの。
ズガルブ

1
説明を変更したようですが、実際のコードスニペット自体は変更していないようです。:)
iPhoenix

@iPhoenixありがとう、私はそれを認めた。
ライコニ

8

brainfuck、66バイト

,[>>>+<[-<+>>-<]<[->+<],]>[<<,>>[-<+>]<-]<<<[>[-<->>+<]<<]>.>>[.>]

入力は暗号化された文字列です。無限のサイズのセルとEOFの0を想定しています。

使い方:

,[>>>+<[-<+>>-<]<[->+<],] Gets input and the number of characters divided by 2
>[<<,>>[-<+>]<-]<<< Remove the second half of the string (the multiplication part)
[>[-<->>+<]<<] Subtract each character from the previous one, while keeping a copy of the previous one.
>.>>[.>] Print the characters

5

Haskell45 35バイト

map toEnum.scanr1(-).fst.span(<245)

オンラインでお試しください!

説明

  • fst.span(<245)リストの先頭から245より小さいすべての数値を取ります。可能な最大の合計はz + z = 122 + 122 = 244であり、可能な最小の積はであるため、これらは合計部分の数値のみですA * A = 65 * 65 = 4225
  • scanr1(-)リストの最後の値を取得し、それを初期アキュムレーターとして使用します。次に、リストの各要素が現在のアキュムレータによって減算され、結果が次のアキュムレータとして使用され、リストに追加されます。
  • map toEnum リスト内の各番号を対応する文字に置き換えて、文字列を再作成します。

3

ゼリー、9バイト

œs2ḢUạ\ỌU

オンラインでお試しください!または両方のテストケースを確認します。

代替案。

説明

œs2ḢUạ\ỌU|| 完全なプログラム。

œs2|| 2つの部分に分割し、必要に応じて最初の部分を長くします。
   Ḣ|| 頭(最初の要素)を取得します。
    U || 逆。
     ạ\ || 減算による累積削減。
       Ọ|| コードポイントから文字に変換します。
        U || そして再び逆。



2

ゼリー、11 バイト

œs2Ḣḅ-$ÐƤAỌ

整数のリストを取り、文字のリストを返す単項リンク。

オンラインでお試しください!

どうやって?

œs2Ḣḅ-$ÐƤAỌ - Link: list of integers     e.g. [210,211,201,101,10989,11100,10100,0]
  2         - literal two                     2
œs          - split into equal parts          [[210,211,201,101],[10989,11100,10100,0]]
   Ḣ        - head                            [210,211,201,101]
       ÐƤ   - for postfixes:                  [210,211,201,101],[211,201,101],[201,101],[101]
      $     -   last two links as a monad:
     -      -     literal minus one           -1
    ḅ       -     convert from base           -99              ,111          ,-100      ,101
         A  - absolute value (vectorises)     [99,111,100,101]
          Ọ - convert to ordinal (vectorises) "code"

1

Pyt、60 バイト

←ĐĐŁ₂⁻⦋⇹ĐŁřĐŁ₂>*ž0`ŕĐĐŁ⁻⦋3ȘĐ4Ș3Ș4Ș÷⇹ĐŁřĐŁ<*žĐŁ⁻łŕ⇹Đ3Ș⇹÷Á↔áƇǰ

整数のリストを取得し、文字列を返します。

説明:

←ĐĐŁ₂⁻⦋⇹          Gets the ASCII code of the last character
ĐŁřĐŁ₂>*ž         Gets the list of products and removes the 0 from the end of the list
0`ŕ ...  ł        Loops (0 is there so that the length can be removed from the stack)
ĐĐŁ⁻⦋              Gets the last product
3ȘĐ4Ș3Ș4Ș÷        Divides by the last ASCII code obtained
⇹ĐŁřĐŁ<*ž         Removes the last element from the array
ĐŁ⁻ł              Gets the length of the array - 1 (if 0, then exit loop - the last entry still has to be processed)
ŕ⇹Đ3Ș⇹÷           Divides the remaining product by the last ASCII code obtained           
Á↔á               Converts to array of ints
Ƈǰ                Converts to string of ASCII characters

オンラインでお試しください!


1

JavaScript(ES6)、80バイト

a=>String.fromCharCode(...eval(`for(a.splice(i=a.length/2);--i;a[i-1]-=a[i])a`))


1

VBスクリプト-74 71バイト

(Do..Loopの代わりにWhile..Wendを使用して、74から71に減らすことができました)

入力は配列a()にあり、出力は文字列dにあります

d="":p=0:n=(UBound(a)+1)/2:While n>0:n=n-1:t=a(n)-p:d=Chr(t)&d:p=t:Wend

説明

d=""          '// initialize the output string
p=0          '// initialize the ansii of following char (working back from last char)
n=(Ubound(a)+1)/2 '// the index of the last summed pair + 1 (base 0)
While n>0    '// begin loop working back from last summed pair
n=n-1        '// move back 1 char
t=a(n)-p     '// calculate the ansii by subtracting the ansii of following char
d=Chr(t)&d   '// prepend the char to output
p=t          '// this char becomes the following char for next
Wend         '// repeat etc.

上記のコードを関数としてラップしたvbscriptファイルでこれをテストしました。

dim s
Dim arr()
s = Split("173, 209, 216, 219, 198, 198, 225, 222, 208, 100, 7272, 10908, 11664, 11988, 9657, 9657, 12654, 12312, 10800, 0", ",")
ReDim arr(UBound(s))
Dim x 
For x = 0 To UBound(s)
    arr(x) = cint(s(x))
Next 

msgbox "=" & d(arr)



Private Function d(a())
d="":p=0:n=(UBound(a)+1)/2:While n>0:n=n-1:t=a(n)-p:d=Chr(t)&d:p=t:Wend
End Function

1

クリーン96 81 78 77バイト

zeroはヌル文字です。
Cleanがソースファイル内のリテラルNULLについてあまり気に入らない場合は、別のバイトを保存できます。

import StdEnv
f=init o foldr(\a t=[toChar a-t!!0:t])[zero]o takeWhile((>)245)

オンラインでお試しください!


匿名関数は一般に受け入れられるため、必要に応じてをドロップできますf=
ライコニ

@Laikoniこの場合、インラインで使用するには括弧が必要でf=あり、最短の割り当てであるため、この呼び出しの有効性についてはわかりません。
18年




0

Perl 6の 43の39  35バイト

{[~] [R,](produce *R-*,[R,] .[^*/2])».chr}

試して

{[~] [R,]([\[&(*R-*)]] [R,] .[^*/2])».chr}

テスト (上記と同じ)

{[~] [R,]([\R[&(*R-*)]] .[^*/2])».chr}

試して

{[R~] [\R[&(*R-*)]](.[^*/2])».chr}

試して

説明:

{[R~] [\R[&(*R-*)]](.[^*/2])».chr}

{                                }      # block lambda with parameter `$_`

      [\R[&(*R-*)]](.[^*/2])            # turn sums back into ordinals (reversed)

                    .[^*/2]             # first half of `$_` (implicit method call)
            *R-*                        # lambda, reverse of *-*
         [&(    )]                      # use it as an infix operator
                                        # (same as R- except left associative)
        R                               # reverse arguments and associativity
                                        # (same as - except right associative)
      [\          ](       )            # produce values `[\+] 1,2,3` => `(1,3,6)`
                                        # uses associativity to determine direction
                                        # `[\**] 1,2,3` => `(3,8,1)`

                            ».chr       # call `.chr` method on all values
                                        # (possibly concurrently)

 [R~]                                   # concatenate in reverse
                                        # (shorter than `join '', reverse …`)

0

05AB1E、9バイト

2ä¬Å«-}çJ

オンラインでお試しください!

説明

2ä        # Split input list in two (equal if possible) parts.
  ¬       # Push head(a).
   Å«-}   # Cumulative reduce the list by subtraction (from the right).
       ç  # Convert each integer in the list to its corresponding ASCII char.
        J # Join list together to string.

JOin は必要ありません。
シャギー

@Shaggy çは、文字のリストを暗黙的に文字列に変換しません。問題を正しく理解している場合、プログラムは文字のリストではなく文字列を出力する必要があります。
ヴィスワフ


弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.