ユークリッドアルゴリズムを再度可視化する


10

仕事

2つの正の整数を与える:

  1. 2つの整数で指定された寸法で長方形を描画します。
  2. スペースがなくなるまでステップ3を繰り返します。
  3. (残りの)長方形の3つの辺に接する最大の正方形を描画して塗りつぶします。
  4. 結果の長方形を出力します。

たとえば、入力は6and 10です。

サイズ6 x 10の中空の長方形を描画します。

xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx

繰り返し正方形を埋めた後、これは私たちが得るものです:

aaaaaabbbb
aaaaaabbbb
aaaaaabbbb
aaaaaabbbb
aaaaaaccdd
aaaaaaccdd

4つの正方形はここにある(abcd)、辺の長さとそれぞれ6422それぞれ。

ルールと自由

  1. 正方形ごとに異なる文字を使用する必要があります。
  2. サポートされる文字がすべて印刷可能な文字であり、少なくともサポートされる文字がある限り、サポートする文字を選択できます10
  3. 上記のステップ3の各反復では、2つの選択肢があります(最後の反復では選択肢が1つしかない場合を除く)。どちらの選択も有効です。
  4. 必要な正方形の数は、サポートする文字の数を超えることはありません。
  5. 正方形には、サポートする文字を任意の順序で入力できます。

テストケース

入力: 6, 10

出力:

aaaaaabbbb
aaaaaabbbb
aaaaaabbbb
aaaaaabbbb
aaaaaaccdd
aaaaaaccdd

または

aaaaaaccdd
aaaaaaccdd
aaaaaabbbb
aaaaaabbbb
aaaaaabbbb
aaaaaabbbb

または

bbbbaaaaaa
bbbbaaaaaa
bbbbaaaaaa
bbbbaaaaaa
ccddaaaaaa
ccddaaaaaa

または

ccddaaaaaa
ccddaaaaaa
bbbbaaaaaa
bbbbaaaaaa
bbbbaaaaaa
bbbbaaaaaa

または

ddddddaaaa
ddddddaaaa
ddddddaaaa
ddddddaaaa
ddddddbbcc
ddddddbbcc

入力: 1,1

出力:

a

入力: 1,10

出力:

abcdefghij

入力: 10,1

出力:

a
b
c
d
e
f
g
h
i
j

上記のテストケースに含めることができるよりも多くの可能性があることに注意してください。

得点

これはです。バイト単位の最短回答が優先されます。

標準の抜け穴が適用されます。


回答:


3

チャコール、30バイト

NδNγFβ¿×γδ«UOγδι¿‹γδA⁻δγδA⁻γδγ

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

Nδ      Input d
Nγ      Input g
Fβ      For i In ['a' ... 'z']
 ¿×γδ«   If g * d
  UOγδι   Oblong g, d, i
  ¿‹γδ    If g < d
   A⁻δγδ   d = d - g
   A⁻γδγ   Else g = g - d

厄介なことに、CharcoalのOblongコマンドは0、4バイトのコストがかかる次元には対応していません。もう1つの方法はwhileループですがg * d、反復処理の方法b(小文字に事前定義されています)を見つけることができませんでした。


おっと、申し訳ありませんが、それは意識的な設計上の決定でした。否定的な入力も許可されるべきだと思いますか?
ASCIIのみ

@ASCIIのみ現在の動作は何ですか(0と負の両方)?私の最高のアイデアは、ネガが右/下ではなく左/上に描画することです。(また、を使用する場合W×γδ、毎回別の文字を印刷するにはどうすればよいですか?)
Neil

@ニールすごい、私はあなたが何を意味するかわかります。
マジックタコの壷



1

ゼリー、32バイト

Ṁ,ạ/y
³,⁴ÇÐĿp/€Fs2
pµ¢ṣLµ€+95ỌsY

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

Ṁ,ạ/y説明が欲しい?ここにあります。

