577
\$\begingroup\$

This "sandbox" is a place where Code Golf users can get feedback on prospective challenges they wish to post to main. This is useful because writing a clear and fully specified challenge on your first try can be difficult, and there is a much better chance of your challenge being well received if you post it in the sandbox first.

Sandbox FAQ

Posting

To post to the sandbox, scroll to the bottom of this page and click "Answer This Question". Click "OK" when it asks if you really want to add another answer.

Write your challenge just as you would when actually posting it, though you can optionally add a title at the top. You may also add some notes about specific things you would like to clarify before posting it. Other users will help you improve your challenge by rating and discussing it.

When you think your challenge is ready for the public, go ahead and post it, and replace the post here with a link to the challenge and delete the sandbox post.

Discussion

The purpose of the sandbox is to give and receive feedback on posts. If you want to, feel free to give feedback to any posts you see here. Important things to comment about can include:

  • Parts of the challenge you found unclear
  • Comments addressing specific points mentioned in the proposal
  • Problems that could make the challenge uninteresting or unfit for the site

You don't need any qualifications to review sandbox posts. The target audience of most of these challenges is code golfers like you, so anything you find unclear will probably be unclear to others.

If you think one of your posts requires more feedback, but it's been ignored, you can ask for feedback in The Nineteenth Byte. It's not only allowed, but highly recommended! Be patient and try not to nag people though, you might have to ask multiple times.

It is recommended to leave your posts in the sandbox for at least several days, and until it receives upvotes and any feedback has been addressed.

Other

Search the sandbox / Browse your pending proposals

The sandbox works best if you sort posts by active.

To add an inline tag to a proposal, use shortcut link syntax with a prefix: [tag:king-of-the-hill]. To search for posts with a certain tag, include the name in quotes: "king-of-the-hill".

\$\endgroup\$
2
  • \$\begingroup\$ What if I posted on the sandbox a long time ago and get no response? \$\endgroup\$
    – None1
    Commented May 15 at 14:05
  • \$\begingroup\$ @None1 If you don't get feedback for a while you can ask in the nineteenth byte \$\endgroup\$ Commented May 29 at 13:27

4832 Answers 4832

1
86 87
88
89 90
162
1
\$\begingroup\$

Print every finite string

In this challenge, you're given the task to create a full program that generates and prints strings indefinitely. The strings must be printed on separate lines, and every finite string must (theoretically) be printed in finite time.

Rules

  • Your submission must be a full program, not a function.
  • You must output the strings to STDOUT.
  • There should be a single line feed between each string.
  • A string is a sequence of printable ASCII characters (0x20-0x7e, inclusive). You may also include any number of null-terminators (0x0), EOFs (0xff) or other zero-space characters.
  • The algorithm used must theoretically print any finite string in finite time. In practice, however, your program may fail after having printed at least 65535 strings.
  • No string can be printed twice. Zero-space characters don't count as part of the string, so the program cannot output both Hello and Hello(EOF).
  • The empty string may or may not be generated.

Valid strings:

Hello
AAAA
(space)
Hello(EOF)
Hello, World!
"#(){ :|~A

Invalid strings:

(tab)
(newline)
ë
ñ
ø
π

This is a , so the shortest answer in each language wins!

\$\endgroup\$
4
  • \$\begingroup\$ I've posted a similar challenge Output all strings though it takes the character set as an input \$\endgroup\$
    – xnor
    Commented Jun 8, 2023 at 19:51
  • \$\begingroup\$ Does the program have to be deterministic? Or, Is it valid that for any string my program will output it with a probability of 1? The second one is simply repeat forever { s=generate random string; if (s not yet printed) { print(s) } }. \$\endgroup\$
    – tsh
    Commented Jun 9, 2023 at 6:36
  • \$\begingroup\$ @xnor I didn't know of that challenge, thanks for notifying me :) I don't think I'll post this one, considering how similar it is to yours. \$\endgroup\$
    – Peter
    Commented Jun 9, 2023 at 7:55
  • \$\begingroup\$ @tsh If all submissions had to be deterministic, they could just set the seed for functions generating random things, so I don't think I'll impose such restriction. \$\endgroup\$
    – Peter
    Commented Jun 9, 2023 at 7:57
1
\$\begingroup\$

Output every second

\$\endgroup\$
2
  • \$\begingroup\$ time is a synonym for date, so maybe not what you want here. I'd also be unsurprised if this is a duplicate, though I'm not certain off the top of my head. \$\endgroup\$
    – Bbrk24
    Commented Jun 21, 2023 at 20:04
  • \$\begingroup\$ @Bbrk24 The tag says "This challenge is intended to be solved using [...] clock times", so it should still be fine (maybe). If there are any other tags you think belong on this len be know. I searched, and didn't find any others that are the same. \$\endgroup\$
    – Dadsdy
    Commented Jun 21, 2023 at 20:54
1
\$\begingroup\$

Counting black and white piano keys

Given a major or minor triad (3 note chord) name, return the amount of black and white piano keys needed to play it. (I will add more explanation later)


Little bit of music theory

