増分ゲーム時間形式


18

増分ゲーム時間形式

ゴール

増分ゲームには、タスクが完了するまでの日、時間、分、秒を表すカウントダウンタイマーが含まれていることがよくあります。使用可能なスペースに応じて、次のようにフォーマットできます。

2d 13h
23h 59m 48s
14m
3h 0m 0s

このゴルフの目的は、このフォーマットを実行する関数またはプログラムを作成することです。

インプット

  • 合計秒数。
  • 出力するセグメントの最大数。

出力

  • セグメントは次のとおりです。
    • 0週間
    • 0
    • 0時間
    • 0
    • 0
  • 各セグメントは単一のスペースで区切られます。
  • 表示されるセグメントは連続している必要があります。たとえば、ゼロ分があっても、分を表示せずに時間と秒を表示しません。
  • 1桁の値には先行ゼロはありませんが、ゼロの値はとして表示する必要があります0
  • 値は切り捨てられます。
  • 表示される最初のセグメントは、最初のゼロ以外の値です。

テストケース

seconds  segments  output
     0      1      0s
   123      1      2m
   123      2      2m 3s
   123      3      2m 3s
 82815      3      23h 0m 15s
307891      2      3d 13h
307891      4      3d 13h 31m 31s
604800      1      1w
604800      6      1w 0d 0h 0m 0s

勝ち

1週間で最も少ないバイト数のソリューションが「受け入れ」になります。

編集

  • 例に示すように、どのセグメントが最初であるかを明確にしました。
  • 要求ごとにテストケース4を追加しました。

期待される出力は307891 1何ですか?0wまたは1w
jnovacho

1
@jnovachoそうではないでしょう3dか?「表示される最初のセグメントは、最初の非ゼロ値です」
ルイージ

@Luigiはい。私はそれを見逃した。
jnovacho

これは「誰かが私のためにこのコードを書いてください」という質問だと思っているのは私だけですか?
fho

実際に毎日のようにコードゴルフタスクが役立つとは限りません。私はそれで行くと言う:D
Geobits

回答:


7

CJam(スナップショット)、41 38バイト

q~"<<^X^G"{imd\}%W%"wdhms":s.+_{0=}#><S*

上記の例では、キャレット表記を使用しています。これは、2つの文字が印刷できないためです。

2バイトのゴルフをしてくれた@ Sp3000に感謝します。

テスト中

最新の安定版リリース(0.6.5)には、Longの代わりにInteger を返すバグがあり{}#ます。かなり逆説的ですが、これは整数にキャストすることで回避できます(i)。

オンラインインタープリターでコードを使用してこれを実行するには、このパーマリンクをクリックするか、この貼り付けからコードをコピーします。

または、次のコマンドを実行して、最新のスナップショットをダウンロードして構築できます。

hg clone http://hg.code.sf.net/p/cjam/code cjam-code
cd cjam-code/
ant

次のようにCJamファイルを作成できます。

base64 -d > game-time.cjam <<< cX4iPDwYByJ7aW1kXH0lVyUid2RobXMiOnMuK197MD19Iz48Uyo=

使い方

q~        e# Read an evaluate the input.
"<<^X^G"  e# Push the string corresponding to the array [60 60 24 7
{         e# For each character:
  i       e#   Replace it by its code point.
  md      e#   Push divisor and residue of the division by that code point.
  \       e#   Swap their order.
}%
W%        e# Reverse the resulting array.
"wdhms":s e# Push ["w" "d" "h" "m" "s"].
.+        e# Perform vectorized concatenation.
_         e# Push a copy.
{0=}#     e# Find the index of the first pair with positive integer.
>         e# Remove the preceding pairs.
<         e# Truncate to the number of pairs specified in the input.
S*        e# Join, separating by spaces.

6

Java、197191バイト

String p(int s,int m){String a[]={s%60+"s",(s/=60)%60+"m",(s/=60)%24+"h",(s/=24)%7+"d",(s/7)+"w"},b="",z="";for(s=5;s>0&&a[--s].charAt(0)=='0';);for(;s>=0&&--m>=0;z=" ")b+=z+a[s--];return b;}

Javaがのような宣言をサポートしていることに気づきましたString a[]。これは私が宣言引っ張って許可bし、z書き込みから私を救っ同じ行、にStringもう一度を。


1
のように;z=" ")-非常に賢い。
-OldCurmudgeon

5

C、134の 127 110 104 103バイト

新しいバージョン:

a,e=5;f(n,x){for(;e;n%=a)a=(int[]){1,60,3600,86400,604800}[--e],x>e?printf("%u%c ",n/a,"smhdw"[e]):0;}

前のバージョン:

#define p(a) x>--e?printf("%u%c ",n/a,"smhdw"[e]):0;n%=a;
e=5;f(n,x){p(604800)p(86400)p(3600)p(60)p(1)}

4

Pyth、39 43バイト

jd_>vz+V?u?GeGPG+m%~/QddCM"<<"Q)Q]0"smhdw

編集:0sテストケースを忘れたため、+ 4文字。

これには、2つの印刷できない文字が含まれます。実際のコードを取得してオンラインで試してください:デモンストレーション

