Which dominoes are missing?

34

A standard domino set has 28 unique pieces:

enter image description here

Given a list of 28 or fewer unique dominoes, output the list required to make a complete set.

Input and output dominoes are specified by two digits - the number of pips on each side of the domino, e.g. 00, 34, 40, 66.

The digits may be given in any order, so 34 is the same domino as 43

Example Inputs

00 01 02 03 04 05 06 11 12 13 14 15 16 22 23 24 25 26 33 34 35 36 44 45 46 55 56 66
00 10 11 20 21 22 30 31 32 33 40 41 42 43 44 50 51 52 53 54 55 60 61 62 63 64 65 66
00 01 02 03 04 05 06 11 12 13 14 15 16 22 23 24 25 26 34 35 36 44 45 46 55 56 66
00 02 03 04 05 06 11 13 14 15 16 22 24 25 26 33 35 36 44 46 55 66
<empty list>

Corresponding Example Outputs

<empty list>
<empty list>
33
01 12 23 34 45 56
00 01 02 03 04 05 06 11 12 13 14 15 16 22 23 24 25 26 33 34 35 36 44 45 46 55 56 66

Digital Trauma

Posted 2016-02-28T05:45:00.673

Reputation: 64 644

2What input formats are allowed? Lists of strings? Lists of lists of integers? – Martin Ender – 2016-02-28T09:33:46.470

1@Martin I was assuming we have a meta consensus somewhere along the lines of "whatever list, array, set, collection, vector, matrix,... Is appropriate for your language. Members may be numbers or strings" – Digital Trauma – 2016-02-28T16:53:32.653

Does that mean that we can request each domino as a pair of integers, for example 03 16 = [0, 3], [1, 6]? – FlipTack – 2017-12-29T11:49:53.797

1@FlipTack Yes, of course – Digital Trauma – 2018-01-01T00:24:42.087

Answers

10

CJam, 11 bytes

{:$7Ym*:$^}

An unnamed block (function) with I/O as a list of pairs of integers.

Test it here.

Explanation

:$   e# Sort each pair in the input.
7Ym* e# Get all pairs with elements in range [0 .. 6] using a Cartesian product.
:$   e# Sort each pair.
^    e# Symmetric set-difference. This will remove all pairs that are in the input
     e# and also remove duplicates, because it's a set operation.

Martin Ender

Posted 2016-02-28T05:45:00.673

Reputation: 184 808

Why do you need the {} brackets? – Chromium – 2018-06-14T01:49:02.093

6

Pyth, 12 10 bytes

-.CU7 2SMQ

Input and output in format [[0, 0], [0, 1], ...].

   U7       generate range [0, 1, ..., 6]
 .C   2     all combinations-with-replacement of 2, generates [[0,0],[0,1],...]
         Q  get the input
       SM   sort each domino (turns ex. [1,0] into [0,1])
-           remove the map-sort'd input from the full array

Try it here.

Thanks to @MartinBüttner for saving 2 bytes with a different input/output format!

Doorknob

Posted 2016-02-28T05:45:00.673

Reputation: 68 138

5

JavaScript (ES7 proposed), 80 76 bytes

s=>[for(n of d="0123456")for(o of d.slice(n))if(s.search(n+o+'|'+o+n)<0)n+o]

Takes input as a space-separated string and returns an array of strings. Array comprehensions really pull their weight for this one.

Neil

Posted 2016-02-28T05:45:00.673

Reputation: 95 035

3

Julia 0.6, 47 bytes

A->setdiff([[i,j]for i=0:6 for j=i:6],sort.(A))

Try it online!

(Corrected range start thanks to JayCe.)


48 bytes

A->[(i,j)for i=0:6 for j=i:6 if[i,j]∉sort.(A)]

Try it online!

sundar - Reinstate Monica

Posted 2016-02-28T05:45:00.673

Reputation: 5 296

I think the zero dominoes seem to be missing from your 3rd TIO test case. For i=0:6 maybe ? – JayCe – 2018-06-17T02:17:20.580

That's what I get from trying to post half asleep at 3AM! Yep, fixed now (hopefully), thanks. – sundar - Reinstate Monica – 2018-06-17T07:41:38.293

3

Ruby 74 bytes

->b{a=(0..27).map{|i|"%d%d"%[i%7,(i+i/7)%7]}
b.map{|e|a-=[e,e.reverse]}
a}

Takes an array of strings, returns an array of strings.

Commented in test program

f=->b{a=(0..27).map{|i|"%d%d"%[i%7,(i+i/7)%7]} #generate complete set of dominos (each domino once) and store in a
b.map{|e|a-=[e,e.reverse]}                     #remove provided dominos (check both forward and reverse representations)
a}                                             #return a

