The Highest Dice

19

Challenge:

Here we have the first 100 items of a sequence:

6,5,4,3,2,1,66,65,64,63,62,61,56,55,54,53,52,51,46,45,44,43,42,41,36,35,34,33,32,31,26,25,24,23,22,21,16,15,14,13,12,11,666,665,664,663,662,661,656,655,654,653,652,651,646,645,644,643,642,641,636,635,634,633,632,631,626,625,624,623,622,621,616,615,614,613,612,611,566,565,564,563,562,561,556,555,554,553,552,551,546,545,544,543,542,541,536,535,534,533,...

How is this sequence formed? We first have the number in the range [6, 1] (all possible values of a single die from highest to lowest). We then have the numbers [66..61, 56..51, 46..41, 36..31, 26..21, 16..11] (all possible concatted values of two dice from highest to lowest). Etc.
This is related to the OEIS sequence A057436: Contains digits 1 through 6 only, but with all the numbers with equal amount of digits sorted backwards in the sequence.

The challenge is to choose one of these three options for your function/program with the sequence above:

  1. Take an input \$n\$ and output the \$n\$'th value of this sequence, where it can be either 0-indexed or 1-indexed.
  2. Take an input \$n\$ and output the first \$n\$ or \$n+1\$ values of this sequence.
  3. Output the values from the sequence indefinitely.

Of course, any reasonable output format can be used. Could be as strings/integers/decimals/etc.; could be as an (infinite) list/array/stream/etc.; could be output with space/comma/newline/other delimiter to STDOUT; etc. etc. Please state what I/O and option you're using in your answer!

General rules:

  • This is , so shortest answer in bytes wins.
    Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.
  • Standard rules apply for your answer with default I/O rules, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
  • Default Loopholes are forbidden.
  • If possible, please add a link with a test for your code (i.e. TIO).
  • Also, adding an explanation for your answer is highly recommended.

Here some larger test cases if you choose option 1:

n         0-indexed output    1-indexed output

500       5624                5625
750       4526                4531
1000      3432                3433
9329      11111               11112
9330      666666              11111
9331      666665              666666
10000     663632              663633
100000    6131232             6131233

Kevin Cruijssen

Posted 2019-02-15T10:20:32.330

Reputation: 67 575

1Per the suggested edit, kolmogorov complexity tag does not apply to sequences, only to a constant, finite, fixed output. A sequence goes on forever. – mbomb007 – 2019-02-23T21:46:49.927

In addition to what @mbomb007 said, I also allow outputting the n'th value or first n/n+1 values based on an input, whereas KC challenges will never have inputs. – Kevin Cruijssen – 2019-02-23T22:23:04.043

Answers

2

Jelly, 5 bytes

ḃ6ạ7V

Try it online!

1-indexed nth value.

Erik the Outgolfer

Posted 2019-02-15T10:20:32.330

Reputation: 38 134

12

Perl 6, 24 23 bytes

-1 byte thanks to nwellnhof

{.put;.[]X~(6...1)}...*

Try it online!

Outputs the sequence infinitely separated by spaces/newlines. Or, for a few more bytes we can have a lazy infinite list we can index into instead.

Perl 6, 27 bytes

{flat {@=.[]X~(6...1)}...*}

Try it online!

Explanation:

{                         }    # Anonymous code block
 flat                          # Return the flattened
                      ...*       # Infinite sequence
      {              }             # Defined as
         .[]                       # The previous element arrayified
            X~                     # Each concatenated with
              (6...1)              # All of 6 to 1
       @=                          # Arrayified

Jo King

Posted 2019-02-15T10:20:32.330

Reputation: 38 234

6

Python 2, 39 38 34 bytes

f=lambda n:n and-n%6+1+f(~-n/6)*10

Try it online!

Outputs 1-indexed number

TFeld

Posted 2019-02-15T10:20:32.330

Reputation: 19 246

6

Bash, 31 bytes

f()(x+={6..1};eval echo $x;f);f

TIO

update from comments, the n'th value 1-indexed, +GNU tools + perl, 64 bytes, 7 bytes saved thanks to @manatwork

dc<<<6o$1p|perl -pe 's/(.)0/($1-1).6/e?redo:s/0//'|tr 1-6 654321

64 bytes

Nahuel Fouilleul

Posted 2019-02-15T10:20:32.330

Reputation: 5 582

Doesn't help much, but the in the 2nd solution is shorter to escape the semicolon than double quoting the entire expression: bc<<<obase=6\;$1. But if you switch to dc, there is nothing to escape: dc<<<6o$1p. – manatwork – 2019-02-15T14:26:13.520

thanks indeed it saves 7bytes but because of bijective numeration it still not working a mix bash perl (66bytes) dc<<<6o$1p|perl -pe '1while s/(.)0/($1-1).6/e;s/0//'|tr 1-6 654321 – Nahuel Fouilleul – 2019-02-15T14:48:17.633

6

R, 43 bytes

p='';repeat cat(p<-sapply(p,paste0,6:1),'')

Try it online!

Prints the sequence indefinitely

  • -9 thanks to @Kirill L.

digEmAll

Posted 2019-02-15T10:20:32.330

Reputation: 4 599

1@tk3: without the second parameter it will concatenate the last value of the sub-sequence of n-digits elements, with the first value of the sub-sequence of n+1 digits elements. e.g. 6 5 4 3 2 166 65 64... – digEmAll – 2019-02-15T18:39:07.753

5

MATL, 11 bytes

`6:P!V@Z^DT

Outputs the values indefinitely.

Try it online!

Explanation

`      % Do...while
  6:   %   Push [1 2 3 4 5 6]
  P    %   Flip: gives [6 5 4 3 2 1]
  !    %   Transpose: turns the row vector into a column vector
  V    %   Convert the number in each row to the corresponding char
  @    %   Push current iteration index, starting from 1
  Z^   %   Cartesian power. Gives a matrix where each row is a Cartesian tuple
  D    %   Display immediately
  T    %   Push true. This is used as loop condition, to give an infinite loop
       % End (implicit)

Luis Mendo

Posted 2019-02-15T10:20:32.330

Reputation: 87 464

5

JavaScript (ES6), 26 bytes

Returns the \$n\$th term, 1-indexed.

f=n=>n--&&[f(n/6|0)]+6-n%6

Try it online!

Arnauld

Posted 2019-02-15T10:20:32.330

Reputation: 111 334

5

Haskell, 38 34 bytes

An infinite list of numbers:

d=[6,5..1]
l=d++[10*m+n|m<-l,n<-d]

Try it online!

Two earlier solutions that give infinite lists of strings, each using 38 bytes:

[1..]>>=sequence.(`replicate`"654321")

Try it online!

do n<-[1..];mapM id$[1..n]>>["654321"]

Try it online!

Christian Sievers

Posted 2019-02-15T10:20:32.330

Reputation: 6 366

A 36; byte version, based on your replicate one. – dfeuer – 2019-03-16T09:38:02.617

5

Haskell, 28 bytes

l=(+).(10*)<$>0:l<*>[6,5..1]

Try it online!

Produces an infinite list of numbers l. Using <$> and <*> cuts a byte off:

29 bytes

l=[10*n+d|n<-0:l,d<-[6,5..1]]

Try it online!

The approach is similar to the Haskell Output All String answer fixed input string "654321", and skipping the empty string output by changing where it's prepended.

30 bytes

l=[n++[d]|n<-"":l,d<-"654321"]

Try it online!

xnor

Posted 2019-02-15T10:20:32.330

Reputation: 115 687

That's great! I saw it's shorter to start off from 0 (or ""), but didn't find a cheap way to not have it in the result... – Christian Sievers – 2019-02-16T01:33:26.423

4

05AB1E, 10 bytes

Outputs the sequence indefinitely.

