21
\$\begingroup\$

Challenge

The goal of this challenge is to generate and output a \$16 \times 32\$ (height \$\times\$ width) grid where all cells contain a decoy character, except for a random single cell that contains the target character. The input will be a pair of ASCII (32-126) characters: [target,decoy], where target is the character to find, and decoy is the character that fills the rest of the grid. The selection of the target cell must be uniformly random from among the \$512\$ cells, ensuring that each cell has an equal chance (\$\frac{1}{512}\$) of being chosen for modification.

Examples

The preferable output format should closely resemble these examples below as this enhances the challenge of locating the target character. However, creative solutions are welcome and minor deviations in format (e.g., additional leading/trailing whitespace, alternative ways of representing the grid) are acceptable.

Input

[0,O]

Output

OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOO0OOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO

Input

[v,w]

Output

wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
wvwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww

[target,decoy] suggestions

[0,O] [1,l] [1,I] [2,Z] [5,S] [8,B] [v,u] [m,n]
[b,d] [p,q] [C,G] [E,F] [V,U] [V,Y] [s,z] [6,9]
[D,O] [&,8] [C,O] [$,S] [R,P] [8,3] [4,A] [c,o]
\$\endgroup\$
5
  • \$\begingroup\$ Can we reverse the input i.e. decoy followed by target? \$\endgroup\$
    – Graham
    Feb 28 at 19:24
  • \$\begingroup\$ Sure. Both [target,decoy] or [decoy,target] are fine. \$\endgroup\$
    – vengy
    Feb 28 at 19:25
  • \$\begingroup\$ I've VTCed because you need to specify the acceptable output formats. \$\endgroup\$
    – Shaggy
    Feb 29 at 22:49
  • \$\begingroup\$ Like Shaggy said, there seems too much wiggle room. For example in C, is char grid[16][32] an acceptable output format, like f(t,d,char g[][32])? Some would argue that a 2D grid layout is implicit in a 2D array, unlike in a string where newlines are needed. Especially in languages like Python where common implementations have pretty-printing of 2D arrays. \$\endgroup\$ Mar 3 at 0:41
  • \$\begingroup\$ One of your test-cases has a non-ASCII character: "¢". The challenge part says "The input will be a pair of ASCII characters". Perhaps that's intended as a bonus for answers that support unicode or an 8-bit character set? \$\endgroup\$ Mar 3 at 0:43

38 Answers 38

9
\$\begingroup\$

Python, 87 bytes

import random as r,textwrap as w
f=lambda t,d:w.fill("".join(r.sample(t+d*511,512)),32)

Attempt This Online!

Same sampling technique as @movatica, but a different approach to wrapping, using textwrap

\$\endgroup\$
1
8
\$\begingroup\$

Google Sheets, 52 bytes

=index(if(randbetween(1,512)=sequence(16,32),B1,A1))

Put the decoy in cell A1, the target in cell B1, and the formula in cell C1. The formula outputs in cells C1:AH16.

odd one out.png

In Google Sheets, array formulas take the dimensions of the argument that has the biggest dimensions. In this instance, it is the matrix produced by sequence(). The value given by randbetween() is compared with each element in the matrix in turn.

(-14 bytes thanks to z..)

\$\endgroup\$
3
  • 2
    \$\begingroup\$ -14 =index(if(randbetween(1,512)=sequence(16,32),B1,A1)) \$\endgroup\$
    – z..
    Feb 29 at 6:27
  • 1
    \$\begingroup\$ D'oh. Thanks @z.. \$\endgroup\$ Feb 29 at 8:55
  • \$\begingroup\$ If you convert this to an excel answer, you can drop the index call, and just go with =IF(RANDBETWEEN(1,512)=SEQUENCE(16,32),A1,B1) \$\endgroup\$ Mar 29 at 17:52
6
\$\begingroup\$

R, 59 46 44 bytes

\(x)write(sample(rep(x,c(1,511))),"",32,,"")

Attempt This Online!

Takes input as a vector c(target,decoy).

\$\endgroup\$
5
\$\begingroup\$

Pip, 17 bytes

(SHaX511.b)<>32Jn

Attempt This Online!

First time trying Pip!

Explanation:

(SH...) shuffle the following:

  • aX511 first input repeated 511 times
  • .b append second input

<>32 split into chunks of length 32

Jn join on newlines