p f[%w{00 01 02 03 04 05 06 11 12 13 14 15 16 22 23 24 25 26 33 34 35 36 44 45 46 55 56 66}]
p f[%w{00 10 11 20 21 22 30 31 32 33 40 41 42 43 44 50 51 52 53 54 55 60 61 62 63 64 65 66}]
p f[%w{00 01 02 03 04 05 06 11 12 13 14 15 16 22 23 24 25 26 34 35 36 44 45 46 55 56 66}]
p f[%w{00 02 03 04 05 06 11 13 14 15 16 22 24 25 26 33 35 36 44 46 55 66}]
p f[[]]

Output

[]
[]
["33"]
["01", "12", "23", "34", "45", "56"]
["00", "11", "22", "33", "44", "55", "66", "01", "12", "23", "34", "45", "56", "60", "02", "13", "24", "35", "46", "50", "61", "03", "14", "25","36", "40", "51", "62"]

In the last example (input empty list) note the order of generation of the complete list of dominoes using modular arithmetic. 7 Doubles are generated first, then 7 dominoes with a difference of 1 (or 6) pips between each side, then 7 dominoes with a difference of 2 (or 5) pips, and finally 7 dominoes with a difference of 3 (or 4) pips.

Level River St

Posted 2016-02-28T05:45:00.673

Reputation: 22 049

2

Python 2, 91 bytes

lambda A,S="0123456":sum([[i+j for j in S[int(i):]if(i+j in A)+(j+i in A)<1]for i in S],[])

Try it online!

FlipTack

Posted 2016-02-28T05:45:00.673

Reputation: 13 242

2

JavaScript (SpiderMonkey), 69 bytes

s=>[for(n of d="0123456")for(o of d)if(n<o<!s.match(n+o+'|'+o+n))n+o]

Try it online!

optimized from Neil's

l4m2

Posted 2016-02-28T05:45:00.673

Reputation: 5 985

2

05AB1E, 12 11 bytes

