Draw the combinations that add up to 100

13

2

You have a box with a single digit number in each corner:

1|2
---
3|4

If we concatenate the digits along rows left to right, we get 12 and 34. If we concatenate the digits along columns top to bottom, we get 13 and 24. If we add all of these numbers, we get 12+34+13+24 = 83.

Challenge

Write a program to print all such boxes where the sum calculated as above is equal to 100.

Assuming each corner contains a single digit number between 1 and 9, there are 9 combinations: 1157, 1247, 1337, 1427, 1517, 2138, 2228, 2318, and 3119. (Note that there are 25 combinations if we include 0, but we're not for this challenge.)

Note that the example 1234 doesn't work since 12+34+13+24 does not equal 100.

Input

None

Output

Answer boxes in the following format:

A|B
---
C|D

Output should be printed to STDOUT or closest alternative.

Rules

  • AB + CD + AC + BD = 100
  • Each corner will contain the positive integers 1-9 only.
  • 0 is excluded.
  • Numbers can be used more than once.
  • You need to draw the boxes, as above. (A|B\n---\nC|D\n)
  • You need to output all valid answers.
  • An additional trailing newline is fine.
  • , so shortest code in bytes wins.
  • Standard loopholes and T&C's apply.

This is my first submission so please do let me know if I need to clarify anything.

Denham Coote

Posted 2015-07-20T12:11:58.990

Reputation: 1 397

Could you show what the output is supposed to be? Also, what about a trailing newline character? – Spikatrix – 2015-07-20T12:18:09.650

Nice challenge. I think you need to explicitly tell what numbers need to be added. The line "The two, concatenated, two digit numbers across PLUS the two, two digit numbers down have to equal 100." is not clear enough. I only figured out using the example of 1234. Also, I am only getting 9 such combinations. Can you please mention the 16 combinations ? – Optimizer – 2015-07-20T12:18:13.823

Sure, I'll update the question to be more specific. Thanks. – Denham Coote – 2015-07-20T12:21:24.513

Sorry @Optimizer, I forgot a few solutions that contained 0 in my list. You are correct. – Denham Coote – 2015-07-20T12:28:56.437

Welcome to PPCG. For future reference, I think this would have been better if you had included zero. The reason is, with only 9 combinations it makes sense to hardcode the solutions, which is 36 characters (or 6 combinations / 24 characters if you count diagonal reflections as equivalent.) Including zero would have made it worthwhile to write code to search for all combinations. But as you already have an answer , I wouldn't change the rules now. – Level River St – 2015-07-20T13:06:06.473

@steveverrill ah, here I thought I was making it more interesting as one would need to manually exclude answers with '0' (if searching through the possible answers) – Denham Coote – 2015-07-20T13:14:17.890

So I can choose which of the 9 solutions I want to output? – Peter Taylor – 2015-07-20T15:32:15.423

Oh, sorry for not being specific. No, you should output all valid combinations. – Denham Coote – 2015-07-20T15:54:14.940

1I made a few edits to the explanation and formatting. If it doesn't match your original intent, please roll back the edit. – Alex A. – 2015-07-20T16:33:27.597

1@DenhamCoote actually, my semi-hardcoded solution was quite interesting to do, because it's only semi-hardcoded. This has been an exception to the rule, because questions where blatant hardcoding is the best / most obvious option often don't produce interesting answers (see recent borromean rings question for example.) Thank you for the question. – Level River St – 2015-07-20T17:56:14.627

Are trailing spaces in the blank lines allowedd? – kirbyfan64sos – 2015-07-20T22:11:41.040

@kirbyfan64son since the blank lines are in fact optional, sure, trailing spaces in blank lines are ok. – Denham Coote – 2015-07-20T22:18:12.167

One question for you regulars: Am I supposed to 'accept' the answer with the fewest bytes, or are the upvotes the decider? If I should, how long does one typically wait? Thank you very much to everyone who has answered so far! – Denham Coote – 2015-07-21T11:55:33.757

Accept the answer with fewest bytes, as that is what's stated in your question (in accordance with the codegolf tag.) This means languages like Pyth or Cjam nearly always win. The fairness of this has been questioned by many new users on meta, but the answer is always the same: an interesting answer in a verbose language gains more reputation from its upvotes, so failing to get accepted doesn't matter. It's strongly preferred to verify the answer yourself before accepting. BTW, questions which "win" by getting the most upvotes are tagged popularity contest (These are disliked by many people.) – Level River St – 2015-07-21T22:22:23.357

Ah, thanks for that! I didn't set a time limit on this, so how long is usually appropriate before accepting? A week? Then, if I accept and then a shorter answer comes, should I change the accepted answer? (Sorry, don't know what's considered rude. Also, why are popularity contests disliked? Surely, just don't take part if it's not someone's cup of tea?) – Denham Coote – 2015-07-21T22:32:55.387