This can be 14 bytes with the -n flag:

Pip -n, 14 bytes

SHaX511.b<>:32

Attempt This Online!

Same as above, but uses assignment at the end to reduce the operator precedence of <> and save a parenthesis. The -n flag joins the output list on newlines at the end of the program. We can't do anything else after the assignment trick because of how it works, so this isn't applicable to the first solution.

\$\endgroup\$
5
\$\begingroup\$

Perl 5 -pl, 47 45 bytes

Credit @DomHastings for saving 2 bytes.

$_ x=512;substr$_,rand 512,1,<>;s/.{32}\K/
/g

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Nice one! This is almost exactly the same idea I had, but you can save a couple of bytes using -p instead of -n and finishing with s/.{32}\K/\n/g (literal newline) too! \$\endgroup\$ Feb 29 at 16:35
4
\$\begingroup\$

JavaScript (ES6), 66 bytes

Expects (target)(decoy).

(a,k=Math.random(n=512)*n)=>g=b=>n--?(n^k?a:b)+[`
`[n&31]]+g(b):''

Try it online!

\$\endgroup\$
4
\$\begingroup\$

APL (Dyalog Unicode), 15 bytes

Full program. Prompts for target+decoy. Requires 0-based indexing (⎕IO←0).

⍞[16 32⍴×?⍨512]

Try it online!

⍞[] prompt for target (index 0) + decoy (index 1) and index with the following array:

?⍨512 random permutation of integers 0…511

× sign (leaves a single 0 and the rest as 1s)

16 32⍴reshape into 16 rows and 32 columns

\$\endgroup\$
2
  • \$\begingroup\$ @LuisMendo The inputs don't include indices. I'm just pointing out that in the input (a two-element character array – "string") the target ends up having character index 0 and the decay ends up having character index 1, so later when we (multi-)index the string with a large Boolean matrix, we end up selecting each result-character from among those two input characters. \$\endgroup\$
    – Adám
    Feb 28 at 22:17
  • \$\begingroup\$ Ah, I see, thanks \$\endgroup\$
    – Luis Mendo
    Feb 28 at 22:33
4
\$\begingroup\$

Vyxal j, 57 bitsv2, 7.125 bytes

k¶ø↳Þ℅32ẇ

Try it Online!

Bitstring:

100000101110010111110010011011011010000100001111110101001
\$\endgroup\$
2
  • 2
    \$\begingroup\$ I thought I was cool until I realized my idea was literally exactly this lol \$\endgroup\$
    – noodle man
    Feb 29 at 0:08
  • 1
    \$\begingroup\$ i mean it just pads the second input to length 512 using the first one and shuffles and splits, really is the most straightforward way of doing it \$\endgroup\$
    – pacman256
    Feb 29 at 1:31
4
\$\begingroup\$

Python, 108 105 103 101 95 bytes

-2 bytes thanx to CursorCoercer -2 bytes thanx to vengy

from random import*
f=lambda*x,k=randrange(512):''.join('\n'[i%32:]+x[i!=k]for i in range(512))

Attempt This Online!

Target and Decoy can be given as iterable or two arguments. Returns grid as string with an extra newline in front.

Still a little longer than the textwrap-approach though :)

\$\endgroup\$
3
  • 1
    \$\begingroup\$ -2 bytes by removing k= \$\endgroup\$ Feb 28 at 23:05
  • \$\begingroup\$ How could I miss that :) \$\endgroup\$
    – movatica
    Feb 29 at 18:41
  • 1
    \$\begingroup\$ 101 bytes \$\endgroup\$
    – vengy
    Feb 29 at 19:52
3
\$\begingroup\$

APL+WIN, 20 bytes

Prompts for decoy followed by target.

16 32⍴(?512)⌽⎕,511⍴⎕

Try it online! Thanks to Dyalog Classic

\$\endgroup\$
5
  • \$\begingroup\$ This one was tricky to find the odd one out. ;) \$\endgroup\$
    – vengy
    Feb 28 at 19:43
  • \$\begingroup\$ @vengy That is surprising as I am using '-' as the decoy and 'x' as the target on the TIO example \$\endgroup\$
    – Graham
    Feb 28 at 19:47
  • \$\begingroup\$ I know, it's so obvious, that's the joke. ;) \$\endgroup\$
    – vengy
    Feb 28 at 19:51
  • \$\begingroup\$ ⍞[16 32⍴×512?512] should work in APL+Win if I understand right. \$\endgroup\$
    – Adám
    Feb 28 at 22:23
  • \$\begingroup\$ @Adám Thanks indeed it does. I will leave my answer as is so as not to become simply a clone of yours.. Also it does show an alternative to your indexing approach. \$\endgroup\$
    – Graham
    Feb 29 at 8:34
