言語算術のサブセット


8

一緒に追加されたいくつかの同じ数列の言語算術ソルバーを実装します。

  TWO
+ TWO
-----
 FOUR

  REPEAT
  REPEAT
  REPEAT
+ REPEAT
--------
 ANDSTOP

  SPEED
+ SPEED
-------
  KILLS

いくつかの制限があります。各文字は異なる数字を表す必要があり、文字をゼロにすることはできません。

オペランドの関数としてソルバーを実装すると、繰り返しの合計と数が解のリストを返します(ソリューション:結果のオペランドと合計のタプル)。例えば:

f(['T','W','O'], ['F','O','U','R'], 2) == [(734, 1468)]

変数を文字として表す必要はなく、ソリューションでハッシュを使用する必要もありません。ブルートフォース検索が許可されました。

最短のコードが勝ちます。


他の2つのケースのサンプルソリューションを提供できますか?
— fR0DDY 2011年

SPEED:= 29331、58662:= KILLS、コードが続き、ゴルフする必要があります。
— ユーザー不明

回答:


3

Mathematica

明確にするためにスペースが追加されました。あまりゴルフをしていません。
入力文字は記号として扱われるため、ギリシャ文字を使用する必要があります。

F[σ_ ,ρ_ ,τ_]:=
 (φ = FromDigits;
 Rest@Union[
   If [ τ * φ@σ == φ@ρ, {φ@σ,φ@ρ} ] /.#& /@
 (Thread[Rule[ σ ∪ ρ , # ] ] & /@ Permutations[Range@9, {Length[σ ∪ ρ] }])])

使用法:

F[{r,e,p,e,a,t},{a,n,d,s,t,o,p},3]
{{819123,2457369}}

F[{s,p,e,e,d},{k,i,l,l,s},3]
{}

F[{t,w,o},{f,o,u,r},2]
{{734,1468},{836,1672},{846,1692},{867,1734},{928,1856},{938,1876}}  

SPEED + SPEED + SPEED = KILLSの解決策は見つかりませんでした...それはバグですか?

編集する

ゼロを許可すると、SPEED + SPEED + SPEED = KILLS方程式の次の解が見つかります。

{{10887,32661},{12667,38001},{23554,70662},
 {23664,70992},{25334,76002},{26334,79002}}

編集する

コメントによると:

F[{s, p, e, e, d}, {k, i, l, l, s}, 2]  

{{21776,43552},{21886,43772},{23556,47112},{27331,54662},
 {29331,58662},{42667,85334},{45667,91334},{46557,93114}}

メタコメント...コードブロックでギリシャ文字を表示する方法はありますか?
— belisarius博士、2011年

編集:紙に示されているものは2つの速度しかありません
— Ming-Tang

belisarius:このトリックは、HTMLエスケープを使用しないことです。それらは直接文字を使用して文字のみを表すので禁止されていません;-)。ただし、インデントを修正する必要がある場合があります。私がその正確さを保ったかどうかはわかりません。
— ジョーイ

@Joeyエスケープされた文字はMathematicaソースでは使用されていません。ここでのレンダリングにのみ使用しました。しかし、すべてのブラウザが文字を等しくレンダリングするわけではないようです。私はあなたのコードを見て、私のものはまったく同じです:)
— Dr. belisarius

1

パイソン

def f(A,B,N):
 D={}
 r=[]
 for j in A:D[j]=0
 for j in B:D[j]=0
 x=len(D)
 for i in xrange(10**(x-1),10**x):
        c=str(i)
        s={}
        for j in c:s[j]=0
        if(len(s)-x or '0' in c):continue
        k=P=Q=0
        for j in D:D[j]=int(c[k]);k+=1
        for j in A:P=P*10+D[j]
        for j in B:Q=Q*10+D[j]
        if(P*N==Q):r.append((P,Q))
 return r
print f(['T','W','O'], ['F','O','U','R'], 2)

http://ideone.com/4wIQe


1

scala:333 289

type S=String
def d(x:S,m:Map[Char,Int])={var s=0
for(c<-x;k=m.find(_._1==c);v=(k.get)._2){s*=10
s+=v}
s}
def s(t:Int,f:S,p:S):Unit={
def c(m:Map[Char,Int])=d(f,m)*t==d(p,m)
val g=f.toSet++p
val m=g.zip(util.Random.shuffle((1 to 9).toSeq).take(g.size))
if(c(m.toMap))print(m)else s(t,f,p)}

使用法:

s (2,"SPEED","KILLS")
Set((D,7), (K,8), (I,5), (E,6), (S,4), (L,3), (P,2))

s(4,"REPEAT","ANDSTOP")
// endless loop :)

0

PHP(200)

この関数は実行に非常に長い時間がかかり、大量のメモリを使用しますが、基準を満たしています。

function f($o,$s,$n){$w=count_chars(($c=join($o)).$d=join($s),3);while(++$i<pow(10,9)){if(($u=count_chars($i,3))&&$u[0]*$u[8]&&($n*$a=strtr($c,$w,$i))==$b=strtr($d,$w,$i))$x[]=array($a,$b);}return$x;}

使用例:

$a=array('T','W','O');
$b=array('F','O','U','R');
$c=f($a, $b, 2); // returns an array of tuples that satisfy the equation

ゴルフではない説明:

function solve($operand, $sum, $num) {
  // convert the operand and sum arrays into strings, join them, then get a string containing the unique characters
  $operand_string = join($operand);
  $sum_string = join($sum);
  $unique_chars = count_chars($operand_string . $sum_string, 3);

  // loop from 1 to 10^9
  while (++$i < pow(10,9)) {
    // get the unique digits in $i
    $unique_digits = count_chars($i, 3);
    // check whether the first digit is non-zero (count_chars sorts in ascending order)
    // and whether the ninth digit is non-zero, these conditions guarantee that $i
    // is a permutation of 1...9 
    if ($u[0] * $u[8]) {
      // translate the operand and sum into numbers, then check if the operand * num = sum
      $translated_operand = strtr($operand_string, $unique_chars, $i);
      $translated_sum = strtr($sum_string, $unique_chars, $i);
      if ($num * $translated_operand == $translated_sum) {
        // add the solution to the solutions array
        $solutions[] = array($translated_operand, $translated_sum);
      }
    }
  }
  // return the solutions array
  return $solutions;
}

配列の代わりにオペランドとして入力して文字列として合計することが許可されている場合、結合操作をスキップして20文字を節約し、合計を180にできます。

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