1Accepting an answer may give the impression the question is finished, so I'd leave it a while yet. If you get more answers afterwards, you can change the acceptance, but that means you have to keep a lookout, which you may not have time to do. Popularity contests are disliked because they are often lazily written and overly broad, often of the type "Do X in the most complex way possible." Generally the only good popularity questions are in image processing, where human evaluation is the only way to decide if the algorithm is good or not. Image processing excludes pure art which is off topic. – Level River St – 2015-07-22T08:26:11.310

Answers

8

Pyth, 42 38 34 bytes

V^ST4IqC\ds*VNsM^,T1 2Xj\|N3"
---

The trailing newline in the code is important. The main reason I'm competitive is because I use the vector dot product of [1, 1, 5, 7] and [20, 11, 11, 2] and compare it to 100.

orlp

Posted 2015-07-20T12:11:58.990

Reputation: 37 067

"|" -> \| , \n -> (literal linefeed) and you don't need the final quote. – Dennis – 2015-07-20T16:40:14.700

@Dennis Was sleeping >.< – orlp – 2015-07-20T17:58:11.963

2If you don't mind, could you please add an explanation for those (like me) who can't decipher your incredibly short answer? – Denham Coote – 2015-07-21T12:24:36.620

6

Ruby, 71

As hardcoding is not disallowed (and in any case it is difficult to draw a line) here's a partial hardcoded answer.

1.upto(3){|i|1.upto(7-i*2){|j|print i,?|,j,'
---
',8-j-2*i,?|,i+6,'
'}}

Explanation

The formula for a solution is as follows:

A*20+(B+C)*11+D*2=100

By modular arithmetic arguments, we see that A and D must differ by a constant amount, such that (A*20+D*2)%11 is constant. In fact D=A+6. The i loop iterates through the three possible values of A.

The value of B can be anything from 1 to 7-i*2 and the total of B and C must be 14-A-D. Thus we obtain the following expressions, which are printed. Ruby allows literal newlines in strings enclosed in ''

   i     |    j
------------------
8-j-2*i  |   i+6

Level River St

Posted 2015-07-20T12:11:58.990

Reputation: 22 049

4

Java, 202 200 198

Trying for the first time :D

EDIT: saved 2 bytes with slightly smaller calculation found in an other comment.

class C{public static void main(String[]c){for(int i=0;++i<5;)for(int j=0;++j<7;)for(int k=0;++k<10;)for(int l=0;++l<10;)if(20*i+11*(j+k)+2*l==100)System.out.printf("%s|%s%n---%n%s|%s%n",i,j,k,l);}}

Koekje

Posted 2015-07-20T12:11:58.990

Reputation: 181

4

Batch - 187 bytes

Brute force.

@!! 2>nul||cmd/q/v/c%0&&exit/b
set y=for /l &set z= in (1,1,9)do 
%y%%%a%z%%y%%%b%z%%y%%%c%z%%y%%%d%z%set/aa=%%a%%b+%%c%%d+%%a%%c+%%b%%d&if !a!==100 echo %%a^|%%b&echo ---&echo %%c^|%%d

Un-golfed it's slightly less disgusting:

@echo off
setLocal enableDelayedExpansion
for /l %%a in (1,1,9) do (
    for /l %%b in (1,1,9) do (
        for /l %%c in (1,1,9) do (
            for /l %%d in (1,1,9) do (
                set/aa=%%a%%b+%%c%%d+%%a%%c+%%b%%d
                if !a!==100 (
                    echo %%a^|%%b
                    echo ---
                    echo %%c^|%%d
                )
            )
        )
    )
)

unclemeat

Posted 2015-07-20T12:11:58.990

Reputation: 2 302

3

CJam, 40 bytes

A4m*{[KBBY].*:+56=},{:)2/'|f*"
---
"*N}/

The approach for finding the combinations is different from @Optimizer's, but the code for printing them is identical.

Try it online in the CJam interpreter.

How it works

A4m*     e# Push all vectors of length 4 with coordinates in [0 ... 9].
         e# We'd normally use [0 ... 8] here, but "9 4m*" is 1 byte longer and
         e# "A4m*" doesn't produce any false positives.

{        e# Filter the vectors:
[KBBY].* e#   Multiply the elements of the vector by 20, 11, 11 and 2.
:+       e#   Add all four products.
56=      e#   Check if the sum is 56. 56 is used instead of 100 since all elements
         e#   of the vector will be incremented and 56 + 20 + 11 + 11 + 2 == 100.
},       e# Keep only vectors for which = pushed a truthy value.

{        e# For each vector:
:)       e#   Increment each coordinate.
2/       e#   Split into pair.
'|f*     e#   Join each pair, delimiting by '|'.
"
---
"*       e#   Join the two pairs, delimiting by "\n---\n".
N        e#   Push "\n".
}/       e#

Dennis

Posted 2015-07-20T12:11:58.990

Reputation: 196 637

Since I know very little apart from some Java, I'd love an explanation of how this works, if you're willing..? – Denham Coote – 2015-07-20T15:58:12.513

That's a nice trick. – Optimizer – 2015-07-20T16:13:44.323

Wow. I have much to learn. Thanks for the explanation :) – Denham Coote – 2015-07-20T16:34:52.463