3
\$\begingroup\$

Retina, 29 bytes

^"/./@_`$0"~L$0`.
16*$(32*$0¶

Try it online!

A nice single eval stage (that hides several stages inside). Takes as input the decoy character followed by the target character, with a newline separating them.

Explanation

^"/./@_`$0"~[...]

This is an eval (~) stage, that builds a Retina program as a string and runs it against the input. Normally the following stage would build the program, and the input of this stage would be used as input to that program as well. We are changing this behaviour in two ways:

  • ^ swaps the input and the program, so the following stage will generate the new input instead
  • "/./@_`$0" is a string option that will be applied as a substitution to the input to generate the new program

All this means that when we have an input of O\n0, we will apply the string substitution to generate the program "/./@_`O\n0".

This is a Replace stage O\n0 that can replace Os with 0s. This stage is applied to a random character thanks to the match mask _, with the option @ selecting a single random match of the regex . (any non-newline character).

So we have a program replacing one random decoy character with our target character, all that's left is generating a 16*32 rectangle of decoy characters:

L$0`.
16*$(32*$0¶

This Lists the character (.) in position 0 (first character of the input, that's the decoy character), and $ubstitutes it with 16*$(32*$0¶, which means 16 times ((32 times that character) plus a newline).

\$\endgroup\$
3
\$\begingroup\$

Rattle, 39 bytes

|!Ig0[s>]512P1=?512+$>^~sP2[[b~>]32B]16

Try it Online!

Takes input as [decoy, target] e.g. ['.','X']. This would also work for .X

Explanation

|                                         take input
 !                                        disable implicit output
  I                                       save decoy, target to memory slots 0,1
   g0                                     get value in slot 0 (decoy)
     [  ]512                              loop 512 times
      s                                   save decoy to memory at the pointer (starting at 2)
       >                                  shift pointer right
            P1                            set pointer to 1
              =?512                       set top of stack to a random value 0-511
                   +                      increment (random 1-512)
                    $                     swap target value with random value in memory
                     >^~                  move pointer right by the value in memory (random value)
                         s                save target value to its random slot
                          P2              set pointer to 2
                            [........]16  loop 16 times
                             [...]32      loop 32 times
                              b~          add the value in memory to the print buffer
                                >         move pointer right
                                    B     print and clear buffer

Basically, this generates a 512-character string of the decoy character, makes one of these characters the target character, then outputs as a 16x32 array.

This code is relatively slow. This is due to Rattle's expanding data tape - the data tape is set to a width of 100 slots by default but will automatically expand as needed (which I apparently need to optimize more because it slows execution down here).

\$\endgroup\$
3
\$\begingroup\$

Retina, 24 21 bytes

^.
511*$&
V?`
L`.{32}

Try it online! Takes input as a pair of characters <decoy><target>. Explanation:

^.
511*$&

Make 511 copies of the decoy.

V?`

Shuffle the characters randomly.