Ṁ,ạ/y          - perform one step of the Euclidean Algorithm, input 2-element list
 ,             - pair of the following two:
Ṁ              -  maximum of the the input list
  ạ/           -  absolute difference of the two elements
    y          - use this as a mapping on the input.

³,⁴ÇÐĿp/€Fs2   - apply Euclidean Algorithm
³,⁴            - start with the pair [input 1, input 2]
   Ç           - apply a step of the Euclidean Algorithm
    ÐĿ         - repetitively until the results repeat
      p/€      - take the Cartesian product of each step
         Fs2   - flatten and split into all coordinate pairs of letters

pµ¢ṣLµ€+95ỌsY
p              - Cartesian product of inputs: provides all possible coordinate pairs.
 µ   µ€       - for each coordinate
   ṣL         - find the number of times it is included in
  ¢           - the above list of covered coordinates.
       +95Ọ   - convert number of times to letters
           s  - split into rows
            Y - join by newlines.

の代わりに暗黙の引数を使用することで、もう少しゴルフをすることができ³,⁴ます。


1

Haskell、181バイト

import Data.List
(['!'..'~']&)
a#[]=a
a#b=zipWith(++)a$transpose b
(s&a)b|b<1=[]|b>a=transpose$s&b$a|n<-div a b,(t,u)<-splitAt n s=foldl1(#)((<$[1..b]).(<$[1..b])<$>t)#(u&b$mod a b)

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

以下のために10バイトより多くのあなたの代わりに素敵なスパイラルを得る:)

!!!!!!!!!!!!!$$$#####
!!!!!!!!!!!!!$$$#####
!!!!!!!!!!!!!$$$#####
!!!!!!!!!!!!!%%'#####
!!!!!!!!!!!!!%%&#####
!!!!!!!!!!!!!""""""""
!!!!!!!!!!!!!""""""""
!!!!!!!!!!!!!""""""""
!!!!!!!!!!!!!""""""""
!!!!!!!!!!!!!""""""""
!!!!!!!!!!!!!""""""""
!!!!!!!!!!!!!""""""""
!!!!!!!!!!!!!""""""""

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

未ゴルフ

(#)オペレータプットお互いに次の二つの行列が、右のいずれかを転置、例えば:

!!!                !!!"
!!! # "#$    ->    !!!#
!!!                !!!$

a # [] = a
a # b  = zipWith (++) a $ transpose b

これは基本的にユークリッドのアルゴリズムの再帰的なバージョンですが、除数と剰余を忘れてを返す代わりにgcd、そこから正方形を構築し、これらをで蓄積し(#)ます。s変数は、私たちが使用できる残りの文字です。

(s & a) b
  | b == 0 = []                     -- Base case
  | b > a = transpose $ (s & b) a   -- In this case we can just flip the arguments and rotate the result by 90 degrees
  | n <- div a b                    -- set n to the number of squares we need
  , (t,u) <- splitAt n s =          -- take n characters, ..
               ((<$[1..b]).(<$[1..b]) <$> t)                     -- .. build squares from them and ..
    foldl1 (#)                                                   -- put them next to each other
                                             (u & b $ mod a b)   -- recursively build the smaller squares with the remaining characters..
                                            #                    -- .. flip them and put them next to the previous one(s)

実際の関数は、すべての印刷可能な文字の文字列で上から関数を呼び出すだけです。

(['!'..'~']&)

import Data.List使用するには数える必要がありますtranspose
Anders Kaseorg 2017

私はそうしましたが、(私の知る限り)ポイントフリー関数を使用するときにそのインポートを行うことはできません。しかし、私はバイト数、バイト数は、実際にここで、TIOを参照してくださいにそれを含め164...
ბიმო

1
ああ。奇抜なプリプロセッサゲームをプレイすることもできますが、TIOからコピーした後、投稿のコードを手動で編集するほうが合理的な場合もあります。
Anders Kaseorg 2017
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.