3

Haskell, 125 121 bytes

s=show
f=putStr$unlines[s a++'|':s b++"\n---\n"++s c++'|':s d|[a,b,c,d]<-mapM id$"abcd">>[[1..9]],20*a+11*(b+c)+2*d==100]

Usage:

*Main> f
1|1
---
5|7
1|2
---
4|7
1|3
---
3|7
1|4
---
2|7
1|5
---
1|7
2|1
---
3|8
2|2
---
2|8
2|3
---
1|8
3|1
---
1|9

>> in "abcd">>[[1..9]] makes a list with 4 (length of 1st parameter) copies of the second element, i.e. [[1..9],[1..9],[1..9],[1..9]]. mapM id makes a list of all combinations thereof, i.e [0,0,0,0] to [9,9,9,9]. Keep those that sum up 100 and build a string with the box of it. Print all boxes.

Thanks @Mauris for 1 byte and making me review my post to find 3 more.

nimi

Posted 2015-07-20T12:11:58.990

Reputation: 34 639

mapM id saves a byte vs. sequence. – Lynn – 2015-08-18T19:18:48.713

3

Haskell, 107 131 bytes

s=show
r=[1..9]
v=putStr$unlines[s a++"|"++s b++"\n---\n"++s c++"|"++s d++"\n"|a<-r,b<-r,c<-r,d<-r,(2*a+b+c)*10+b+2*d+c==100]

The second version of my first Haskell program ever!

This time with display as per requirements, shamelessly stolen adapted from nimi's (well I did some research but it seems there aren't so may efficient ways to display characters in Haskell so that putStr$unlines is hard to avoid).

And...apart from the factorisation of the formula at the end, it's still readable =)

dungeoncrawler

Posted 2015-07-20T12:11:58.990

Reputation: 31

2

CJam, 43 42 bytes