L`.{32}

Split into rows of 32.

Edit: Saved 3 bytes thanks to @Leo.

\$\endgroup\$
2
  • \$\begingroup\$ That's a much better idea than mine! You can save some bytes in your shuffling line by using ? Try it online! \$\endgroup\$
    – Leo
    Feb 29 at 2:43
  • \$\begingroup\$ @Leo I thought I was being so clever... \$\endgroup\$
    – Neil
    Feb 29 at 6:33
3
\$\begingroup\$

Knight (v2), 51 bytes

;=r+%R512=a!=p*P32W>17=a+1aO Sp%r 16T I?a+1/r 32P[p

Input the decoy in the first line and the target in the second line.

Try it online!

I think there are way too many spaces in this code, but this is what I got for now.

\$\endgroup\$
2
  • \$\begingroup\$ I clicked the Minify button and it reduced a space from %R 512 to %R512. \$\endgroup\$
    – vengy
    Feb 28 at 20:51
  • \$\begingroup\$ @vengy ah yep ofc, brainfart moment. I thought R was a variable name instead of a function so I thought I needed a space between R and 512. \$\endgroup\$
    – Aiden Chow
    Feb 29 at 6:56
3
\$\begingroup\$

K(ngn/k) 20 19 16 bytes:

Input comes in as [decoy,target].

{x@&/:/(*1?=)'32 16}
{16 32#x@~-512?512}

-3 bytes thanks to @ngn.

{16 32#x@~<?512}

try it online

\$\endgroup\$
6
  • 1
    \$\begingroup\$ i have an idea for a shorter solution. there's an overload of monadic ? that generates n random floats - ?n. it can be used together with "grade" to mask a random item: ~<?512 (there's a negligible theoretical bias to lower indices but it's ok for golfing). then that can be reshaped and indexed into x. \$\endgroup\$
    – ngn
    Mar 1 at 13:18
  • \$\begingroup\$ I tried the link but got a 404 error. Is there a way to switch to https://tio.run/#k-ngn instead? Thanks. \$\endgroup\$
    – vengy
    Mar 1 at 13:43
  • \$\begingroup\$ @vengy can you access codeberg.page at all? the status looks ok right now. tio uses an ancient version and cant be updated, unfortunately. \$\endgroup\$
    – ngn
    Mar 1 at 14:48
  • \$\begingroup\$ Works now... although the period in the output is always in the same position? ("OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO" "OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO" "OOOOOOOOOOOOOOOOOOOOOOOOOO.OOOOO"...) \$\endgroup\$
    – vengy
    Mar 1 at 14:58
  • 1
    \$\begingroup\$ @vengy It seems the version of ngn/k on tio.run is a bit old. For example, the overload of monadic ? mentioned by @ngn is not supported. \$\endgroup\$
    – akamayu
    Mar 2 at 15:34
3
\$\begingroup\$

Ruby, 42 41 bytes

->a,b{[b,*[a]*511].shuffle.each_slice 32}

Attempt This Online!

Outputs an Enumerator of lines. Jordan had the nice idea to use * to combine the lists instead of +, which saved a byte.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ @Jordan Thanks! I'm still so bad at Ruby :) I liked your idea too. I had thought of doing .scan /.{32}/ but it was so much longer with .shuffle \$\endgroup\$
    – noodle man
    Mar 17 at 16:39
3
\$\begingroup\$

Ruby, 43 42 bytes

A byte longer than noodle man’s Ruby solution but I liked it anyway.

->a,b{a*=512
a[rand 512]=b
a.scan /.{32}/}

Attempt This Online!

\$\endgroup\$
1
  • \$\begingroup\$ TIL strings are mutable in Ruby \$\endgroup\$
    – noodle man
    Mar 17 at 16:43
3
\$\begingroup\$

Uiua SBCS, 16 15 bytes

↯∞_32deal⚂⊂↯511

Try it!

-1 byte thanks to noodle man!

Note that deal was deprecated after I submitted this answer. It still works for now, but won't in the future. I'll leave this here for posterity.

↯∞_32deal⚂⊂↯511
           ↯511  # create an array of 511 decoy characters
          ⊂      # prepend target character
     deal⚂       # shuffle
↯∞_32            # reshape to 16x32
\$\endgroup\$
3
  • \$\begingroup\$ ↯16_32⊏=⌊×⚂⟜⇡512 also works for equal bytes and takes input as a string. \$\endgroup\$
    – Tbw
    Mar 1 at 20:48
  • \$\begingroup\$ Note deal was removed after this was made. Also, you can save a byte by changing 16 to infinity, as in my solution. \$\endgroup\$
    – noodle man
    Mar 24 at 17:01
  • \$\begingroup\$ @noodleman Good thinking, thanks. \$\endgroup\$
    – chunes
    Mar 25 at 0:08
3
\$\begingroup\$

Easyfuck, 50 49 48 47 43 bytes

¡ã$SPAÖ¼\úëå8CCH>|ÅókVTSÞ[[NELòçEPAT¬|³␓ô¿U¼~MW[èòEPA␀ÿ

due to lack of unicode representations for c1 control characters, they have been replaced by their superscripted abbreviations

Decompressed:

a(<2$[-<.>]>)b(>>1>6\+<[-a>.<]),>,b?%}+<?%+>GJ.

Explanation:

a( )b( ) declares functions a and b

,>,b takes input into 1st and 2nd cell, then executes b

>>1>6\+< sets cell 3 to 16 and cell 4 to 10

[-a>.<] 16 times executes a and prints a new line

<2$[-<.>]> prints second input 32 times

?%}+<?%+> sets cells 3 and 4 to random numbers in the correct range

GJ. moves the cursor and prints first input

This one is shorter but may be breaking the rules as it generates the rectangle, moves the cursor to a random spot on it, and from there asks for input, immediately outputting it.

a(<2$[-<.>]>)b(>>1>6\+<[-a>.<]),b?%}+<?%+>G,.

I'd greatly appreciate it if OP could discern the illegalness of the second solution.

\$\endgroup\$
2
\$\begingroup\$

Uiua, 16 15 bytes

↯∞_32⊏=⌊×⚂⟜⇡512

Try it

Explained:

↯∞_32⊏=⌊×⚂⟜⇡512­⁡​‎‎⁡⁠⁤⁢‏⁠‎⁡⁠⁤⁣‏⁠‎⁡⁠⁤⁤‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁣⁤‏⁠‎⁡⁠⁤⁡‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁣⁡‏⁠‎⁡⁠⁣⁢‏⁠‎⁡⁠⁣⁣‏‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁢⁤‏‏​⁡⁠⁡‌⁢⁡​‎‎⁡⁠⁢⁣‏‏​⁡⁠⁡‌⁢⁢​‎‎⁡⁠⁡‏⁠‎⁡⁠⁢‏⁠‎⁡⁠⁣‏⁠‎⁡⁠⁤‏⁠‎⁡⁠⁢⁡‏⁠‎⁡⁠⁢⁢‏‏​⁡⁠⁡‌­
             512  # ‎⁡Push 512
          ⟜⇡     # ‎⁢Push the range [0, 512) behind it
       ⌊×⚂        # ‎⁣Multiply 512 by a random float and round down
      =           # ‎⁤For each in the range, a 1 if it’s equal to this number and 0 otherwise
     ⊏            # ‎⁢⁡Index into the input with this
↯∞_32             # ‎⁢⁢Reshape this list to have 32 equal columns
💎

Created with the help of Luminespire.

Uiua, 17 bytes

↯16_32⊏=0⍏[⍥⚂512]

Try it online

This was my first attempt at the problem, but I ended up saving a byte with a slightly different method.

Explained:

↯16_32⊏=0⍏[⍥⚂512]­⁡​‎‎⁡⁠⁣⁣‏⁠‎⁡⁠⁣⁤‏⁠‎⁡⁠⁤⁡‏⁠‎⁡⁠⁤⁢‏⁠‎⁡⁠⁤⁣‏⁠‎⁡⁠⁤⁤‏⁠‎⁡⁠⁢⁡⁡‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁣⁢‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁢⁤‏⁠‎⁡⁠⁣⁡‏‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁢⁣‏‏​⁡⁠⁡‌⁢⁡​‎‎⁡⁠⁡‏⁠‎⁡⁠⁢‏⁠‎⁡⁠⁣‏⁠‎⁡⁠⁤‏⁠‎⁡⁠⁢⁡‏⁠‎⁡⁠⁢⁢‏‏​⁡⁠⁡‌­
          [⍥⚂512]  # ‎⁡512 random floats
         ⍏         # ‎⁢Indices of this sorted
       =0          # ‎⁣Is each equal to 0? (Leaves a 1 in a random position and 0 everywhere else)
      ⊏            # ‎⁤Index into the input with this
↯16_32             # ‎⁢⁡Reshape list to 16*32
💎

Created with the help of Luminespire.

\$\endgroup\$
2
\$\begingroup\$

05AB1E, 11 bytes

žA<׫.ržwô»

Try it online!

How?

žA<׫.ržwô»             
   ×                    String of first input
žA<                        of length 511
    «                   Append second input
     .r                 Shuffle
       žwô              Split into chuncks of 32
          »             Join by new lines
\$\endgroup\$
2
\$\begingroup\$

Charcoal, 15 bytes

UO³²⊗⁸SJ‽³²‽¹⁶S

Try it online! Link is to verbose version of code. Takes the decoy as the first input and the target as the second input. Explanation:

UO³²⊗⁸S

Fill a 32×16 oblong grid with the first input.

J‽³²‽¹⁶

Jump to a random cell.

Overwrite it with the second input.

\$\endgroup\$
2
\$\begingroup\$

MathGolf, 11 bytes

•r_wm=§y♥/n

Inputs as a pair \$[fillterChar,randomChar]\$.

Try it online.

Explanation:

•r          # Push a list in the range [0,512)
  _         # Duplicate this list
   w        # Pop and push a random item from this list
    m       # Map over the list with this random item as argument
     =      #  Check which item equals this argument
            # (we now have a list of 511 0s and a random 1)
      §     # (0-based) index those into the (implicit) input-pair characters
       y    # Join this list of characters to a string
        ♥/  # Split it into parts of size 32
          n # Join by newlines
            # (after which the entire stack is output implicitly as result)
\$\endgroup\$
2
\$\begingroup\$

Jelly, 10 bytes

x511;Ẋs32Y

Try it online!

I'm revisiting this language after several years and I kinda want to write a tool that translates something like (Y (s (Ẋ (; (x λ 511) ρ)) 32)) into the above.

12 bytes

2*511BẊṃs32Y

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Go (1.19+), 138137 bytes

import."math/rand"
type S=string
func f(a S,b S){p:=Intn(512)
for i:=0;i<512;i++{if i==p{print(b)}else{print(a)}
if i%32==31{println()}}}

Try it online!

Note: On the Tio platform you will get the same output each time. This is because it is using an older version of Go that do not auto seed the RNG which was introduced in 2022. See the Go Playground for a working (but force-ungolfed) example.

\$\endgroup\$
5
  • \$\begingroup\$ I'm getting the same output when run each time. Perhaps your code needs to seed the random number generator such as this example. Thanks! \$\endgroup\$
    – vengy
    Feb 29 at 14:22
  • \$\begingroup\$ I noticed that too! It shows different number on my machine and AFAIK go by default seeds the RNG differently every time. I think it is an issue of the Tio runtime. \$\endgroup\$ Feb 29 at 14:27
  • \$\begingroup\$ I've never coded in GO, but p:=Intn(512) is always outputting 33 as seen in this example \$\endgroup\$
    – vengy
    Feb 29 at 14:55
  • 1
    \$\begingroup\$ If you run it on the official Go playground you should get different numbers. I should have addressed this in the answer. I delved a bit more into this it seems Go set the random seed automatically since this, maybe tio is running an older version of Go. I will update my answer to explain this \$\endgroup\$ Feb 29 at 15:05
  • \$\begingroup\$ The TIO Go version is go1.11.13 \$\endgroup\$
    – vengy
    Feb 29 at 17:58
2
\$\begingroup\$

Jelly, 9 bytes

⁹ḤṬẊịs32Y

A monadic Link that accepts a pair of characters, [Target, Decoy], and yields a list of characters.

Try it online!

How?

⁹ḤṬẊịs32Y - Link: pair of characters, TD
⁹         - 256
 Ḥ        - double -> 512
  Ṭ       - untruth -> [0,0,...,0,1] (511 zeros, 1 one)
   Ẋ      - shuffle
    ị     - index into {TD} (1:T; 0:D)
     s32  - split into chunks of length 32
        Y - join with newline characters
\$\endgroup\$
2
\$\begingroup\$

Scala 3, 85 bytes

A port of @Nicola Sap's Python answer in Scala.


Golfed version. Attempt This Online!

(t,d)=>scala.util.Random.shuffle((t+d*511).toSeq).mkString.grouped(32).mkString("\n")

Ungolfed version. Attempt This Online!

object Main {
  def main(args: Array[String]): Unit = {
    val t = "0"
    val d = "O"
    println(f(t, d))
  }

  def f(t: String, d: String): String = {
    // Create a long string by concatenating `t` with `d` repeated 511 times
    val longString = t + d * 511
    
    // Convert the string to a sequence of characters, shuffle it, and convert back to string
    val shuffledString = scala.util.Random.shuffle(longString.toSeq).mkString
    
    // Split the shuffled string into lines of 32 characters each and join them with newline characters
    wrapText(shuffledString, 32)
  }

  def wrapText(text: String, lineLength: Int): String = {
    // Split the text into lines of `lineLength` characters
    text.grouped(lineLength).mkString("\n")
  }
}
\$\endgroup\$
2
\$\begingroup\$

AWK, 76 71 bytes

srand(){for(r=int(rand()*512);i<512;++i%32?b:a=a"\n")a=r-i?a$2:a$1}$0=a

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ This is a little bit shorter, but still uses the same algorithm srand(){for(r=int(rand()*512);i<512;++i%32?b:a=a"\n")a=r-i?a$2:a$1}$0=a \$\endgroup\$
    – cnamejj
    Mar 3 at 1:37
2
\$\begingroup\$

Swift 5.9, 138 130 105 bytes

let h={let s=([$0+""]+{[_].init}()($1,511)).shuffled()
(0...15).map{print(s[$0*32..<$0*32+32].joined())}}

The first argument to h(_:_:) is the "decoy", and the second is the "target".

Try It Online doesn't support a modern enough version of Swift, and Attempt This Online doesn't support Swift at all as of yet. So, here's a link to something entirely different (JDoodle) that'll still get the job done.

Whitespace restored:

let h = {
  let s = ([$0 + ""] + { [_].init }()($1, 511)).shuffled()
  (0...15).map {
    print(s[($0 * 32)..<($0 * 32 + 32)].joined())
  }
}

I'll run through this step-by-step.

let h = { /* ... */ }

Function signatures in Swift are long. So I decided to assign a closure to a variable instead. I was able to infer the types of both arguments and the return value (which is just () anyway, this saved one byte).

let s = ([$0 + ""] + { [_].init }()($1, 511)).shuffled()

There's a lot to swallow here, so I'll go inside out.

  • [_].init is inferred as [String].init; this saves two bytes over Array.init.

  • We toss the initializer in a closure and immediately call it, giving us the same initializer, but with one important difference: all argument labels are removed. This is especially handy for the bulky .init(repeating:count:) initializer.

  • We then call this new label-less initializer with two arguments:

    • the value to repeat (the "target" value, referred to as $1 thanks to implicit parameters)
    • the number of times to repeat it (512 - 1)
  • We prepend (or append, it doesn't matter) an array literal containing one value -- the "decoy". This gives us a 512-element array, containing 511 "targets" and 1 "decoy".

    Notice the trick here with $0 + "" -- appending an empty string gives the type checker a hint that the type of $0 is String, which, combined with other inferences, allows us to drop type signatures entirely.

  • Finally, we call the shuffled() method on this mess, giving us an identical array, except that the "decoy" is now somewhere else. (I'm trusting that Swift's SystemRandomNumberGenerator is good enough for this challenge.)

(0...15).map { /* ... */ }

We create a ClosedRange from 0 to 15 and iterate over it. map is four bytes shorter than forEach, and it behaves identically, with the exception that it'll generate a warning if you don't use the return value (which doesn't exist here anyway).

print(s[($0 * 32)..<($0 * 32 + 32)].joined())

What this basically does is iterate over s in 32-element chunks. For some reason, Swift still doesn't have some sort of chunk(into:) method in the standard library, and implementing one would be overkill for this challenge, so I'm just doing a bit of math instead.

We join the chunks into strings and print them. By default, Swift's print(_:separator:terminator:) function prints a trailing newline, which is exactly what we want here.

\$\endgroup\$
0
2
\$\begingroup\$

VBA Excel, 43 54 bytes

[b1:ag16]=[a1]:cells(int(rnd*16+1),int(rnd*32+2))=[a2]
  • Input the values target and decoy into the cells A1:A2
  • Run the code in immediate window

enter image description here

\$\endgroup\$
3
  • 2
    \$\begingroup\$ Just curious, does your random method (pick a random row, then a random cell within that row) agree with each cell having a \$\frac{1}{512}\$ chance of being chosen? Probability was never a strong point. Thanks. \$\endgroup\$
    – vengy
    Mar 23 at 18:10
  • 1
    \$\begingroup\$ Looks like it's okay: P(specific cell) = P(row) \$\times\$ P(cell in row) \$ = \frac{1}{16} \times \frac{1}{32} = \frac{1}{512}\$ \$\endgroup\$
    – vengy
    Mar 23 at 18:21
  • 4
    \$\begingroup\$ @vengy It is a very good point. I have just run 100000 cycles and I have found out that the columns B and Q and rows 1 and 16 are populated about 4 times less frequently when I use the formula cells(rnd*31+1,rnd*15+2). I will optimize the code. Thanks for paying attention. \$\endgroup\$ Mar 23 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.