6Ýã€{JI€{KÙ

-1 byte thanks to @Emigna.

Try it online.

Explanation:

6Ý         # [0,1,2,3,4,5,6]
  ã        # Duplicate and take the cartesian product (pair up each)
   €{      # Sort each pair
     J     # Join them together to strings
I€{        # Sort each pair-string of the input
K          # Remove all pairs from the input from the cartesian list
Ù          # Only leave unique values

Kevin Cruijssen

Posted 2016-02-28T05:45:00.673

Reputation: 67 575

2

R, 111 bytes

function(s,p=paste0,L=lapply)setdiff(p(sequence(1:7)-1,rep(0:6,t=1:7)),L(L(strsplit(s,''),sort),p,collapse=''))

Try it online!

Not really proud of this, but R is not very "golfy" in splitting/concatenating strings...

digEmAll

Posted 2016-02-28T05:45:00.673

Reputation: 4 599

2

Perl, 48 + 1 = 49 bytes

for$=(0..6){for$.($=..6){/$=$.|$.$=/||say$=.$.}}

Requires the -n flag, and the free -M5.010|-E:

$ perl -nE'for$=(0..6){for$.($=..6){/$=$.|$.$=/||say$=.$.}}' <<< '00 02 03 04 05 06 11 13 14 15 16 22 24 25 26 33 35 36 44 46 55 66'                      
01
12
23
34
45
56

Pretty boring answer overall, but here goes with an ungolfed version:

# '-n' auto reads first line into `$_`:
# $_ = <>;
foreach $a (0..6) {
  foreach $b ($a..6) {
    say $a . $b unless $_ =~ /$a$b|$b$a/;
  }
}

andlrc

Posted 2016-02-28T05:45:00.673

Reputation: 1 613

1

Java 8, 105 bytes

A void lambda accepting a mutable java.util.Set<String>.

s->{for(int i=0,a,b;i<49;)if(s.add(""+(a=i/7)+(b=i++%7))&(s.add(""+b+a)|a==b))System.out.print(" "+a+b);}

Try It Online

Ungolfed

s -> {
    for (int i = 0, a, b; i < 49;)
        if (
            s.add("" + (a = i / 7) + (b = i++ % 7))
            & (
                s.add("" + b + a)
                | a == b
            )
        )
            System.out.print(" " + a + b);
}

Acknowledgments

  • -1 byte thanks to Jonathan Frech

Jakob

Posted 2016-02-28T05:45:00.673

Reputation: 2 428

1int i=0,a,b;while(i<49 can be for(int i=0,a,b;i<49;. – Jonathan Frech – 2018-06-14T11:42:15.093

1

J, 26, 24 bytes

-2 bytes thanks to FrownyFrog

(;(,.i.,])&.>i.7)-.\:~"1
  • (;(,.i.,])&.>i.7) calculates the full set (this part could be golfed further, I believe. And please do if you see how...)
  • -. is "set minus"
  • /:~"1 orders each of the inputs

Try it online!

Original

Try it online!

((#~<:/"1)>,{;~i.7)-./:~"1

Jonah

Posted 2016-02-28T05:45:00.673

Reputation: 8 729

(;(,.i.,])&.>i.7) saves 2 (reverses the order) – FrownyFrog – 2018-06-14T06:45:32.087

@FrownyFrog thanks, updated. – Jonah – 2018-06-18T20:51:50.410

1

Jelly, 8 bytes

Ṣ€7ḶŒċ¤ḟ

Try it online!

Argument is a list of length-2 lists of integers. The footer transforms the input from the format in the test cases to the format this solution accepts.

Erik the Outgolfer

Posted 2016-02-28T05:45:00.673

Reputation: 38 134

1

Python 2, 89 86 bytes

Saved a few bytes by simplifying domino set generation.

lambda z,s="0123456":{x+y for x in s for y in s[int(x):]}-{(a+b,b+a)[a>b]for a,b in z}

Try it online!

Takes a list of strings like ["00", "10", "02] as an argument for the dominoes. Returns python set objects, which are unordered distinct lists.

Explanation

# anonymous function, s should always have its default value
lambda z,s="0123456":
                     # contents are a set
                     {                                  }
                          # iterate over characters in string
                          for x in s
                                     # for each x, iterate over x from index of current item forward
                                     for y in s[int(x):]
                     # add characters together for domino string
                     x+y
                                                         # contents are a set, return the difference between these two sets 
                                                         -{                          }
                                                             # iterate over items in input list, split strings into two characters
                                                                         for a,b in z
                                                             # sort strings in input list (so that i.e. "10" => "01")
                                                             # essentially, "ab" if a<b, otherwise "ba"
                                                             (a+b,b+a)[a>b]

Triggernometry

Posted 2016-02-28T05:45:00.673

Reputation: 765

1

Mathematica, 49 bytes

Complement[Join@@Table[{x,y},{x,0,6},{y,0,6}],#]&

Input is list of list of integers.

CalculatorFeline

Posted 2016-02-28T05:45:00.673

Reputation: 2 608

3Fails on the last test case; remember, these are unordered sets. – LegionMammal978 – 2016-02-29T11:15:50.993

I would agree with @LegionMammal978; this answer appears to be invalid. – Jonathan Frech – 2018-08-09T22:15:36.247

0

racket

(define (missing-dominoes input)
  (filter
   (lambda (n)
     (not (member n (map
                     (lambda (n)
                       (let ((x (quotient n 10)) (y (remainder n 10)))
                         (if (<= x y) n (+ (* y 10) x))))
                     input))))
   (for*/list ([i (in-range 7)] [j (in-range i 7)])
     (+ (* 10 i) j))))

Kevin

Posted 2016-02-28T05:45:00.673

Reputation: 81

2Nice to see people golfing in racket! This is a code-golf question so you probably ought to include your bytecount in your answer. You could also definitely remove quite a bit of whitespace from this answer. – Post Rock Garf Hunter – 2018-06-18T17:29:58.970

I would agree with @WW in that this answer does not seem to be sufficiently golfed to be valid. – Jonathan Frech – 2018-08-09T22:17:20.897

0

Haskell, 65 bytes

f x=[[a,b]|a<-"0123456",b<-[a..'6'],notElem[a,b]x&&notElem[b,a]x]

Usage example:

*Main> f ["00","02","03","04","05","06","11","13","14","15","16","22","24","25","26","33","35","36","44","46","55","66"]
["01","12","23","34","45","56"]

Iterate a in an outer loop over all digits from 0 to 6 and b in an inner loop over all digits from a to 6 and keep those ab where neither ab nor ba are found in the input string.

nimi

Posted 2016-02-28T05:45:00.673

Reputation: 34 639

0

Seriously, 16 bytes

,`S`M7r;∙`εjS`M-

Takes input as a list of strings, outputs a list of strings

Try it online!

Explanation:

,`S`M7r;∙`εjS`M-
,`S`M             map: sort each input string
     7r;∙         cartesian product of range(0,7) ([0,1,2,3,4,5,6]) with itself
         `εjS`M   map: join on empty string, sort (results in all valid dominoes with some duplicates)
               -  set difference (all values present in valid dominoes set not present in input, with duplicates removed)

Actually, 13 bytes (non-competing)

♂S7r;∙`εjS`M-

This is identical to the Seriously answer (with the exception of implicit input and ♂S being a shorter way to short each input string).

Try it online!

Mego

Posted 2016-02-28T05:45:00.673

Reputation: 32 998

1You have duplicates in the output – Digital Trauma – 2018-06-13T19:12:34.623

@DigitalTrauma This is due to backwards-incompatible changes made since the time of posting. – Mego – 2018-06-13T22:49:14.410