A piano contains 88 keys: 52 whites and 36 blacks. The keys are divided into octaves which have 7 white keys and 5 black. The notes represented by the white keys go from A to G (although the starting note in an octave is C; notes from left to right are as following: C, D, E, F, G, A, B). The notes represented by the black keys are accidentals of the white keys. An accidental is either a sharp note (#) or a flat note (b). (There is an exception since there is only 5 black keys and 7 white keys, Should I expand on this?) . A sharp note is one semitone higher than the same note (C# is one semitone higher than C). A flat note is one semitone lower (Db is one semitone lower than D). There is one full tone in between notes except for E-F and B-C where there is a semitone instead and one semitone between keys.

I'll keep adding more details if needed

Note: For this challenge, we don't care which octave the triad is played on since it will be the same answer for any octave.

Example

Cmaj  = 3 white, 0 black 
Fmin  = 2 white, 1 black
G#maj = 1 white, 2 black
Gbmaj = 0 white, 3 black

Input can be the chord name or can be divided into 2 variables: Root (C, F, G#, Gb) and quality (maj, min). I will extend on music theory if this is accepted as a good challenge

Output must be an array of 2 position where the first one is the number of white keys and the second one the number of black keys


Has this been asked before?

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Looks like a nice challenge idea. But, as you say in your post, this definitely needs more music theory to explain what the major/minor triad names really mean. PS: Welcome back! \$\endgroup\$
    – Arnauld
    Commented Jun 29, 2023 at 13:21
  • \$\begingroup\$ @Arnauld An usual explaination is just [0,3,7] and [0,4,7], which is enough here \$\endgroup\$
    – l4m2
    Commented Jul 3, 2023 at 11:46
  • \$\begingroup\$ @Arnauld thanks mate! Good to be back. I have to research more into this challenge (I know music theory but I dont know how to explain it very well) therefore I'll leave here until my inspiration comes back. I have another idea tho I'll be posting soon! \$\endgroup\$ Commented Jul 12, 2023 at 19:15
1
\$\begingroup\$

Draw the initial positions of Mölkky pins in ASCII Art

\$\endgroup\$
5
  • 1
    \$\begingroup\$ I'm afraid this won't lead to many creative answers. Even with a short clever formula generating the pin numbers in the correct order, the cost of applying the correct formatting is very likely to give something longer than just hardcoding the output (for standard languages) or using a built-in string compression (for esolangs). \$\endgroup\$
    – Arnauld
    Commented Jun 29, 2023 at 14:08
  • \$\begingroup\$ @Arnauld Sadly i found the same conclusion while trying to code it. For standard languages, would it be more interesting if i discouraged hardcoding by discouraging using too many digits, for example with a scoring system? (for esolangs, i think it's part of the game that anything goes and i shouldn't try to oppose it) \$\endgroup\$
    – Fhuvi
    Commented Jun 29, 2023 at 15:00
  • 1
    \$\begingroup\$ This is usually a bad idea. Maybe it would be better to turn this into a slightly more complex ASCII art, giving more compression opportunities. Here is a quick attempt. \$\endgroup\$
    – Arnauld
    Commented Jun 29, 2023 at 15:25
  • 1
    \$\begingroup\$ Alternate version \$\endgroup\$
    – Arnauld
    Commented Jun 29, 2023 at 15:28
  • 1
    \$\begingroup\$ @Arnauld Thank you for your input. I've changed the challenge with your alternate version. I still hope that this would allow some clever and competitive formulas for the pins numbers! \$\endgroup\$
    – Fhuvi
    Commented Jun 29, 2023 at 16:01
1
\$\begingroup\$

Critical Calculations

\$\endgroup\$
2
  • \$\begingroup\$ Computing the damage per ring instead of just the total damage seems like an unnecessary transformation, perhaps allow both? \$\endgroup\$ Commented Jul 7, 2023 at 6:24
  • \$\begingroup\$ @CommandMaster Fair point, I think I will allow both \$\endgroup\$ Commented Jul 7, 2023 at 14:23
1
\$\begingroup\$

Landmine Number IV

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

Tic-Tac-Toe: Maintain the Draw!

Tic-Tac-Toe is a game for two players who take turns marking the spaces in a 3x3 grid with an X or an O. If a player succeeds in placing three of their marks in a horizontal, vertical or diagonal line they are the winner; otherwise, the game is considered drawn.

Challenge

You have been chosen to represent the human race in a game of Tic-Tac-Toe against the AIs. Dauntingly, your opponent is a programme which always finds the optimal move in a given position. Worse still, you will be playing second. The only consolation is that you are required to survive for one move only: that is, you must make a single move in reply to the computer's opening gambit which leads to a position which is not theoretically lost. If you find such a move, humanity lives to fight another day; if not, ...

Assuming a Tic-Tac-Toe board with the following notation

1 2 3
4 5 6
7 8 9

given the AI's opening move output all possible replies which do not lead to a theoretically lost position.

Input

An integer \$n\$ (where \$1<=n<=9\$) which represents the machine's opening move as per the given board notation.

Output

A sorted list, array, etc. of integers which represents all possible solutions.

Explained Cases

Input => Output

1 => [5]
The only reply to a corner-square opening move which does not lead to a theoretical loss
is that which occupies the central square.

2 => [1, 3, 5, 8]

The only replies to an edge-square opening move which do not lead to a theoretical loss
are those which occupy the same row or column as that of your opponent's move.

5 => [1, 3, 7, 9]

The only replies to a central-square opening move which do not lead to a theoretical
loss are those which occupy one of the four corner squares.

Test Cases

Input => Output

1 => [5]
2 => [1, 3, 5, 8]
3 => [5]
4 => [1, 5, 6, 7]
5 => [1, 3, 7, 9]

This is , so shortest answer in bytes wins!

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

Tableing in 3 operations

There was a puzzle requiring to convert EBCDIC into ASCII in 4 operations. Actually, if big integers are allowed, 3 mathematical operations(so no bit shift, integer division, etc.) is enough, even for more complex mapping.

Given an array \$\left[x_1,x_2,x_3,...,x_n\right]\$, generate arguments \$a,b,c\$, such that \$ \forall 0 < k \le n, a \mod \left(bk+c\right) = x_k\$.

Fastest algorithm wins. Here word RAM model is used: bitwidth is by default \$\text O \left (\log \max\left\{ x_k, n\right\} \right)\$. You can cost \$2^t\$ time to extend the bitwidth to \$\text O\left(t\right)\$ without extra code. BigInts are treated as multiple ints, and take longer time to process larger values.

Sandbox Notes:

\$\endgroup\$
1
  • \$\begingroup\$ 1. Sandbo xNotes should be Sandbox Notes. 2. Your challenge needs a title 3. This might be confusing to people without a mathematical background, so you should probably explain it in plain English. \$\endgroup\$ Commented Jul 16, 2023 at 11:18
1
\$\begingroup\$

Print all polynomials

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

WumpusWars King of the Hill

This is probably going to stay in the sandbox for a few weeks, until I get the time to write the actual scoring code up... Yep, I'm both lazy and busy :P I'm not very good at coding challenges but I'm good at thinking up creative ideas, so this time my idea is to try reviving the genre of KotH, as the last one on this site (Crazyhouse Chess) has been very unsuccessful (0 submissions for pretty obvious reasons) and the others are all many months old...

Inspired by Hunt the Wumpus, an early command line game. It is somewhat popular among esolang communities as a programming challenge, especially for languages like Befunge. However, the original, singleplayer version is boring, so I wanna upgrade it. Hence I created WumpusWars, a better version of it!

Story: You are a brave hunter in a dark cave, trying to kill the Wumpus, a mysterious monster. You start with 5 arrows that can kill the Wumpus if you shoot the arrow into the same room as the Wumpus. The cave consists of large rooms and small passageways in between. It can be represented, hence, with a graph, so that the rooms are nodes/vertices and the passageways are edges. There are a total of 50 vertices in the graph, each of them having a degree of at least 3, and the graph is guaranteed to be connected. Rooms are numbered from 0 to 49. You are spawned in a random room.

The place is so dark that you cannot see anything, but you can feel, smell, and hear. Hence you have access to the functions listed in the source code below. Lemme explain what the functions are associated with in more detail:

  1. Bats: There are 5 rooms in total that contain bats. Entering one of them will cause you to be carried away to a random empty room ("empty room" defined as a room without bats, pits, players, or the wumpus, but there can be corpses in the room).
  2. Pits: There are 5 rooms in total that contain a pit. Entering one of them will cause you to BOOM! CLASH! CRACK!
  3. Wumpus: It remains in its own room unless either an arrow is shot by anyone anywhere, or the game is in deathmatch mode (see gamemode list for explanation). If it enters your room then... You know what. When an arrow is shot or in deathmatch mode it moves randomly to adjacent rooms. Bats and pits do not affect it.
  4. Players: Other players. Note that you cannot shoot a player in the same room as you because the arrow is magically explosive and shooting in the same room as your own will cause you to BOOM! CLASH! CRACK!
  5. Corpse: When a player is killed by anything except a pit they become a corpse, and corpses don't move (obviously). You can collect arrows from the corpses, and if the corpse has already been arrow-collected there will be 0 arrows on it.

Also, every 10 rounds an arrow will spawn in an empty room. Entering that room will cause your number of arrows to be incremented by 1. Multiple arrows can spawn in the same room which gives you several at once, but that's pretty rare.

You need to write a player program in Python and submit it as an answer to the post. Use the "PlayerName" class template. You will have to define with the participate function which gamemodes are you participating in (the input to the function will be one of the abbreviation strings):

  1. Singleplayer (abbrev: "SP"). Precisely what the name says. Your score is the percentage of games in which you win (by shooting the Wumpus with an arrow). You lose if you die. After 100 rounds the game enters deathmatch mode, where the Wumpus keeps moving randomly every round regardless of whether an arrow is shot.
  2. Player vs Environment (abbrev: "PVE"). Same as singleplayer except that there are multiple players. The winner is the one who kills the Wumpus (if multiple players shoot it in the same round they split the point equally). If everyone dies then no one wins. Arrows have no effect on players. Deathmatch starts after 20*n rounds, where n is the number of players.
  3. Player vs Player (abbrev: "PVP"). Kinda PVE inverted. The Wumpus is now immortal –– but the players are not! If you get shot by other players or get eaten by the Wumpus you die and lose. The last one standing is the winner. If multiple people die last in the same round they split the point equally. It's deathmatch from start to finish.
  4. Among Us (abbrev: "IP" for Imposter and "CM" for Crewmate). The Wumpus is immortal. Some players become impostors. Others are crewmates. Impostors get 3 arrows at the beginning (number might change according to # of players). Crewmates start with no arrows (but can still pick randomly spawned arrows up). Crewmates are immune from arrows fired by other crewmates. If all impostors die then all crewmates (dead or alive) win a point. If all crewmates die then all impostors (dead or alive) win a point. No deathmatch.

If you have any ideas please lemme know!

Source code:

class Environment:
    def __init__():
        # Hidden
    def legal_moves(player) -> list:
        # Returns an ordered list of adjacent rooms
        # Example: [5, 8, 10]
    def bats(player) -> bool:
        # True if bats are in adjacent rooms, false otherwise
    def pits(player) -> bool:
        # True if pits are in adjacent rooms, false otherwise
    def wumpus(player) -> bool:
        # True if wumpus is in adjacent rooms, false otherwise
    def others(player) -> bool:
        # True if the shortest path from you to the nearest player passes at most 2 edges.
    def arrows(player) -> int:
        # Returns the number of arrows player has
    def corpse(player) -> list:
        # Returns a list of integers
        # The length of the list is the number of corpses in the player's room
        # Elements in the list are the number of arrows on each corpse
    # Other functions hidden

Template:

class PlayerName: # Change the name!
    def __init__(mode:str, room:int, env):
        # Gamemode. Such as 'SP'.
        self.mode = mode
        # Current room number.
        self.room = room
        # Environment
        self.env = env
    def participate() -> bool:
        # Add code here...
        
        # Return True if self.mode is a supported gamemode
        # False if not
    def action() -> tuple:
        legal_moves = self.env.legal_moves(self)
        bats = self.env.bats(self)
        pits = self.env.pits(self)
        wumpus = self.env.wumpus(self)
        others = self.env.others(self)
        arrows = self.env.arrows(self)
        corpse = self.env.corpse(self)

        # No methods or variables from Environment class allowed below here!
        # Suggested to first check which gamemode and use strats accordingly.

        # Add code here...

        # Return a tuple of type (bool, int)
        # The bool is True for shooting, False for moving
        # The int is the cave number to shoot or move to
        # An illegal move signifies doing nothing
```
\$\endgroup\$
1
\$\begingroup\$

Write a Turing machine that draws this fractal

This challenge is inspired by and similar to my other challange about a fractal matrix, but uses a slightly different matrix (for n up to 3 the matrices are nearly the same, but for n >= 4 they start to differ more significantly)


A 2D-Turing machine is like a regular Turing Machine but the memory tape is 2 dimensional, meaning that after each step you can in addition to moving left or right also move up or down.

[...]

Your task is to create a 2 dimensional Turing machine that prints the infinite fractal matrix generated by the following procedure:

  • Start with a single cell, set it to 1
  • repeat n times:
  1. add a copy of the previous matrix to the left of the previous matrix
  2. add a copy of the previous matrix below the previous matrix
  3. add a copy of the previous matrix with 1 and 0 swapped, diagonally to the bottom right of the previous matrix.

First few steps:

n=0:

1

n=1:

1 1
1 0

n=2:

1 1 1 1
1 0 1 0
1 1 0 0
1 0 0 1

n=3:

1 1 1 1 1 1 1 1
1 0 1 0 1 0 1 0
1 1 0 0 1 1 0 0
1 0 0 1 1 0 0 1
1 1 1 1 0 0 0 0
1 0 1 0 0 1 0 1
1 1 0 0 0 0 1 1
1 0 0 1 0 1 1 0

n=4:

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0
1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1
1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0
1 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1
1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1
1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0
1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0
1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1
1 1 0 0 1 1 0 0 0 0 1 1 0 0 1 1
1 0 0 1 1 0 0 1 0 1 1 0 0 1 1 0
1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1
1 0 1 0 0 1 0 1 0 1 0 1 1 0 1 0
1 1 0 0 0 0 1 1 0 0 1 1 1 1 0 0
1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1

Rules:

  • It is allowed to output/display any two (distinguishable) distinct cells values for the two different values in the matrix

  • Cells inside the matrix have to have the correct value more that 50% of the time (\$\lim_{steps \to \infty} \frac{correct}{steps} >0.5\$)

  • Cells outside the matrix should be empty more that 50% of the time.

  • The score of an answer is the number of distinct cell values used times the number of states of the Turing machine

  • This is the solution with the lowest score wins

related: generate the matrix


Meta:

  • Is this a duplicate?

  • Is my explanation clear?

  • Should I link to an example implementation of a 2D Turing machine?

\$\endgroup\$
2
  • \$\begingroup\$ I feel like saying "it differs for n>=4" suggests you might want an example for n = 4 at least :) \$\endgroup\$
    – RubenVerg
    Commented Jul 22, 2023 at 18:06
  • \$\begingroup\$ Duplicate? \$\endgroup\$
    – alephalpha
    Commented Jul 28, 2023 at 7:43
1
\$\begingroup\$

Pack the sequence!

You have a list of sequences of numbers (1 byte max, unsigned) as an input, e.g.:

4 6 4 9 3 15 150
3 2 1
9 6 4 1
…

Your task is to pack as many of them as possible into the square composed from the numbers from this list - the size of square is also an input.

The square of size 4 may look like:

10 13 56 30
98 11 10  4
15 36 77 86
 1  1 45 11

The numbers may repeat as many times as you want.

The sequence is considered "packed" if you can draw the line from the first number inside square to the next adjacent number in sequence and so on. Each number may be used only once for a single sequence.

For example, the sequence 36 11 77 11 is packed into the square above - the number may have up to 8 adjacent numbers: top, left, right, bottom and diagonals. While the sequence 11 36 10 11 is not - since the same number 11 can't be used twice.

Scoring

For the packed sequence of size S you get 2^S scores. Each sequence counts only once - even if you can pack it multiple times in the same square.

Victory Condition

The algorithm wins if it outputs the square of expected size with the maximum scoring for some hidden test lists. The algorithm is expected to be general enough to work with any random list.

Output

Program should output square as a sequence of numbers on a single line. For example: 1 2 3 4 5 6 7 8 9 is the square of size 3:

1 2 3
4 5 6
7 8 9

Limitations

  • Any list contains [1, 1000000] sequences.
  • Any sequence contains [N, 20] numbers, where N is size of square.
  • The size of square N is [2, 10].
  • Program should use no more than 1Gb of user-space memory.
  • Program should work no more than 60 seconds and use a single thread.
  • Program may output more than one square - one per line. The last full square after 60 seconds is considered the answer.
\$\endgroup\$
2
  • \$\begingroup\$ It's too likely that no two number is same, and best solution is trivial \$\endgroup\$
    – l4m2
    Commented Aug 1, 2023 at 6:07
  • \$\begingroup\$ How is it likely? Numbers are limited in [0, 255]. Max possible 1000000*20 = 2'000'000 numbers in the list - they are definitely likely to be the same. Also in some test scenarios specifically numbers can be tuned to be in much smaller interval like [0, 9] only. \$\endgroup\$
    – abyss.7
    Commented Aug 1, 2023 at 7:46
1
\$\begingroup\$

The New York Times have a new game -

Letter Boxed

(try it at https://www.nytimes.com/puzzles/letter-boxed, the rules below match it).

The game consists of a square, with three Letters on each side. The aim of the game is to join the letters to make words. Each word must start with the last letter of the previous word; and each letter must not share a side of the square with the previous letter. The aim is to solve (use all of the letters at least once) in as few words as possible.

For example:

  H  E  M

O         U

S         R

W         N

  A  I  G

You could use all of the letters in one go to form the word Housewarming, but Swim would be forbidden because the S and the W are consecutive and share a side on the square.

Additionally, HOUSE followed by ERGO would be allowed, but HOUSE followed by WARM would not because they don't share a last/first letter (E->W).

Letters can be repeated (e.g. WEARER), but not sequentially (so SEEN is forbidden), because they share a side.

The challenge

, usual rules.

The aim is to write a program that takes in the set of letters in any reasonably form (a string, or an array of arrays, for example); along with a word list (or you can read the word list in your code for free).

The output should be a word of sequential list of words that solve the puzzle by using all of the letters at least once.

The puzzle will always be solvable for the letters and word list given, but not always in one word.

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

Challenge

Create a program that, when entered two numbers A as a numerator and B as a denominator, generates a list of equivalent fractions based off the fraction A/B.

Definition

Equivalent fractions are any set of fractions that equal the same when converted to a decimal number. Example: 3/6 (three sixths), 2/4 (two fourths) and 1/2 (one half) are equivalent fractions, since each fraction equals 0.5 when converted to a decimal number.

Rules

  • Generate only the equivalent fractions with denominators smaller than B. Do not generate equivalent fractions with decimal points or negative numbers.
  • Your output must look like this: (This example is for entering 10 as A and 20 as B)
1/2 2/4 3/6 4/8 5/10 6/12 7/14 8/16 9/18 10/20
  • If you cannot generate equivalent fractions with denominators smaller than B, output A/B itself: (This example is for entering 1 as A and 2 as B)
1/2
  • The answer with the lowest amount of bytes wins.
  • You may use any programming language of your choice.
\$\endgroup\$
4
  • 1
    \$\begingroup\$ Ideally, you should provide all the information needed to participate in the challenge, rather than linking to an external resource (though you may provide links for supplemental materials or further reading). Also, what do you mean by "only the ones smaller than A/B", if they definitionally have the same value? \$\endgroup\$
    – Bbrk24
    Commented Aug 3, 2023 at 18:05
  • \$\begingroup\$ By smaller than "A/B" I meant having lower denominators. Sorry for the mixup \$\endgroup\$
    – boopsie84
    Commented Aug 4, 2023 at 12:23
  • \$\begingroup\$ About the output formatting, I also recommend reading through our I/O defaults. The first couple pages of answers (if you sort by highest score) are pretty much accepted; once you get to the third page it's more controversial, and I think you can safely ignore the answers with score 6 or lower. \$\endgroup\$
    – Bbrk24
    Commented Aug 4, 2023 at 12:49
  • 1
    \$\begingroup\$ You should add examples for the expected output given different inputs. For example: What should the program output for when the Input is 1 2? Can A or B become negative, if yes what happens in that case? \$\endgroup\$
    – bsoelch
    Commented Aug 4, 2023 at 15:04
1
\$\begingroup\$

Comment Out the Comment

I'm back! Lately I've been working on something, but finally I've finished it and can return to CGCC! I'll be continuing the Fast & Golfiest Series soon, and also try to start working on (given I have the time and energy) WumpusWars. For now, a new question as a gift for everyone!

In LaTeX if you type %a, the a would be commented out. But if you type \%a, the a would still be there because the backwards slash converted the meaning of %. And if you type \\%a, the a would be commented out because the first backwards slash converted the meaning of the second one. So do you know what happens to the a if you type \\\\\\\\\\\%\\\%a? (Answer: The a is not commented out).

This is a code golf challenge so shortest code wins: the input is a string which consists exclusively of \ and %, except that at the end there is always %a. Output 1 or True if the a is commented out, and 0 or False otherwise.

\$\endgroup\$
2
  • \$\begingroup\$ Is \a valid?. \$\endgroup\$
    – l4m2
    Commented Aug 12, 2023 at 3:02
  • \$\begingroup\$ @l4m2 Thanks for pointing that out! No. I'll add that a is always preceded by %. \$\endgroup\$ Commented Aug 12, 2023 at 3:03
1
\$\begingroup\$

How Turing complete is you language?

\$\endgroup\$
4
  • \$\begingroup\$ The premise is good, but I don't think the definition of "syntax character" is helpful here. It just moves the problem from languages that have "all TC sets must contain X" to those that have "all TC sets must contain either X or Y". \$\endgroup\$
    – Bubbler
    Commented Aug 14, 2023 at 10:15
  • \$\begingroup\$ @Bubbler Do you think allowing to reuse characters is bad in general (which would make solutions in most normal languages impossible) or do you think I have to improve my definition for which characters I allow to reuse. \$\endgroup\$
    – bsoelch
    Commented Aug 14, 2023 at 11:46
  • \$\begingroup\$ Yes to the former. Arbitrary rules like that tend to not go well, mainly because there are so many different langs and different (often unexpected) ways they work. It isn't that trivial even in the case of Python (you can write nontrivial programs without whitespace). So let's just take the task literally and see what creative stuff people can come up with. \$\endgroup\$
    – Bubbler
    Commented Aug 14, 2023 at 12:39
  • \$\begingroup\$ @Bubble you may be right, I tried to find two disjoint Turing complete sets in Python and the main limiting factors seems to be the letter enot : or \n as I previously expected \$\endgroup\$
    – bsoelch
    Commented Aug 14, 2023 at 12:46
1
\$\begingroup\$

Optimal Duck Game Moves

\$\endgroup\$
1
  • 2
    \$\begingroup\$ I would prefer if the instructions were in the post itself, they aren’t that long. \$\endgroup\$ Commented Aug 6, 2023 at 14:55
1
\$\begingroup\$

The rectangle spanned by two numbers

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

Print every Unicode character (almost)

Print every Unicode character, except U+0000. Any order is allowed.

Specification

Every Unicode character from U+0001 to U+10FFFF, inclusive. Exclude U+D800 to U+DFFF inclusive as apparently these 'surrogates' cause weird behaviour.

To be honest, I don't know much about Unicode so before this gets posted someone will have to fill me in on how Unicode works

\$\endgroup\$
4
  • 2
    \$\begingroup\$ This might be more suitable as a CMC. \$\endgroup\$
    – Adám
    Commented Aug 23, 2023 at 11:53
  • 1
    \$\begingroup\$ Please remove your edit in all caps and/or change it to not be in caps. This just clutters the post. \$\endgroup\$ Commented Aug 23, 2023 at 15:32
  • 4
    \$\begingroup\$ Does this include non-printable (e.g. U+00) / unassigned (e.g. U+D800-U+D8FF) Unicode characters? \$\endgroup\$
    – bsoelch
    Commented Aug 23, 2023 at 17:37
  • \$\begingroup\$ What's a CMC? @Adám || EDIT: nvm found it "chat mini challenge" \$\endgroup\$
    – W D
    Commented Aug 24, 2023 at 6:15
1
\$\begingroup\$

Multiply multivariate polynomials

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

Make a braille cribbage board

The braille characters lie in the range U+2800 () to U+28FF (), making it a SBCS (Single Byte Character Set). Oddly enough, the braille patterns look a lot like holes in a cribbage board. Here is an image of one of the many cribbage boards for reference:

Cribbage Board

So you must print this pattern:

+-------+
|⣀⣀⠀⣿⠀⣀⣀|
|⣿⣿⠀⠀⠀⣿⣿|
|⣿⣿⠀⠀⠀⣿⣿|
|⣿⣿⠀⠀⠀⣿⣿|
|⣿⣿⠀⠀⠀⣿⣿|
|⣿⣿⠀⠀⠀⣿⣿|
|⣿⣿⠀⠀⠀⣿⣿|
|⣿⣿⠀⠀⠀⣿⣿|
|⠉⠉⠀⣿⠀⠉⠉|
+-------+

Note that the blanks are U+2800 and NOT the space.

,? is ,7tag3code-golf7'1 s %orte/ code w9s6

Oh, we're not using grade 2 contracted braille. You're not blind.

This is , so shortest code wins!


\$\endgroup\$
0
1
\$\begingroup\$

Half even rounding

\$\endgroup\$
1
  • \$\begingroup\$ As far as I know, PowerShell and brev also use banker's rounding, and some languages like Jelly that may be similar/based on Python may have a built-in that's a "Python round() function" || also, you may want to specify that this is Python 3 behaviour \$\endgroup\$
    – W D
    Commented Aug 22, 2023 at 9:24
1
\$\begingroup\$

Implement RAID

RAID, or Redundant Array of Independent Disks is a way to efficiently store data in such a way that if any one disk fails you can still recover the data. Instead of implementing the official protocol, any function that can preserve data counts.

Your task is to produce two functions.

The first is the encoder. Given N lists with M elements each, produce N+1 lists with M elements each. What these lists contain is up to you.

The second part is the recovery system. Take as input the lists produced by the encoded, except one is missing. You must be able to recover the original data no matter which of the lists is gone.

Each list should contain only integers between 0 and 255. It is your choice how to represent the empty list, for example you can:

  • Replace it with an empty list
  • Replace it with a non-list value like None
  • Prefix all present lists with a 1 and the missing list with 0.
  • Take as a separate input which list is missing then just zero out the values.

Example

A basic XOR based solution could look like this:

Encoder:

def encode(list_of_lists):
    return [*list_of_lists, [reduce(lambda a,b:a^b, i) for i in zip(*list_of_lists)]]

def decode(list_with_one_removed):
    return [
        (i if i else [reduce(lambda a,b:a^b, j) for j in zip(*(k for k in list_with_one_removed if k))
        for i in list_with_one_removed
    ][:-1]

You don't need to use a XOR based solution, as long as you can extract the original data again your solution is valid.

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

Efficiently solve quadratic equations when 1+1=0

This is a sequel to my previous challenge about solving quadratic equations in the shortest possible code, this challenge is about solving the same task the fastest possible way

The goal of this challenge is to find a solutions for a general quadratic polynomial ax²+bx+c. Meaning be an integer r such that a*r*r+b*b+c is zero, with + meaning exclusive or and * being carry-less multiplication (use xor as addition in binary long multiplication).

To simplify the problem, you can assume that both a and b are one, so you only have to solve equations of the form r*(r+1)=n for an integer n. As any quadratic equation can be converted in this normal form, this is enough to solve the general case (up to linear transformations)

Your challenge will be to solve this simplified equation in the most efficient way possible.

Input: An integer n Output: A number r such that r*(r+1)=n with * being multiplication without carry and + being exclusive or.

Rules:

  • You may assume there is an integral solution to the equation
  • You only have to return one of the two roots (the other root will only differ in the last bit)
  • You program should be able to handle inputs up to at least 0x5555555555555555aaaaaaaaaaaaaaaa
  • The algorithm you are using should work for arbitrarily large n
  • The program with the lowest time complexity in n wins
  • For your score you can assume that xor-addition and xor-multiplication are computed in O(1) (even if your program might need O(n²) operations)
  • If multiple programs have the same complexity, the shortest program (per language) wins

Examples (both possible solutions given):

6 -> 2 | 3
20 -> 4 | 5
18 -> 6 | 7
72 -> 8 | 9
78 -> 10 | 11
...
113427455640312821160607117168492587690 = 0x5555555555555555aaaaaaaaaaaaaaaa -> 0xfffffffffffffffe | 0xffffffffffffffff

Meta

  • Would this be an interesting challenge ?
  • Should I extend the challenge to general polynomials (this can be done in O(1) multiplications/divisions after solving the original problem (or computing the normal square root in some cases))?
  • Is the rule about multiplication&addition do not count towards the complexity clear?
\$\endgroup\$
1
\$\begingroup\$

Golf the fast growing hierarchy

\$\endgroup\$
2
  • \$\begingroup\$ Looks like a really interesting challenge; but you might want to be careful with languages saying "oh but my biggest integer value is [X small number] so I only have to deal with that case" \$\endgroup\$
    – W D
    Commented Aug 22, 2023 at 8:12
  • 2
    \$\begingroup\$ @WD That would abuse the loophole "Abusing native number types to trivialize the challenge". \$\endgroup\$ Commented Sep 3, 2023 at 15:01
1
\$\begingroup\$

Simulate Round Robin Scheduling

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

Sokobunny I

\$\endgroup\$
4
  • 2
    \$\begingroup\$ You should definitely mentioned this is taken from the game Paquerette Down the Bunburrows \$\endgroup\$ Commented Sep 12, 2023 at 19:00
  • \$\begingroup\$ I think it might be good to start with a simpler challenge, like whether a sequence of moves wins, or even which way a bun runs in a single situation. \$\endgroup\$
    – xnor
    Commented Sep 12, 2023 at 21:25
  • \$\begingroup\$ Edited the question, now I think this is going to be simple enough. \$\endgroup\$ Commented Sep 13, 2023 at 0:16
  • \$\begingroup\$ Guarantees: No buns were harmed during the making of this challenge. \$\endgroup\$
    – Philippos
    Commented Sep 14, 2023 at 11:36
1
\$\begingroup\$

Tamagochi

I can't believe there has never been a Tamagochi challenge on code golf! Edit: There has never been a Tamagochi challenge, but there was a Tamagotchi, whatever that is.

Rules:

  • The output must begin with "You found an egg!"
  • Text-adventure style tamagochi.
  • Prints status changes to stdout.
  • Three actions at any moment:
    • Rub/pet
    • Feed
    • Info
  • At the start of the program, you must wait for an egg to hatch.
  • If not fed for 4 minutes, pet moves down a hunger status
  • If fed twice in 4 minutes, pet moves up a hunger status.

Accommodations for golfing languages:

  • The language can't access system time:
    • You now have to implement a command wait x, that waits for x minutes.
    • Each newline advances the time by 30 seconds.
  • The language can't prompt:
    • Read the input from a file, and provide the file you used to test.
  • The language can't read from files:
    • Well, that sucks. Maybe another lang?

Info:

Shows the following:

  • Pet name
  • Pet age
  • Pet hunger
  • Pet happiness

Pet Statuses/behavior:

Initial conditions: Hungry (0 food), Sad (0 pets), Age: 0s

Happy: The pet is happy. Transitions to sad in 3 minutes if no interaction.
Sad: Pet needs a petting within 2 minute or becomes sick. Takes t/15 pats to return to happy, where t is in seconds.

Hungry: Pet needs to be fed within 2 minutes, or becomes sick.
Overfed: Pet needs to not be fed more than twice in 4 minutes, or becomes sick. If currently sick and fed by player, advance death timer by 1 minute.
Fed: Reverses the death timer if not sad or dead.

Old: Pet's age + time not in "Happy" and "Fed" > 1h
Sick: Pet will die in 5 minutes, if status causing sick is not cured. If sick with no status, remove sick in one minute.

If a pet dies, prints name, condition(s) and time.
    "Mr. Johnson died of old age, 1h:23m"
    "Tamagotchi died of sadness 0h:7m"

The smallest Tamagochi wins.

Example

The following is an example simulation, where > could be a prompt, or other input as mentioned in accommodations.

Whenever a pet gains or loses a status, it must output {pet name} is no{w | longer} {status}

Termagotchi
You found an egg!
...
The egg is hatching!
...
The egg has hatched!
What would you like to name your pet?
> Mr. Johnson
Mr. Johnson is now hungry.
Mr. Johnson is now sad.
> Info
Mr Johnson
Age: 13s
Hungry, Sad
> Feed
You fed Mr. Johnson.
> Feed
You fed Mr. Johnson.
Mr. Johnson is now fed.
Mr. Johnson is now happy.
> Pet
You petted Mr. Johnson.
> Pet
You petted Mr. Johnson.
> Pet
You petted Mr. Johnson.
> Pet
You petted Mr. Johnson.
...
Mr. Johnson is now sad.
...
Mr. Johnson is now sick.
> Pet
You petted Mr. Johnson.
> Pet
You petted Mr. Johnson.
> Pet
You petted Mr. Johnson.
> Pet
You petted Mr. Johnson.
Mr. Johnson is now happy.
> Info
Mr Johnson
Age: 14m:27s
Happy, Sick
> Feed
You fed Mr. Johnson.
...
Mr. Johnson is now not sick.
...
> Feed
You fed Mr. Johnson.
> Feed
You fed Mr. Johnson.
> Feed
You fed Mr. Johnson.
Mr. Johnson is now overfed.
> Feed
You fed Mr. Johnson.
> Feed
You fed Mr. Johnson.
Mr. Johnson is now sick.
> Feed
You fed Mr. Johnson.
> Feed
You fed Mr. Johnson.
> Feed
You fed Mr. Johnson.
> Feed
You fed Mr. Johnson.
> Feed
You fed Mr. Johnson.
Mr. Johnson died of overfed, 23m:11s
\$\endgroup\$
8
  • 2
    \$\begingroup\$ This isn’t TikTok, there’s no need to say “unalive”. There’s no special algorithm here (except HNQ); it just shows the most recently modified or answered question first. \$\endgroup\$
    – Bbrk24
    Commented Sep 16, 2023 at 22:22
  • 2
    \$\begingroup\$ Also keep in mind that popcons have been soft-banned here and don’t tend to be seen favorably. \$\endgroup\$
    – Bbrk24
    Commented Sep 16, 2023 at 22:23
  • 3
    \$\begingroup\$ This is heavily underspecified \$\endgroup\$
    – pajonk
    Commented Sep 17, 2023 at 6:17
  • 1
    \$\begingroup\$ Things to avoid when writing challenges: Bonuses in code golf. It's also unclear how any of the rules should be implemented in a program. \$\endgroup\$ Commented Sep 17, 2023 at 9:04
  • 1
    \$\begingroup\$ @pajonk Is this still underspecified? \$\endgroup\$ Commented Sep 17, 2023 at 19:15
  • 1
    \$\begingroup\$ Still pretty underspecified. The introduction ("You found an egg!...") is unspecified, it's unclear whether the ">" prompt is necessary, the initial values for age/hunger/happiness are unspecified, none of the status change outputs are specified. \$\endgroup\$ Commented Sep 17, 2023 at 22:46
  • \$\begingroup\$ Also, the example suggests that languages must have both a way to "prompt" for input during the program's run and access to system time, which excludes certain classes of languages. Not that this is necessarily a bad thing, just something to consider. \$\endgroup\$ Commented Sep 17, 2023 at 22:49
  • \$\begingroup\$ Also, related question \$\endgroup\$ Commented Sep 17, 2023 at 22:53
1
\$\begingroup\$

Fastest decimal to binary

Same challenge, but code golf

Given a decimal number, your task is to convert it to binary.

Test cases

8496867758 -> 111111010011100111110100110101110
28284301455933783441 -> 11000100010000110000001100101000101001000111111110011110110010001
701550526345030865283462565098928586836 -> 1000001111110010011011011101110100010101010100010110011100010101100110100000111111010100100011101000000011010011011100000001010100

Rules

  • You can take input either as a string containing decimal digits, or a list of decimal digits.
  • You need to output the number in binary, either as a string containing the binary digits or a list of binary digits.
  • You are allowed to have leading zeros in the output.
  • The input won't contain leading zeros.

Scoring

Your score is the maximum number of digits your code can handle in under a second on my computer (Intel Core i7-9700 with 8 threads, 32GB RAM). If your code takes less than a second for a number with a million digits, you beat all solutions which can't do that, and the tie-breaker is the time it takes for a million digit number.

To calculate the time I will randomly select 10 numbers, and look at the average time.

Meta question

Is this a dupe? I looked for this question but couldn't find it.

I noticed that using GMP's standard I/O functions this takes 0.05 seconds for a million digit number. Is there a point to this question, or is it likely GMP is optimized enough that this will be the winning solution?

\$\endgroup\$
6
  • \$\begingroup\$ Will the program be reset between calls or is it allowed to reuse temporary values (e.g powers of 10) computed in the first call for the other 9 calls \$\endgroup\$
    – bsoelch
    Commented Sep 21, 2023 at 15:35
  • \$\begingroup\$ @bsoelch I'd say it's reset between calls, although allowing precomputation somehow might be good? I'm not sure what would be more interesting. \$\endgroup\$ Commented Sep 21, 2023 at 15:39
  • \$\begingroup\$ On my computer time python3 -c 'bin(int("1"*1000000))' executes for 4 seconds \$\endgroup\$
    – l4m2
    Commented Sep 22, 2023 at 0:31
  • \$\begingroup\$ About GMP: You could ban third party libraries. At least in Python the standard library version of string to int is not the fastest possible solution (I tried a relatively simple recursive approach and managed to get the time for 1000000 digits from over 4 seconds to below one second). \$\endgroup\$
    – bsoelch
    Commented Sep 22, 2023 at 17:20
  • 1
    \$\begingroup\$ Bad news: Python is probably getting an impl that can convert 4,000,000-digit numbers in 0.3s. It is already available as a 3rd party lib. github.com/python/cpython/issues/90716#issuecomment-1717742073 \$\endgroup\$
    – Bubbler
    Commented Sep 25, 2023 at 6:32
  • 1
    \$\begingroup\$ Also there is very little point in banning 3rd party libs, as you can always copy the entire code to submit it anyway. \$\endgroup\$
    – Bubbler
    Commented Sep 25, 2023 at 6:38
1
\$\begingroup\$

Barbrack

Your task is to write a program or function that takes a non-negative integer (in decimal or any other convenient base for your language) and output a number in the numbering system Barbrack.

What's that?

Barbrack is a numbering system I made up that can represent non-negative integers. Zero is represented with an empty string or an underscore, one is represented with [], and all other positive integers can be represented with a brack.

A brack is delimited with brackets [] and works as follows (with an example of 84):

  1. Take your number a and find its prime factorization. In this case, the prime factorization of 84 is 22*31(*50)*71.
  2. Find the indices of these primes, where the index of 2 is 1. In this case, the index of 3 is 2, since it's the prime right after 2, and the index of 7 is 4, since it's the fourth prime.
  3. Take the exponents of each prime, and put them in brackets in increasing order of the size of the prime, with consecutive exponents being separated by bars (|). So the general format is [exponent of 2|exponent of 3|exponent of 5…]—in this case, [2|1|0|1]. Minimize the number of cells!
  4. Recursively calculate the exponents in Barbrack, remembering that 0 is the empty string and 1 is []. So [2|1|0|1] => [[1]|[]||[]] => [[[]]|[]||[]].
  5. Output the final result.

Test inputs

0 -> (nothing)
1 -> []
2 -> [[]]
5 -> [||[]]
45 -> [|[[]]|[]]
84 -> [[[]]|[]||[]]
65535 -> [|[]|[]||||[](48 bars)[]]
65536 -> [[[[[]]]]]

(sidenote: (48 bars) means 48 consecutive bars in the actual output)

Rules

Scoring

Minimum bytes on a per-language basis.

\$\endgroup\$
7
  • 1
    \$\begingroup\$ Can characters other than [|] be used? \$\endgroup\$
    – lyxal
    Commented Sep 24, 2023 at 23:59
  • \$\begingroup\$ No, actually. I would allow other symbols, but I'm trying to stay true to my own base. (Let me guess, angle brackets for Vyxal?) \$\endgroup\$
    – Someone
    Commented Sep 25, 2023 at 0:02
  • \$\begingroup\$ @Someone It's generally preferred to be flexible with the output format. There's no reason for those characters specifically to be used, as far as I can tell. In fact, instead of strings, you could allow nested lists too (unless I'm reading the challenge wrong) \$\endgroup\$
    – user
    Commented Sep 25, 2023 at 1:15
  • \$\begingroup\$ similar challenges: codegolf.stackexchange.com/questions/139034/encode-an-integer codegolf.stackexchange.com/questions/254870/… \$\endgroup\$
    – bsoelch
    Commented Sep 25, 2023 at 9:45
  • \$\begingroup\$ Yeah, the /10 bonus was definitely imbalanced. \$\endgroup\$
    – Someone
    Commented Sep 25, 2023 at 12:56
  • \$\begingroup\$ Input validation is generally discouraged. Is there a particular reason you want it? How would it even work if I write a function which takes unsigned int as input? \$\endgroup\$ Commented Sep 25, 2023 at 18:45
  • \$\begingroup\$ Fair point. The rule was basically just "undefined behavior" but dumber anyway. \$\endgroup\$
    – Someone
    Commented Sep 25, 2023 at 18:47
1
86 87
88
89 90
162

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .