r/dailyprogrammer • u/jnazario 2 0 • Sep 10 '15
[2015-09-09] Challenge #231 [Intermediate] Set Game Solver
Our apologies for the delay in getting this posted, there was some technical difficulties behind the scenes.
Description
Set is a card game where each card is defined by a combination of four attributes: shape (diamond, oval, or squiggle), color (red, purple, green), number (one, two, or three elements), and shading (open, hatched, or filled). The object of the game is to find sets in the 12 cards drawn at a time that are distinct in every way or identical in just one way (e.g. all of the same color). From Wikipedia: A set consists of three cards which satisfy all of these conditions:
- They all have the same number, or they have three different numbers.
- They all have the same symbol, or they have three different symbols.
- They all have the same shading, or they have three different shadings.
- They all have the same color, or they have three different colors.
The rules of Set are summarized by: If you can sort a group of three cards into "Two of ____ and one of _____," then it is not a set.
See the [Wikipedia page for the Set game](http://en.wikipedia.org/wiki/Set_(game)) for for more background.
Input Description
A game will present 12 cards described with four characters for shape, color, number, and shading: (D)iamond, (O)val, (S)quiggle; (R)ed, (P)urple, (G)reen; (1), (2), or (3); and (O)pen, (H)atched, (F)illed.
Output Description
Your program should list all of the possible sets in the game of 12 cards in sets of triplets.
Example Input
SP3F
DP3O
DR2F
SP3H
DG3O
SR1H
SG2O
SP1F
SP3O
OR3O
OR3H
OR2HExample Output
SP3F SR1H SG2O
SP3F DG3O OR3H
SP3F SP3H SP3O
DR2F SR1H OR3O
DG3O SP1F OR2H
DG3O SP3O OR3OChallenge Input
DP2H
DP1F
SR2F
SP1O
OG3F
SP3H
OR2O
SG3O
DG2H
DR2H
DR1O
DR3OChallenge Output
DP1F SR2F OG3F
DP2H DG2H DR2H
DP1F DG2H DR3O
SR2F OR2O DR2H
SP1O OG3F DR2H
OG3F SP3H DR3O
2
u/[deleted] Sep 10 '15
Your wikipedia link is missing the right parenthesis
18
u/adrian17 1 4 Sep 10 '15
Python:
14
u/XenophonOfAthens 2 1 Sep 10 '15
Oh, how can you not want to do this problem in Prolog! It's like the perfect Prolog problem! Here's my solution, without the boring I/O crap that Prolog sucks at:
Look how pretty! It's like if you translated the text of the problem into logic, AND SHIT JUST WORKS! AWESOME!
Now, some grizzled Prolog veterans out point out that this will unify not just with all the valid sets, but also all the permutations of all the valid sets. To those people I say: why you gotta hate, man? What's your deal?
But fine, this is a more correct version of the
card_set/2:But that's just not as pretty... Anyway, if you ran a query with the deck in it and asked it for all solutions, this is what'll happen:
DOPE AS SHIT, Y'ALL .
10
u/a_Happy_Tiny_Bunny Sep 10 '15 edited Sep 10 '15
Haskell
I just realized this is one of the shortest solutions (excluding J, obviously), so, if anyone is interested, here is some code-golfing:
132 characters
6
u/curtmack Sep 10 '15
Haskell
Types are awesome you guys.
5
u/skeeto -9 8 Sep 10 '15
C, comparing sets with a bitmask.
4
u/carrutstick Sep 10 '15 edited Sep 10 '15
Haskell
Spent a while looking for a way to avoid explicit recursion, but couldn't find anything efficient.
4
u/katyne Sep 10 '15
Java. I might have a problem. I need to let go :/
The whole thing
https://gist.github.com/anonymous/af8698039f32d0fc7db7
3
u/fvandepitte 0 0 Sep 10 '15
Haskell, feedback is apriciated.
Output & usage
1
u/jnazario 2 0 Sep 10 '15 edited Sep 10 '15
scala solution. this is some of the first scala i wrote (i drafted this challenge a long time ago) back when i transitioned from F#, so it may not be the most idiomatic.
2
u/MEaster Sep 10 '15 edited Sep 10 '15
F#
If you want something over-engineered, I have the solution for you! GitHub link.
Result (backwards due to the way the combination function works):
I also wrote a generator of all the cards, which I could then use to get all 1080 of the sets(Assuming I wrote the solution correctly).
[Edit] Made the match checking less stupid after seeing /u/XenophonOfAthens solution.
2
u/Godspiral 3 3 Sep 10 '15 edited Sep 10 '15
in J,
2
u/halfmonty 1 0 Sep 10 '15
C# Saw the concise nature of the Python submission by adrian17 and wondered what the same approach would look like in C#. It would require an extension to handle the combinations, then the logic is similar.
I though long and hard about trying to inline the "if" with a "for" inline like done in adrian17's but I'm thinking it's simply not possible in C#, at least not in any way that isn't horrible and ugly. Anyway, minus the required extension, I think it's a fairly decent match for code in implementation and method of resolution to really show off the language differences.
2
u/casualfrog Sep 10 '15
ECMAScript 6 (feedback welcome)
Output:
2
u/narcodis Sep 10 '15 edited Sep 11 '15
Javascript, using NodeJS. No one likes javascript. Why do I insist on doing everything with javascript. No idea.
EDIT: Cleaned up the code a lot.
Challenge input/output:
2
u/patrickwonders Sep 10 '15
For those looking for a Linear Algebra solution: https://www.math.ucdavis.edu/~anne/FQ2014/set_game.pdf
2
u/Lube Sep 11 '15
Haskell but not happy with it, I appreciate tips to improve!
1
u/TheBlackCat13 Sep 10 '15 edited Sep 10 '15
Python 3.x solution. This solution will work for arbitrary-sized groups of cards:
And answer:
1
u/chrissou Sep 10 '15
Brute-force Haskell
1
u/marchelzo Sep 10 '15
I didn't spend much time thinking about how to do this before I began coding, so I guess I got what was coming to me with this needlessly complicated solution (C).
Here's a gist
Output:
1
u/[deleted] Sep 10 '15 edited Sep 10 '15
Using CHR in Prolog (with SWI-Prolog's library(chr)). I'm just starting to learn CHR. It's quite fun and seems well suited for this kind of problem. As always: suggestions and questions welcome.
Challenge:
Edited: added explanatory paraphrases above the CHR rules and rendered the code more elegant.
1
u/XDtsFsoVZV Sep 10 '15
Full disclosure: I peeked at /u/adrian17's solution, but I tried to come up with my own solution instead of copying theirs or merely putting it in my own words. I still feel dirty and like I can't put this in my GitHub now, though (I put my solutions in my GitHub so a potential employer can see them, since I still suck too much to contribute to projects or create my own).
Python 3
1
u/[deleted] Sep 10 '15
More bit twiddling in Fortran - note no external libraries needed, just using elemental intrinsic bit functions like nature intended.
..output... input set 1
input set 2
1
u/patrickwonders Sep 10 '15
Common Lisp
1
u/ullerrm Sep 10 '15
Python:
1
u/Def_Your_Duck Sep 10 '15
Java
And the Card class:
1
u/ChazR Sep 11 '15
Haskell.
1
u/awernick Sep 11 '15 edited Sep 11 '15
Ruby. I was heavily influenced by /u/XenophonOfAthens's fantastic ProLog solution.
1
u/UncleVinny 1 0 Sep 11 '15
Java -- this was kind of a cakewalk. Looking forward to seeing other responses to see if I missed a faster way to do it.
1
u/codeman869 Sep 11 '15
Ruby Learned about the Set class through this exercise! :)
Output:
1
u/13467 1 1 Sep 11 '15
C++11. I used bitmask hackery instead of a set.
1
u/Marek86 Sep 11 '15
f#
example:
1
u/Billigerent Sep 11 '15 edited Sep 11 '15
Did this in python. First, I wanted to generate all possible Set cards.
Next, I check a list of cards with brute force and a big block of logic.
It worked for both given inputs and for any random list of cards. Feedback is always welcome.
1
u/tangbj Sep 12 '15
Python:
1
u/jeaton Sep 13 '15
python:
1
u/SportingSnow21 Sep 13 '15
Go:
First post here, so please excuse any accidental craziness in my formatting.
1
u/slwz Sep 13 '15
Here, have a bit of ES6 javascript written in a hurry : ) I'm starting to love this language. For those who want to give it a try, you have to install transpiler like babel. It's available on npmjs.
1
u/ReckoningReckoner Sep 13 '15
ruby:
And here's my native combinations algorithm
1
u/luarockr Sep 14 '15
Oberon-2
1
u/arunner Sep 15 '15
I'm late to the party! One more in python, pretty much the same as adrians. I changed the letter of the last attribute from (O)pen to 0(zero), its more practical this way.
one more:
Peter Norvig has written about this
1
u/doesmycodesmell Sep 16 '15
Ruby
1
u/l4adventure Sep 22 '15 edited Sep 22 '15
[Ruby] Not very proud of this one. It's as brute-force as they get. I especially don't like the valid_set? method I made. There just HAS to be an easier way to do it than to do 4 nested if statements with 9 comparisons:
output:
Any feedback is welcome!
1
u/[deleted] Oct 01 '15
Common Lisp brute force solution
1
u/ken__mongolia Nov 08 '15 edited Nov 08 '15
Python.
Edited to clean up a few parts.
1
u/smls Nov 09 '15
Perl 6
0
u/roryokane Sep 16 '15 edited Sep 16 '15
A Ruby solution with unit tests. It is longer than most other solutions because I tried to have small, focused methods, and I made
Cardan actual struct instead of working directly with raw strings. My input and output functions don’t refer to stdin and stdout directly, so that I can mock them when I run the tests.Main code, lib/kata.rb
The important part is
module SetGame.At first I used
Set.new(values).sizewith the RubySetclass to count whether values were all the same or all different, but then I realized I didn’t needSet– I could just useArray#uniq.Testing code that uses MiniTest, test/kata_test.rb
I actually do use the Ruby
Setclass here, to implementassert_have_same_lines. That method checks whether the output is correct while ignoring the order of the lines – my test was spuriously failing before I wrote that.