説明:

                                         z = 1st input line (segments, as string)
                                         Q = 2nd input line (time, as int)
                         "<<.."          string with 4 chars
                       CM                convert to ASCCI-values => [60,60,24,7]
                m                        map each d of ^ to:
                   /Qd                     Q / d 
                  ~                        update Q, but use the old value for
                 %  Q d                    Q mod d
                                         this gives [sec, min, hour, day]
               +               Q         add Q (week)
        u                       )        apply the following expression to G, 
                                         starting with G = [s,m,h,d,w], until
                                         G stops changing:
         ? eG                              if eG != 0:
          G                                  update G with G (stop changing)
                                           else:
             PG                              update G with G[-1]
                                         this gets rid of trailing zeros
      +V                         "smhdw  vectorized add with "smhdw"
   >vz                                   only use the last eval(z) items
  _                                      reverse order
jd                                       join by spaces and print

3

Pythonの2.7 - 181の 178 174バイト

ゴルフをする私の最初の試み。

def I(s,M):
 z=[0]*5;j=0
 for i in [604800,86400,3600,60,1]:z[j],s=s/i,s%i;j+=1
 l=[z.index(n)for n in z if n][0]
 return" ".join([str(i)+n for n,i in zip('wdhms',z)][l:l+M])

1
素晴らしい最初の試み!私の6回目の試行よりも優れています...;)に変更if n!=0することで3バイトをカットできますif n
kirbyfan64sos

そうそう、0がFalseと評価されることを忘れていました。ありがとう。
f。ロドリゲス

2

ジュリア、158バイト

これはもっと賢いアプローチで短くなると確信していますが、これは今のところ持っているものです。

(s,g)->(j=0;p=cell(5,1);for i=[604800,86400,3600,60,1] p[j+=1],s=s÷i,s%i end;z=findfirst(p);z>0?join([string(p[i],"wdhms"[i])for i=z:min(z+g-1,5)]," "):"0s")

これにより、2つの整数を入力として受け入れ、文字列を返す名前のない関数が作成されます。呼び出すには、名前を付けf=(s,g)->...ます。

Ungolfed +説明:

function f(s, g)
    # Initialize an array and an index
    p = cell(5, 1)
    j = 0

    # Loop over the number of seconds in a week, day, hour,
    # minute, and second
    for i in [604800, 86400, 3600, 60, 1]
        # Set the jth element in p to be the quotient and s
        # to be the remainder of i into s
        p[j+=1], s = s ÷ i, s % i
    end

    # Get the index of the first nonzero value in p
    z = findfirst(p)

    if z > 0
        # We start the string at the first nonzero value
        # and continue until we hit the desired number of
        # units (z+g-1) or the maximum number of units (5),
        # whichever comes first. The appropriate unit is
        # appended to each value and the values are then
        # joined with a space.
        join([string(p[i], "wdhms"[i]) for i in z:min(z+g-1,5)], " ")
    else
        # If there are no nonzero values in p, we just
        # have 0 seconds
        "0s"
    end
end

例:

julia> f(82815, 6)
"23h 0m 15s"

julia> f(604800, 4)
"1w 0d 0h 0m"

1

Scala、147バイト

def p(s:Int,i:Int)=List(s/604800+"w",s/86400%7+"d",s/3600%24+"h",s/60%60+"m",s%60+"s").dropWhile(k=>k.head=='0'&&k.tail!="s").take(i).mkString(" ")

1

rs、367バイト

^(\d+)/(_)^^(\1)
(_*) (\d)/\1!\2
_{60}/#
#{60}/@
@{24}/:
:{7}/;
(_+)/(^^\1)s
(#+)/(^^\1)m
(@+)/(^^\1)h
(:+)/(^^\1)d
(;+)/(^^\1)w
([a-z])/\1 
w ((\d+[^\dd])|!)/w 0d \1
d ((\d+[^\dh])|!)/d 0h \1
h ((\d+[^\dm])|!)/h 0m \1
(m|^) ?!/\1 0s!
!(\d+)/!(_)^^(\1)
#
+#(\d+)([a-z]) ?(.*)!(_*)/\1\2 #\3!\4@
#/
(@+)/ (_)^^((^^\1))
!(_+) \1(_*)/!\2
_+ _+/
+\d+[a-z] ?!_/!
 *!/
^$/0s

ライブデモとすべてのテストケース。

乱雑、乱雑、乱雑...

Chrome for Androidでテストケースを実行するのに約3〜7秒かかります。デバッグモードを使用しないでください。デバッグモードを使用すると、印刷されるすべての出力が原因で、この場合はブラウザーがフリーズする可能性があります。


RS何-----?
カレブポール

@Wideshanksタイトルにリンクを追加しました。私が書いた正規表現ベースの言語っぽいものです。
kirbyfan64sos

0

C#、239237170164バイト

これは他のソリューションほどコンパクトではありませんが、自分で突き刺さなくてはこの課題をもたらすことはできません。

この最新の反復は、ESCの回答に触発されました。

明確にするためにインデント:

string G(int s,int n){
    int[]x={s%60,(s/=60)%60,(s/=60)%24,(s/=24)%7,s/7};
    for(s=5;x[--s]==0&s>0;);
    var o="";
    while(n-->0&s>=0)
        o+=" "+x[s]+"smhdw"[s--];
    return o.Trim();
}
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.