Questions tagged [code-golf]
Code-golf is a competition to solve a particular problem in the fewest bytes of source code.
12,313 questions
11
votes
8
answers
1k
views
Longest streak of new animal every day
Every day, you spot zero or more animal species, and keep a log. For example, the first day you see a kangaroo and a koala, the second day, a koala and a zebra, just a koala the third day, and a ...
5
votes
7
answers
2k
views
Recognize my coworker
My work has recently changed the employee tracking system to one that happens to include a fun game to learn your coworker's faces:
Just a small problem: all the pictures include that person's ...
-4
votes
0
answers
95
views
What are common algorithms for rendering 3D primitive shapes as character art? [closed]
https://i.sstatic.net/M6CJyyzp.png
...
13
votes
7
answers
1k
views
Squaring the roots of a polynomial
In this challenge, you are given a polynomial \$p(x)\$, and you need to find a polynomial \$q(x)\$ whose roots are exactly the squares of the roots of \$p(x)\$ (counted with multiplicity). In other ...
20
votes
11
answers
1k
views
What's left after repeatedly removing palindromes
Sometimes a string has buried palindromes in it:
hallolilah contains lol.
And if we took out ...
14
votes
4
answers
830
views
Identify Last-Layer Perm of a Rubik's Cube
A popular method for solving Rubik's cube consists of:
solving its first two layers, by convention starting with the white side
orienting the last layer's pieces so they face the same direction with ...
1
vote
1
answer
250
views
The Parable of the Dagger Prelude
Taken from lesswrong.com
Once upon a time, there was a court jester who dabbled in logic.
The jester presented the king with two boxes. Upon the first box was
inscribed:
"Either this box ...
-4
votes
3
answers
199
views
Print Today's Motto [closed]
Motto of the day (MOTD) or Today's Motto is a algorithm to generate a unique & dynamic motto for every day.
Input of the date to calculate motto for could be in any reasonable format. The program ...
18
votes
7
answers
2k
views
Seven-dimensional cross product
The cross product is a peculiarly 3-dimensional phenomenon. There are multiple ways of generalizing it, but each of them have trade offs.
If you require that a cross product be a product of two ...
23
votes
15
answers
2k
views
Write two very different programs with the same characters
Your task is to write two programs/functions, in the same language.
Program 1: Verify that two inputs are anagrams of each other.
Program 2: Verify that two inputs differ from each other at every ...
6
votes
14
answers
1k
views
Make a Mulenère encryption program
Mulenère cipher is a version of Vigenère ciphers, which uses multiplication instead of addition.
Algorithm
It's simple. First, the password will repeated as much as reach a size bigger than length of ...
16
votes
8
answers
1k
views
Output shortest string containing all input strings
Given a list of words (letters only) in no particular order, output the shortest string containing every word (disregarding case). Output must be lowercase, except the first letter of each word is ...
2
votes
13
answers
577
views
List of prime distances
Task:
Write a code golf program that, given two positive integers n and m, returns a list of the distances between consecutive prime numbers in the range [n, m] ...
16
votes
12
answers
2k
views
Who was leading the race?
Your goal is to find out the order of the drivers in a race (1st, 2nd, 3rd, etc) each time the start / finish line is crossed. This would allow someone to see the progression of a driver throughout ...
12
votes
3
answers
2k
views
The shortest NP-complete problem
Find an NP-complete problem that covers all finite ASCII strings, and present a solver for the problem in the programming language of your choice.
By the definition, if there is a finite ASCII string ...
7
votes
4
answers
1k
views
Minimalist Pokemon Type Chart
We have a few questions related to pokemon types, but the only one that has you print the type chart requires you to take a parameter for dual typings which adds a ton of complexity, switches the axes ...
19
votes
14
answers
2k
views
Output 200 words
Output any 200 words from Wordnik without using external sources. Any case. No repeated or extra words. Code golf.
11
votes
4
answers
1k
views
Follow the glider
Generate four 2D arrays(2D string or whatever is fine) of same size that follows a glider in Conway's life of game.
Each cell is 3x3 plus 1-width border.
Each frame, it advances 1 cycle and shift the ...
5
votes
10
answers
1k
views
Genshin Elemental Aura Decay
There is this game Genshin Impact, when an element is applied to an enemy i.e. Electro it remains for some time causing decay to the enemy health. For this challenge, we'll simplify this to a single ...
18
votes
5
answers
1k
views
Hide from the "left-hand wall" robot
You need to hide in a maze from a robot which methodically follows the left hand wall rule. That is, it enters, immediately turns left, and will always move keeping its left hand on the wall. It only ...
14
votes
11
answers
636
views
Slender sub-lists
A somewhat interesting task came up for me recently which I thought would make a fun code-golf.
Given a number \$n\$ and a list of positive integers less than or equal to \$n\$, we call a sublist ...
15
votes
5
answers
992
views
Make a floor plan given the number of doorways
Output a floorplan of a house given the number of doorways of each room[1]. Every room is a square, and has 0 to 4 doorways. There are never doorways to the outside.
Given this input:
...
28
votes
42
answers
4k
views
Does your source code contain this character?
Given a character as input, determine if your source code contains that character or not.
Shortest code wins. Though, I'd like to see some "normal" languages too.
UPDATE: the question ...
5
votes
5
answers
328
views
Calculate the Squadrat "yard"
Write a program/function to calculate the squadrat "yard" from a collection of squadrats.
Squadrats is a platform that gamifies outdoor activities by recording which large (~1.6 km square) ...
18
votes
12
answers
2k
views
Make an animated winking smiley 😉
Recently, I've been looking more and more into retrogaming programming, and I saw this nice example from an old GBDK wiki (A tool to make Gameboy games with C, replaced by GBDK-2020):
This example ...
7
votes
9
answers
507
views
The Array Walker (part 2)
The Array Walker (part 2)
Input: A non-empty array of integers (without nesting1). This can have negative values!
Output: A boolean value2 indicating whether we can reach the end of the array when ...
7
votes
4
answers
344
views
Output the holes
Copy the holes
Write a program/function to output pieces that would fill the holes in a given shape.
For example, given this shape:
...
-4
votes
3
answers
185
views
Write a program that shows the time until the expiration of Trump's presidential term in milliseconds [closed]
The RULES
Each submission must be a full program.
The program takes no input and prints the time in milliseconds until Trump leaves office to the standard output. So pretty much the title. Write a ...
9
votes
4
answers
520
views
Triangular Transposition Cipher
A text that can be arranged triangularly in some fashion can be read back in some other fashion effectively enciphering it.
Narrowing down a set of plausible triangular numberings allows to ...
16
votes
11
answers
1k
views
Generate a text shape with given area and perimeter
Write a function/program to generate an ASCII art shape with a given area and perimeter, using # and (space) characters.
Each <...>
8
votes
22
answers
829
views
Can we escape from array?
Can we escape from the array?
Input: A non-empty array of non-negative integers (without nesting1).
Output: A boolean value2 indicating whether we can reach the end of the array when starting from the ...
14
votes
16
answers
926
views
Number of intersections between all ranges
Take an array of pairs [L, R]. Count the number of unique intersecting pairs.
A pair [L1, R1] intersects with the pair [L2, R2] if L1 <= L2 <= R1 or L2 <= L1 <= R2. L is guaranteed to be &...
9
votes
3
answers
464
views
Shortest Brainf*ck program that loads prime numbers into memory
Write the shortest Brainfuck program that loads all primes below 256 into memory consecutively. All other cells must be zero.
You may place them in whatever order you like, but they must be ...
9
votes
2
answers
294
views
Putting the pieces together
In this code-golf challenge, you will count the number of ways of putting together pieces of a building toy which consists of slotted squares that interlock with one another, shown below. In ...
28
votes
27
answers
3k
views
Reverse NATO Phonetic Spelling
NATO Phonetic Spelling is a way to spell out letters so that they're distinguishable over a noisy connection. The mapping of letters (stolen from here) is as follows:
...
8
votes
2
answers
1k
views
Shortest Brainf*ck program that loads all Brainf*ck instructions into memory
Write the shortest Brainfuck program that loads all code characters of Brainfuck commands into memory consecutively.
The Brainfuck instructions are:
...
7
votes
4
answers
576
views
Solve Star Battle (Queens) Puzzle
Queens is a puzzle that consists of a NxN grid divided into continuous color regions.
Your goal is to have exactly one queen (Q) in each row, column, and color region
Two queens cannot be adjacent to ...
4
votes
4
answers
181
views
Shortest TeX document that generates ``Overfull \hbox`` Warning
Attempt to produce the shortest TeX document that generates the warning
Overfull \hbox (<x>pt too wide) in paragraph at lines <y>--<z>
that ...
6
votes
17
answers
1k
views
555 Timer Calculator
The Challenge
In honour of getting 555 Rep, here's a little challenge to work out the frequency of a 555 Timer. The frequency can be worked out using
$$ f=\frac{1.44}{(R_1+2R_2)C_1} $$
Implement this ...
3
votes
2
answers
189
views
Winding a Queue
You will be given a matrix. It contains a ride exit (with a direction), some path, as well as some obstacles. Your goal is to build the longest possible queue line in that space, which connects the ...
-4
votes
12
answers
294
views
Power Grid Management
You are writing the management software for one "node" of a power grid. A node has a layout like this
+
|
+---N---+
|
+
Each + is a ...
10
votes
7
answers
1k
views
Solve Tango Puzzle
Tango is a puzzle that consists of a 6x6 grid partially filled with moons (M) and suns (S) and two lists of coordinate pairs, which we will call = and ...
13
votes
7
answers
898
views
Traverse the list
Update 2025-06-04: Thanks for the submissions everyone! It seems like a lot of the solutions are quite slow / timeout for the 100 length test case, so I don't think any will be able to do the 10k. ...
16
votes
12
answers
4k
views
A broken clock is right n times a day
You will be provided with a string containing a digital clock. You must determine how many times per day that clock could be correct. You can ignore daylight savings time. You do not know if the clock ...
7
votes
4
answers
671
views
Ray's intersecting lines
Ray has created some ASCII lines which he is using to draw with on a canvas.
His canvas can be any width and height between 1 to 100 characters (the borders do not count towards these dimensions). He ...
9
votes
16
answers
772
views
Maximum contiguous subarray average
Take an array of integers and an integer K. For all contiguous subarrays of length at least K, return the maximum average value (sum of all elements in the subarray divided by length of subarray, ...
1
vote
0
answers
95
views
The SBFC generator
SBFC(Shifted Brainfuck Compressed) tries to compress 3 tokens in Shifted Brainfuck into a byte. Because supporting all reasonable sequence happens to need a few more states than a byte can express(...
10
votes
6
answers
777
views
Optimal Assembly Jump Distance
In some forms of assembly, there are separate "short jump" and "long jump" instructions. However, these jumps can themselves have different lengths, which can affect the distance ...
19
votes
12
answers
1k
views
Print Dyck paths
Definition
A Dyck path of length \$2n\$ can be defined as a two-dimensional path such that:
The path consists of straight lines of equal length.
The path goes left to right while moving either up or ...
15
votes
5
answers
2k
views
When is my coworker's next birthday?
inspired by this post.
Consider 2 coworkers who work Monday - Friday, every week of every year, with no breaks or national holidays.
On day n (day, month and year), worker 1 asks worker 2 their ...