Skip to main content
Commonmark migration
Source Link

#Java 10, 140 139 bytes

Java 10, 140 139 bytes

#Java 10, 140 139 bytes

Java 10, 140 139 bytes

deleted 1 character in body
Source Link
Kevin Cruijssen
  • 131.4k
  • 13
  • 144
  • 384

#Java 10, 140140 139 bytes

v->{var r="  1\n x x\nx 9 x\n x x\n  x";for(int dd=9,m=3;m<511;r=r.replaceFirst("x",d+""))for(d=9;m==;m==(m|=1<<(d*=Math.random()));d=9);return r;}

Try it online.Try it online.

v->{                // Method with empty unused parameter and String return-type
  var r="  1\n x x\nx 9 x\n x x\n  x";
                    //  Result-String, starting at:
                    //  "  1
                    //    x x
                    //   x 9 x
                    //    x x
                    //     x"
  for(int dd=9,       //  Temp integer for the random digit, starting at 9
          m=3;      //  Bit-mask `m`, starting at 3 (binary 000000011)
          m<511     //  Loop as long as `m` is not 511 yet (binary 111111111):
          ;         //    After every iteration:
           r=r.replaceFirst("x",d+""))
                    //     Replace the first "x" with digit `d`
    for(d=9;       //   Reset `d` to 9
        m==;m==        //   Inner loop as long as `m` is unchanged after
            (m|=    //   `m` is bitwise-ORed with:
                1<< //    2 to the power
                   (d*=Math.random()));
                    //    a random digit in the range [0,9)
      d=9);         //     After every iteration: reset `d` to 9 again
  return r;}        //  And finally return the result-String

#Java 10, 140 bytes

v->{var r="  1\n x x\nx 9 x\n x x\n  x";for(int d,m=3;m<511;r=r.replaceFirst("x",d+""))for(d=9;m==(m|=1<<(d*=Math.random()));d=9);return r;}

Try it online.

v->{               // Method with empty unused parameter and String return-type
  var r="  1\n x x\nx 9 x\n x x\n  x";
                   //  Result-String, starting at:
                   //  "  1
                   //    x x
                   //   x 9 x
                   //    x x
                   //     x"
  for(int d,       //  Temp integer for the random digit
          m=3;     //  Bit-mask `m`, starting at 3 (binary 000000011)
          m<511    //  Loop as long as `m` is not 511 yet (binary 111111111):
          ;        //    After every iteration:
           r=r.replaceFirst("x",d+""))
                   //     Replace the first "x" with digit `d`
    for(d=9;       //   Reset `d` to 9
        m==        //   Inner loop as long as `m` is unchanged after
           (m|=    //   `m` is bitwise-ORed with:
               1<< //    2 to the power
                  (d*=Math.random()));
                   //    a random digit in the range [0,9)
      d=9);        //     After every iteration: reset `d` to 9 again
  return r;}       //  And finally return the result-String

#Java 10, 140 139 bytes

v->{var r="  1\n x x\nx 9 x\n x x\n  x";for(int d=9,m=3;m<511;r=r.replaceFirst("x",d+""))for(;m==(m|=1<<(d*=Math.random()));d=9);return r;}

Try it online.

v->{                // Method with empty unused parameter and String return-type
  var r="  1\n x x\nx 9 x\n x x\n  x";
                    //  Result-String, starting at:
                    //  "  1
                    //    x x
                    //   x 9 x
                    //    x x
                    //     x"
  for(int d=9,      //  Temp integer for the random digit, starting at 9
          m=3;      //  Bit-mask `m`, starting at 3 (binary 000000011)
          m<511     //  Loop as long as `m` is not 511 yet (binary 111111111):
          ;         //    After every iteration:
           r=r.replaceFirst("x",d+""))
                    //     Replace the first "x" with digit `d`
    for(;m==        //   Inner loop as long as `m` is unchanged after
            (m|=    //   `m` is bitwise-ORed with:
                1<< //    2 to the power
                   (d*=Math.random()));
                    //    a random digit in the range [0,9)
      d=9);         //     After every iteration: reset `d` to 9 again
  return r;}        //  And finally return the result-String
Source Link
Kevin Cruijssen
  • 131.4k
  • 13
  • 144
  • 384

#Java 10, 140 bytes

v->{var r="  1\n x x\nx 9 x\n x x\n  x";for(int d,m=3;m<511;r=r.replaceFirst("x",d+""))for(d=9;m==(m|=1<<(d*=Math.random()));d=9);return r;}

Port of @Arnauld's JavaScript answer, so make sure to upvote him as well!

Try it online.

Explanation:

v->{               // Method with empty unused parameter and String return-type
  var r="  1\n x x\nx 9 x\n x x\n  x";
                   //  Result-String, starting at:
                   //  "  1
                   //    x x
                   //   x 9 x
                   //    x x
                   //     x"
  for(int d,       //  Temp integer for the random digit
          m=3;     //  Bit-mask `m`, starting at 3 (binary 000000011)
          m<511    //  Loop as long as `m` is not 511 yet (binary 111111111):
          ;        //    After every iteration:
           r=r.replaceFirst("x",d+""))
                   //     Replace the first "x" with digit `d`
    for(d=9;       //   Reset `d` to 9
        m==        //   Inner loop as long as `m` is unchanged after
           (m|=    //   `m` is bitwise-ORed with:
               1<< //    2 to the power
                  (d*=Math.random()));
                   //    a random digit in the range [0,9)
      d=9);        //     After every iteration: reset `d` to 9 again
  return r;}       //  And finally return the result-String