13
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Could you show what the output is supposed to be? Also, what about a trailing newline character? \$\endgroup\$ – Spikatrix Jul 20 '15 at 12:18
  • \$\begingroup\$ 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 ? \$\endgroup\$ – Optimizer Jul 20 '15 at 12:18
  • 1
    \$\begingroup\$ I made a few edits to the explanation and formatting. If it doesn't match your original intent, please roll back the edit. \$\endgroup\$ – Alex A. Jul 20 '15 at 16:33
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ – Level River St Jul 20 '15 at 17:56
  • 1
    \$\begingroup\$ Accepting 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. \$\endgroup\$ – Level River St Jul 22 '15 at 8:26

13 Answers 13

8
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ "|" -> \| , \n -> (literal linefeed) and you don't need the final quote. \$\endgroup\$ – Dennis Jul 20 '15 at 16:40
  • \$\begingroup\$ @Dennis Was sleeping >.< \$\endgroup\$ – orlp Jul 20 '15 at 17:58
  • 2
    \$\begingroup\$ If you don't mind, could you please add an explanation for those (like me) who can't decipher your incredibly short answer? \$\endgroup\$ – Denham Coote Jul 21 '15 at 12:24
6
\$\begingroup\$

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
\$\endgroup\$
4
\$\begingroup\$

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);}}
\$\endgroup\$
4
\$\begingroup\$

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
                )
            )
        )
    )
)
\$\endgroup\$
3
\$\begingroup\$

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#
\$\endgroup\$
  • \$\begingroup\$ Since I know very little apart from some Java, I'd love an explanation of how this works, if you're willing..? \$\endgroup\$ – Denham Coote Jul 20 '15 at 15:58
  • \$\begingroup\$ That's a nice trick. \$\endgroup\$ – Optimizer Jul 20 '15 at 16:13
  • \$\begingroup\$ Wow. I have much to learn. Thanks for the explanation :) \$\endgroup\$ – Denham Coote Jul 20 '15 at 16:34
3
\$\begingroup\$

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 =)

\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ mapM id saves a byte vs. sequence. \$\endgroup\$ – Lynn Aug 18 '15 at 19:18
2
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

CJam, 43 42 bytes

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

Explanation to follow.. by today EOD

Try it online here

\$\endgroup\$
  • \$\begingroup\$ 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 :) \$\endgroup\$ – Denham Coote Jul 20 '15 at 13:04
  • \$\begingroup\$ @DenhamCoote That's every CJam, Pyth, and GolfScript answer. \$\endgroup\$ – phase Jul 20 '15 at 19:06
  • \$\begingroup\$ Still looking forward to that explanation ;-) \$\endgroup\$ – Denham Coote Jul 21 '15 at 12:25
1
\$\begingroup\$

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]
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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");
            }
  }
}

\$\endgroup\$
  • 3
    \$\begingroup\$ Hint: 20*a + 11*(b + c) + 2*d == 100. \$\endgroup\$ – orlp Jul 20 '15 at 15:44
  • \$\begingroup\$ Yeah, this solution was purely string concatenation - a very humble first attempt. \$\endgroup\$ – Denham Coote Jul 20 '15 at 15:53
  • 1
    \$\begingroup\$ I count only 436 bytes, not 450. Also, the whitespace isn't necessary, which would save you a considerable amount. \$\endgroup\$ – Alex A. Jul 20 '15 at 16:03
  • \$\begingroup\$ Furthermore, removing unnecessary whitespace, this should be closer to 340 bytes :) \$\endgroup\$ – Kade Jul 21 '15 at 15:08
1
\$\begingroup\$

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
\$\endgroup\$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.