A four-digit code puzzle with per-digit feedback looks like a ten-minute job. For each position in the guess, mark it correct if it matches the secret in that position, present if the secret contains the digit somewhere else, and absent otherwise. That version works until a digit is repeated.
The usual rule is that each digit in the secret can justify only one coloured tile. Numble states it in its grey tile description: grey means the digit "is not in the code, or you have already used up every copy of it". The second half of that sentence is the part a quick implementation leaves out.
The bug
Here is the quick version, written so that you can compare it with the correct one:
public enum Mark { Absent, Present, Correct }
public static Mark[] ScoreNaive(string guess, string secret)
{
var marks = new Mark[4];
for (int i = 0; i < 4; i++)
marks[i] = guess[i] == secret[i] ? Mark.Correct
: secret.Contains(guess[i]) ? Mark.Present : Mark.Absent;
return marks;
}
Take a secret of 1234 and a guess of 1122. The secret has one 1 and one 2. The first 1 in the guess is an exact match and uses up the only 1. The second 1 should therefore be absent. The first 2 is in the wrong place, and the secret has one 2, so it is present. The second 2 has no copy left, so it is absent. The correct feedback is correct, absent, present, absent. The naive function returns correct, present, present, present, which tells the player the code contains two 1s and two 2s.
The bug is easy to hit. It only appears when the guess repeats a digit, and 4,960 of the 10,000 possible codes do.
Two passes
Exact matches have to claim their copies before any wrong-place matches are considered. Otherwise an early wrong-place digit can use up a copy that a later exact match needed. With a secret of 1234 and a guess of 2256, the second 2 is an exact match. A single left-to-right pass that uses up copies as it goes would give the first 2 a present mark because it comes first, then find no copy left for the exact match at position two. For 1234 and 2256 that gives present, absent, absent, absent, where the right answer is absent, correct, absent, absent.
public static Mark[] Score(string guess, string secret)
{
var marks = new Mark[4];
var unmatched = new int[10]; // copies of each digit in the secret not used by an exact match
for (int i = 0; i < 4; i++)
{
if (guess[i] == secret[i]) marks[i] = Mark.Correct;
else unmatched[secret[i] - '0']++;
}
for (int i = 0; i < 4; i++)
{
if (marks[i] == Mark.Correct) continue;
int d = guess[i] - '0';
if (unmatched[d] > 0)
{
marks[i] = Mark.Present;
unmatched[d]--; // each copy can be claimed once
}
}
return marks;
}
The first loop marks the exact matches and counts what is left of the secret. The second loop hands out present marks from that remainder, left to right. For 1234 and 2256 it returns absent, correct, absent, absent.
The codes are strings, not integers, because a code can start with zero and 0123 has to stay four characters. That is also why the examples use "0123" and never 123.
Testing it
The check worth copying into a test suite is a counting property: the number of non-absent marks must equal, summed over the digits 0 to 9, the smaller of the digit's count in the guess and its count in the secret. It doesn't depend on the algorithm above, so it can catch a mistake in it. I ran it over all 100,000,000 guess and secret pairs on .NET 8 and Score passed every one, while ScoreNaive fails it on the 1234 and 1122 example.
The naive function disagrees with Score on 15.2 per cent of all pairs, which is a good reason not to rely on spot checks with distinct digits.
I also compared Score with the colour function in the live game's JavaScript on all 100,000,000 guess and secret pairs, and the results were identical.
A candidate filter
With only 10,000 codes, you can keep every code that is still possible and filter it after each guess:
static string Show(Mark[] m) =>
string.Concat(m.Select(x => x switch { Mark.Correct => 'G', Mark.Present => 'Y', _ => '-' }));
var all = Enumerable.Range(0, 10000).Select(n => n.ToString("D4")).ToArray();
string guess = "1122", feedback = "GYY-";
var possible = all.Where(c => Show(Score(guess, c)) == feedback).ToArray();
If the secret is 1231, the guess 1122 produces GYY-, and the filter leaves 17 codes. A first guess of 4567 against the same secret produces ---- and leaves 1,296, which is 6 to the power 4: any code made only from 0, 1, 2, 3, 8 and 9. A hundred full passes over all 10,000 codes took 95 milliseconds on the machine I used, so no indexing is needed.
The live game also shows an arrow saying whether the secret is higher or lower than the guess. That is one comparison, int.Parse(secret).CompareTo(int.Parse(guess)), which is positive when the secret is higher. It is a separate piece of information from the marks, so it needs a separate filter condition.
A daily code from the date
To give everyone the same code on the same day, derive it from the calendar date instead of storing it. DateOnly.DayNumber is a plain integer for the date, and a small mixing function turns it into four digits:
public static string DailyCode(DateOnly day)
{
unchecked
{
ulong z = (ulong)day.DayNumber + 0x9E3779B97F4A7C15UL;
z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9UL;
z = (z ^ (z >> 27)) * 0x94D049BB133111EBUL;
z ^= z >> 31;
return (z % 10000UL).ToString("D4");
}
}
The unchecked block matters: the multiplications are meant to wrap around, and a project built with overflow checking switched on would throw without it.
The mixing steps are the SplitMix64 output function, with the day number as its starting state. I used it in preference to new Random(seed) because the Microsoft documentation says the Random implementation is not guaranteed to stay the same across major versions of .NET, so the same seed may not give the same sequence after an upgrade. A fixed function gives the same string for the same date on every version. Over 100,000 consecutive days, 9,844 codes started with 0, close to the one in ten you would expect.
Choose the date deliberately. DateOnly.FromDateTime(DateTime.Now) gives the local date of the machine running the code, so on the player's own device it matches a puzzle that changes at local midnight. DateTime.UtcNow would change everybody's puzzle at the same instant instead.
Join the conversation! Your thoughts help the community grow.