A,1>4m*{2/_z+Afb:+100=},{2/'|f*"
---
"*N}/

Explanation to follow.. by today EOD

Try it online here

Optimizer

Posted 2015-07-20T12:11:58.990

Reputation: 25 836

My golfed java version (which doesn't bother with boxes, it only lists the sequences) was 197 chars. This does it all in a quarter of the length! Cool :) – Denham Coote – 2015-07-20T13:04:21.770

@DenhamCoote That's every CJam, Pyth, and GolfScript answer. – phase – 2015-07-20T19:06:23.360

Still looking forward to that explanation ;-) – Denham Coote – 2015-07-21T12:25:05.160

2

Python 2, 145 129 Bytes

I'm currently toying with a few different methods of calculating which should be shorter than what is outlined, but I will post what I have now.

i=int
for k in range(1000,9999):
 a,b,c,d=`k`
 if i(a+b)+i(c+d)+i(a+c)+i(b+d)==100and not'0'in`k`:print a+'|'+b+'\n---\n'+c+'|'+d

Kade

Posted 2015-07-20T12:11:58.990

Reputation: 7 463

1

Python 3, 159

Quick and dirty.

N='123456789'
D='%s|%s\n'
O=D+'---\n'+D
I=int
[print(O%(a,b,c,d)if I(a+b)+I(c+d)+I(a+c)+I(b+d)==100 else'',end='')for a in N for b in N for c in N for d in N]

Daniel Wakefield

Posted 2015-07-20T12:11:58.990

Reputation: 484

1

Java, 450

My first (ungolfed) attempt looked like this:

class B{
  public static void main(String[]a){
    for(int i=1;i<10;i++)
      for(int j=1;j<10;j++)
        for(int k=1;k<10;k++)
          for(int l=1;l<10;l++)
            if(Integer.parseInt(i+""+j)+Integer.parseInt(k+""+l)+Integer.parseInt(i+""+k)+Integer.parseInt(j+""+l)==100){
              System.out.println(i+"|"+j);
              System.out.println("---");
              System.out.println(k+"|"+l+"\n");
            }
  }
}

Denham Coote

Posted 2015-07-20T12:11:58.990

Reputation: 1 397

3Hint: 20*a + 11*(b + c) + 2*d == 100. – orlp – 2015-07-20T15:44:54.460

Yeah, this solution was purely string concatenation - a very humble first attempt. – Denham Coote – 2015-07-20T15:53:11.223

1

I count only 436 bytes, not 450. Also, the whitespace isn't necessary, which would save you a considerable amount.

– Alex A. – 2015-07-20T16:03:38.010

Furthermore, removing unnecessary whitespace, this should be closer to 340 bytes :) – Kade – 2015-07-21T15:08:34.950

1

R, 165 bytes

e=expand.grid(d<-1:9,d,d,d)
a=apply
o=a(e[a(e,1,function(x)20*x[1]+11*(x[2]+x[3])+2*x[4]==100),],1,function(x)cat(x[1],"|",x[2],"\n---\n",x[3],"|",x[4],"\n",sep=""))

This would have been significantly shorter had I chosen to hard-code the output in some way. Like a few other solutions, this takes advantage of the identity 20 x1 + 11 (x2 + x3) + 2 x4 = 100.

Ungolfed + explanation:

# Create a matrix where each row is a combination of the digits 1-9
e <- expand.grid(1:9, 1:9, 1:9, 1:9)

# Filter the rows of the matrix using the aforementioned identity
e <- e[apply(e, 1, function(x) 20*x[1] + 11*(x[2]+x[3]) + 2*x[4] == 100), ]

# Print each row formatted into boxes
o <- apply(e, 1, function(x) cat(x[1], "|", x[2], "\n---\n", x[3], "|", x[4], sep = ""))

You may be wondering why the last statement is an assignment. As it turns out, the cat function, which concatenates and prints, returns a value of NULL. When you call cat from within a function like apply, the output will be followed by NULL, which is undesirable. There are two ways around this: assign it to a variable or wrap it in invisible. Here I've opted for the former since it's significantly shorter.

You can try it online.

Alex A.

Posted 2015-07-20T12:11:58.990

Reputation: 23 761

1

PowerShell , 98

adapted steveverrill's formula

:\>cat printbox.ps1

1..9|%{for($j=1;$j-lt10;$j++){if(($k=(8-$j-2*$_))-gt0){"{0}|{1}`n---`n{2}|{3}"-f$_,$j,$k,
($_+6)}}}

:\>powershell -f printbox.ps1
1|1
---
5|7
1|2
---
4|7
1|3
---
3|7
1|4
---
2|7
1|5
---
1|7
2|1
---
3|8
2|2
---
2|8
2|3
---
1|8
3|1
---
1|9

blabb

Posted 2015-07-20T12:11:58.990

Reputation: 219