¸[6LRâJD»,

Try it online!

Explanation

¸           # initialize the stack with a list containing the empty string
 [          # loop
  6L        # push [1 ... 6]
    R       # reverse
     â      # cartesian product
      J     # join each inner list
       D    # duplicate (saving a copy for next iteration)
        »,  # join on newline and print

Emigna

Posted 2019-02-15T10:20:32.330

Reputation: 50 798

1Never knew ¸ at the start creates a list containing an empty string. And 2 bytes shorter than the solution I used to generate the test cases, so of course a +1 from me. :) – Kevin Cruijssen – 2019-02-15T11:50:54.653

4

Perl 5, 40 37 bytes

-3bytes thanks to @Xcali

map{say}<"$_">while s//{6,5,4,3,2,1}/

37 bytes

40 bytes

Nahuel Fouilleul

Posted 2019-02-15T10:20:32.330

Reputation: 5 582

37 bytes – Xcali – 2019-02-15T17:41:26.593

Brilliant use of glob. +1 – msh210 – 2019-02-23T23:26:56.163

3

Java (JDK), 48 bytes

String f(int n){return n-->0?f(n/6)+(6-n%6):"";}

Try it online!

This returns the 1-indexed nth element.

Recursion seems to beat the iterative lambda.


Iterative version, 49 bytes

n->{var r="";for(;n-->0;n/=6)r=6-n%6+r;return r;}

Try it online!

Olivier Grégoire

Posted 2019-02-15T10:20:32.330

Reputation: 10 647

3

C# (.NET Core), 38 bytes

int f(int n)=>n-->0?f(n/6)*10+6-n%6:0;

Try it online!

Outputs the n-th value (1-based).

dana

Posted 2019-02-15T10:20:32.330

Reputation: 2 541

Which one is this solving? – Stackstuck – 2019-02-20T10:03:52.840

@Stackstuck - I've now specified this in my answer. – dana – 2019-02-20T11:13:24.047

1Oh good! We're not overlapping. I just wrote the infinite printer. – Stackstuck – 2019-02-20T11:15:06.387

3

Brachylog, 13 11 bytes

Thanks to Fatalize for 2 bytes

6~d{⟧₁∋}ᵐẉ⊥

Outputs indefinitely. Try it online!

Here's a 14-byte version that outputs the first \$n\$ values: Try it online!

Explanation

6~d           Start with a number, all of whose digits are 6's
              Brachylog considers these in the order 6, 66, 666, 6666...
   {   }ᵐ     Map this predicate to each of those digits:
    ⟧₁         1-based reverse range: [6,5,4,3,2,1]
      ∋        The output digit must be a number in that range
              Brachylog considers possible outputs in this order: 6, 5, 4, 3, 2, 1, 66, 65...
         ẉ    Write a possible output with newline
          ⊥   Force the predicate to fail and backtrack to the next possibility

DLosc

Posted 2019-02-15T10:20:32.330

Reputation: 21 213

You are on a Brachylog roll! – Fatalize – 2019-02-18T12:31:28.140

1

You can save 2 bytes by using a failure-driven loop, as they are called in Prolog: 6~d{⟧₁∋}ᵐẉ⊥. You basically end your program with "false" which will force it to print all solutions.

– Fatalize – 2019-02-18T12:36:14.560

Ooh, nice. Yes, I've been enjoying it a lot! – DLosc – 2019-02-18T19:13:42.277

2

Japt, 14 bytes

There's gotta be a shorter solution using function methods and/or Cartesian product but (for now?) the best I can manage is a port of Arnauld's JS solution so be sure to upvote him too.

©ß´Uz6)s+6-Uu6

Try it or test terms 0-1000

Shaggy

Posted 2019-02-15T10:20:32.330

Reputation: 24 623

2

Wolfram Language (Mathematica), 88 78 bytes

