Kdb+ Mastermind Challenge

4 Oct 2017 | , , , , ,
Share on:

By Nick Psaris

Nick Psaris is an expert kdb+/q programmer and the author of “Q-Tips: Fast, Scalable and Maintainable Kdb+” (available on Amazon). At the most recent Kx Community NYC Meetup (September 2017), Nick presented the results of his “Kdb+ Mastermind Challenge.” The rules for the challenge appear below, and more information is available on Nick’s GitHub.


Mastermind is a popular code-breaking game introduced in 1975 that pits a “codemaker” against a “codebreaker.” The “code,” represented by four pegs chosen from a universe of 6 colors, is picked by the “codemaker.” The “codebreaker” continually makes 4-peg guesses and is told by the “codemaker” how many are the correct color in the correct position, and how many are the correct color in the wrong position.

The actual game uses black and white begs to denote these two cases. We will use a list of two values to represent the score. For example, if two pegs are correct and one peg is the right color but in the wrong position, the score would be: 2 1. The game continues until the “codebreaker” guess the correct code (with a score of 4 0).

There are 14 possible scores:

q)flip (where;raze til each)@\: 5 4 3 1 1
0 0
0 1
0 2
0 3
0 4
1 0
1 1
1 2
1 3
2 0
2 1
2 2
3 0
4 0


The challenge is to write a function that returns the score when provided a code and guess. For example, given the code of “1124” and the guess of “1412”, the score would be 1 3.

1 3

Once a peg is used for a match it can not be used again. For example, given the code of “1234” and the guess of “1111”, the score would be 1 0, not 1 3.

1 0


There are are a total of 1296 possible codes:

q)count C:(cross/)4#enlist "123456"

Your solution will be judged valid if it computes the proper md5:

q)md5 3 raze/ string C score\:/: C	

The fastest valid submission (measured in milliseconds) will be judged the winner.

q)\t C score\:/: C

In the event of a tie, the submitter who provided the first valid submission (irrespective of performance) will be declared the winner.


There were 14 submissions in the month before the Meetup, including an excellent C implementation which was ineligible for the prize. The top four solutions were notable by their completely different approaches.

First place went to this solution:

k)score:{[x;y;z;w]y@6/:x w,z}[@[&,55;"123456";:;!6];{(+/m;0+/&/g@\:!*g:(#:'=:)'(y n;x n:&~m:x=y))}.',/(,\:/:)/2#,,:'.q.cross/4#,"123456"]

Second place went to this solution:

score:{(4-c),$[f2~df:distinct f2:f where not (c:count w)=f:(x@:w)?y@:w:where not x=y;
  count df;
  count[df]+sum @[y;f?df;:;"_"] in @[x;df;:;" "]

Third place went to this solution:

score:{n:0 0 0 0 0 0 0 0 0 0 0 0i;n[-49 -49 -49 -49 -43 -43 -43 -43i+"i"$x,y]+:1i;b,(sum(&). 0 6_n)-b:sum x=y}

Fourth place went to this solution:

score:{n,4-(n:sum x=y)+count{x _x?y}/[x;y]}

The winning solution was fast not just because it cached the mappings from guess to score, by creating a projection, but also because their caching mechanism was faster than a dictionary lookup.

First prize was a new Mastermind board game which now resides in the common room of our Meetup host, 1010data.


Kx Product Insights: Inter-Trading Alert

5 Dec 2018 | , , ,

by Aidan O’Neill Kx has a broad list of products and solutions built on the time-series database platform kdb+ that capitalize on its high-performance capabilities when analyzing very large datasets. Kx for Surveillance is a robust platform widely used by financial institutions for monitoring trades for regulatory compliance. The Surveillance platform instantly detects known trading […]

Kx extends relationship with NASA Frontier Development Lab and the SETI Institute

The Exploration of Space Weather at NASA FDL with kdb+

4 Dec 2018 | , , , ,

Our society is dependent on GNSS services for navigation in everyday life, so it is critically important to know when signal disruptions might occur. Physical models have struggled to predict astronomic scintillation events. One method for making predictions is to use machine learning (ML) techniques. This article describes how kdb+ and embedPy were used in the ML application.