# Kdb+ Mastermind Challenge

##### 4 Oct 2017 | Kdb+, kx meetup, mastermind, meetup, q, q programming language
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.

Background

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``````

Challenge

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.

``````q)score["1124";"1412"]
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.

``````q)score["1234";"1111"]
1 0``````

Scoring

There are are a total of 1296 possible codes:

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

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

``````q)md5 3 raze/ string C score\:/: C
0x08dd3c8cfd42bda309c38b9bdab16a06``````

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

``````q)\t C score\:/: C
5838``````

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

Results

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® and kdb+ are registered trademarks of Kx Systems, Inc., a subsidiary of First Derivatives plc.

### SUGGESTED ARTICLES

#### Head of Products, Solutions and Innovation at Kx on Product Design and the Vision for the Future

##### 16 Mar 2018 | CAT, financial services, Kdb+, MiFID II

As the SVP of Products, Solutions and Innovation at Kx Systems, James Corcoran is part of a new chapter in software development at Kx. Since joining Kx parent First Derivatives as a financial engineer in 2009, James has worked around the world building enterprise systems at top global investment banks before moving to the Kx product team in London. James sat down with us recently to discuss his perspective on product design and our technology strategy for the future.

#### Kdb+ Utilities: Essential utility for identifying performance problems

##### 28 Feb 2018 | APL, Kdb+

If you are a kdb+/q developer, you will find the utilities created by Kx Managing Director and Senior Solution Architect Leslie Goldsmith to be a valuable resource. The “Kdb+ Utilities” series of blog posts gives a quick introduction to the utilities, available at Leslie Goldsmith’s GitHub. In this third part of the series we look at Leslie’s qprof, which allows a programmer to drill down into q functions or applications to inspect performance and CPU usage in a fine-grained fashion.

#### Kdb+ Utilities: Q code Workspace Utilities

##### 6 Feb 2018 | APL, Kdb+, kx

If you are a kdb+/q developer, you will find the workspace utilities created by Kx Managing Director and Senior Solution Architect Leslie Goldsmith to be a valuable resource. This is the first in a series of blog posts that give a quick introduction to several utilities available at Leslie Goldsmith’s GitHub. In this part of the series we look at an essential tool which contains routines for summarizing and searching the contents of a workspace, ws.