(l=d=c=7-Range@6;While[Length@c<#,d=Flatten[(10#+l)&/@d];c=c~Join~d;];c[[#]])&

Try it online!

saved 4 + 6 bytes thanks to @IanMiller

List is 1 indexed, outputs the n'th number.

Kai

Posted 2019-02-15T10:20:32.330

Reputation: 201

1You can replace Range[6,1,-1] with 7-Range@6 to save 4 characters – Ian Miller – 2019-02-17T02:36:49.777

1For codegolf rules you can also write it as an anonymous function: (l=d=c=7-Range@6;While[Length@c<#,d=Flatten[(10#+l)&/@d];c=c~Join~d;];c[[#]])& – Ian Miller – 2019-02-17T02:39:23.383

@IanMiller thanks! I wasn't sure what the rules were about the format. – Kai – 2019-02-17T03:04:21.820

2

Mathematica, 56 bytes

Flatten[FromDigits/@Tuples[7-Range@6,#]&/@Range@#][[#]]&

Requires excessive amounts of memory to actually run as it creates all numbers in the sequence which have length equal to or less than the input (i.e. it creates \$\frac{6}{5}(6^n-1)\$ terms) then takes the required value from the list. For practice use replace the second last # with a specific value for the length you want to use it on.

Ian Miller

Posted 2019-02-15T10:20:32.330

Reputation: 727

+1, that is monstrous overkill but works perfectly for brevity! – Kai – 2019-02-18T05:24:05.190

@JonathanFrech Thanks for fixing my mathjax. I wasn't sure how to activate it here as its slightly different that math.se – Ian Miller – 2019-02-26T07:48:07.787

Please note that the original edit was by this user.

– Jonathan Frech – 2019-02-26T20:13:43.587

Oops my bad. Thanks to @geza-kerecsenyi too. – Ian Miller – 2019-02-27T09:53:18.480

1

Pip -l, 16 bytes

x:YP6-,6W1PxCP:y

Outputs the sequence indefinitely. Try it online!

Explanation

The -l flag means that lists are printed with each item on its own line; if an item is itself a list, its elements are concatenated with no separator. E.g. the list [1 [2 3] [4 [5 6]]] would be printed as

1
23
456

With that cleared up:

x:YP6-,6W1PxCP:y
      ,6          Range(6): [0 1 2 3 4 5]
    6-            Subtract each element from 6: [6 5 4 3 2 1]
  YP              Yank that value into the y variable, and also print it
x:                Assign that value also to x
        W1        While 1 (infinite loop):
           xCP:    Assign to x the cartesian product of x with
               y   the list [6 5 4 3 2 1]
          P        Print it

After the first loop iteration, x looks like [[6;6];[6;5];[6;4];...;[1;1]]; after the second iteration, [[[6;6];6];[[6;6];5];[[6;6];4];...;[[1;1];1]]; and so on. We don't need to worry about flattening the sublists, because -l effectively does it for us.

DLosc

Posted 2019-02-15T10:20:32.330

Reputation: 21 213

1

Charcoal, 18 bytes

NθWθ«←I⊕﹪±θ⁶≔÷⊖θ⁶θ

Try it online! Link is to verbose version of code. 1-indexed. Explanation:

Nθ

Input n

Wθ«

Repeat until n is zero.

←I⊕﹪±θ⁶

Reduce -n modulo 6, then increment the result and output from right to left.

≔÷⊖θ⁶θ

Decrement n and integer divide it by 6.

Neil

Posted 2019-02-15T10:20:32.330

Reputation: 95 035

1

Retina 0.8.2, 36 bytes

.+
$*_
+`(_*)\1{5}(_+)
$1$.2
T`7-1`d

Try it online! Link includes test cases. 1-indexed. Explanation:

.+
$*_

Convert to unary. (Retina 1 would save 2 bytes here.)

+`(_*)\1{5}(_+)
$1$.2

Convert to bijective base 6 by repeated divmod. Note that the use of + means that the extracted digit is always a number from 1 to 6 instead of 0 to 5 for regular base 6 conversion. ((_{6})* is faster but costs bytes extracting the quotient.)

T`7-1`d

Transpose the digits so that the 6s come first and the 1s last. (There are no 7s or 0s but this allows me to use the d shortcut.

Neil

Posted 2019-02-15T10:20:32.330

Reputation: 95 035

1

Cubix, 22 bytes

This will output the sequence indefinitely. The general idea is that it has a base number which 6 - 1 is added to. For each add the result is output multiplied by 10, which is pushed to the bottom of the stack to be used later in the sequence. The base is then popped and the next base started.

..w.06+ONo*w;|uW!(pq;;

Try it online!

    . .
    w .
0 6 + O N o * w
; | u W ! ( p q
    ; ;
    . .

Watch It Run

MickyT

Posted 2019-02-15T10:20:32.330

Reputation: 11 735

1

C# (.NET Core), infinite printing, 181 180 88 bytes.

string[] s=new string[]{" "},t;int i,j,k,l=1;while(true){j=l;t=new string[l=6*j];for(i=6;i>0;i--)for(k=(6-i)*j;k<l;)t[k]=i+s[k++%j];
for(k=0;k<l;)System.Console.Write(t[k++]);s=t;}

Sadly it freezes repl.it instead of outputting properly in the infinite version as written (I believe it to be an error in repl.it, as it does not output as the program loops like it should), so anyone hoping to test needs a computer. If you add a read at the front of the loop it works in repl.it as well.

Outputs to the console, obviously.

On any finite system the code will most likely eventually crash with an out of memory error.

Reworked the code to use @dana 's lambda.

int f(int n)=>n-->0?f(n/6)*10+6-n%6:0;for(int i=0;++i>0;)System.Console.Write(f(i)+" ");

Try it online!

Stackstuck

Posted 2019-02-15T10:20:32.330

Reputation: 209

I have no idea if I've golfed this well or not. – Stackstuck – 2019-02-20T11:40:07.660

Saved one byte by removing unnecessary k++. – Stackstuck – 2019-02-20T11:42:57.010

(also, I greatly welcome golfing assistance, I'm very new to this.) – Stackstuck – 2019-02-20T11:52:48.537

2

Welcome :) If you are interested in golfing in C#, you may want to check out this post for some tips: https://codegolf.stackexchange.com/q/173/8340

– dana – 2019-02-20T15:53:41.640

1

Forth (gforth), 63 bytes

: f recursive dup 6 < if 6 - abs 1 .r else 6 /mod 1- f f then ;

Try it online!

0-indexed outputs nth value

Explanation

If N is less than 6, output the absolute value of N - 6. Otherwise, get the quotient and remainder of dividing N by 6. Call the function recursively on the quotient, then call it on the remainder.

Code Explanation

: f                 \ start a new word definition
  recursive         \ declare that this word is recursive so we can call it from itself
  dup 6 <           \ check if n is less than 6
  if                \ if it is:
    6 - abs 1 .r    \ subtract 6, get the absolute value, then print with no appended space
  else              \ else if it's greater than 6:
    6 /mod          \ get the quotient and remainder of dividing n by 6
    1-              \ subtract 1 from the quotient (because we're 0-indexed)
    f               \ call f on the result
    f               \ call f on the remainder (shortcut to format and print, it's always < 6)
  then              \ end the if/else
;                   \ end the word definition

reffu

Posted 2019-02-15T10:20:32.330

Reputation: 1 361

1

APL(NARS), 27 chars, 54 bytes

{0>⍵-←1:0⋄(6-6∣⍵)+10×∇⌊⍵÷6}

traslate the solution by dana https://codegolf.stackexchange.com/a/179980 in APL... test:

  f←{0>⍵-←1:0⋄(6-6∣⍵)+10×∇⌊⍵÷6}
  f 500
5625
  f¨750 1000 9329 9331 10000 100000
4531 3433 11112 666666 663633 6131233 
  f¨⍳9
6 5 4 3 2 1 66 65 64 

RosLuP

Posted 2019-02-15T10:20:32.330

Reputation: 3 036

0

C#, print from start to n, ??? bytes

Credit to @dana for the lambda expression.

int f(int n)=>n-->0?f(n/6)*10+6-n%6:0;for(int i=0;i<int.Parse(a[0]);)System.Console.Write(f(++i)+" ");

Operation: Run with command line 0th argument equal to the integer you want to count to. (It should be noted that a[0] is a reference to the otherwise unmentioned command line args array, and I don't know how to count it.)

Stackstuck

Posted 2019-02-15T10:20:32.330

Reputation: 209

Since part of the code is a snipper instead of a full program or function, I assume you're using the Visual C# Interactive Compiler? Could you perhaps add a Try it online link with test code? PS: Your current byte-count is 102

– Kevin Cruijssen – 2019-02-20T17:34:38.320

ah shit it isn't working what in the heck. – Stackstuck – 2019-02-21T01:58:05.387