24
\$\begingroup\$

The Borromean rings are a curious set of three circles, linked such that the removal of any one of them will unlink the other two:

enter image description here

You can make a set for yourself with a couple rubber bands and a binder ring. With more rubber bands, it's easy to make any Brunnian link.

Write a program or function that outputs (prints or returns) this ascii-art representation of Borromean rings:

    +-----+
    |     |
+-----+   |
|   | |   |
| +-|---+ |
| | | | | |
| | +-|---+
| |   | |
+-|---+ |
  |     |
  +-----+

The lines may have trailing spaces and there may be a trailing newline.

The shortest code in bytes wins.

\$\endgroup\$
  • 2
    \$\begingroup\$ I have to say that this is a pretty hard challenge because of how simple the expected output is \$\endgroup\$ – Beta Decay Jul 20 '15 at 5:20
  • 3
    \$\begingroup\$ I'm a little disappointed. Thought the challenge would be to take an integer size and output rings of that size. \$\endgroup\$ – Blacklight Shining Jul 20 '15 at 13:42
  • \$\begingroup\$ yeah, I thought so too (program takes int as an input and then draws Brunnian Link with that many components, but that is not unique, maybe a number of crossings?). That kind of program would have to actually do search (or at least trial and error - do these rings interlock and if I remove one, do the go free?) instead of just drawing a fixed picture... \$\endgroup\$ – alexey Jul 20 '15 at 23:36
7
\$\begingroup\$

CJam, 53 51 50 49 bytes

Plain old base conversion...

"FÓîÞ¤ÛY­ËB[¢O²êÍÓ
}²|äG"299b4b"+ -|"f=B/N*

All characters are well in extended ASCII range (ASCII code 1 to 255), so number of characters == number of bytes.

Try it online here and get the original code here

\$\endgroup\$
  • \$\begingroup\$ Just curious, where is the newline in your lookup? \$\endgroup\$ – Maltysen Jul 20 '15 at 6:47
  • \$\begingroup\$ @Maltysen I don't have it. B/N* splits by 11 characters and joins be newline \$\endgroup\$ – Optimizer Jul 20 '15 at 6:48
  • \$\begingroup\$ that's coooool. \$\endgroup\$ – Maltysen Jul 20 '15 at 6:48
6
\$\begingroup\$

Pyth - 51 bytes

I'm sure someone's gonna beat this quick, but just a base compression answer cuz I'm feeling lazy. I'll try to write a serious answer soon.

s@L"
 +-|"jC" zB²;¶Ê ¿ïÁ»#-ÌClHõy%|ap"5

Try it here online.

s              Reduce on string concatenation
 @L            Map second arg to index first arg
  "..."        String of all chars (Pyth allows literal newlines)
  j            Base conversion to list
   C           Base conversion 256 -> 10
    "..."      Base 256 string
   5           To Base 5
\$\endgroup\$
4
\$\begingroup\$

Pyth, 49 bytes

jbc11s@L"+ -|"jC"Tª]UʨWÕÝ_K¨}ÝÝ÷K¨Ý]Òê]UÕ*¡"4

Demonstration.

This uses base 4 encoding, and chops the string into elevenths, then rejoins them on newlines.

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

Ruby, 110

-2.upto(8){|i|s=" "*(i%6)+"+-----+"*(1-i%2)+" "*9
6.times{|j|"@d me?K[RR@"[i+2].ord>>j&1>0&&s[j*2]=?|}
puts s}

Something different from straight base conversion.

Ungolfed:

-2.upto(8){|i|                                           #for each line
  s=" "*(i%6)+"+-----+"*(1-i%2)+" "*9                    #load s with "+-----+" (if required!) padded appropriately with leading spaces and with nine trailing spaces.   
  6.times{|j|"@d me?K[RR@"[i+2].ord>>j&1>0&&s[j*2]=?|}   #replace characters with | as necessary, according to the 6-bit number encoded by each character in the magic string.
  puts s}                                                #print the line.
\$\endgroup\$
3
\$\begingroup\$

Ruby, 117 bytes

Not winning, but I thought it was a cute approach:

puts'    --
    |     |
--  |
|   | |   |
| -||
| | | | | |
| | -|
| |   | |
-||
  |     |
  --'.gsub /-./,'+\0---+ '
\$\endgroup\$
2
\$\begingroup\$

BrainFuck, 361 bytes

Here is a little BrainFuck program, only printing char by char.

+++++++++[->>>>++++++++++++++<+++++<+++++<++++<]>----....>--.>.....>-->++++++++++<<<.>>>.<<<<....>>>.<<<.....>>>.>.<<<.>.....<.<...>>>.>.<.<<<...>>>.<<<.>>>.<<<...>>>.>.<.<<<.>.>.>.<...<.<.>>>.>.<.<<<.>>>.<<<.>>>.<<<.>>>.<<<.>>>.<<<.>>>.>.<.<<<.>>>.<<<.>.>.>.<...<.>>>.<.<<<.>>>.<<<...>>>.<<<.>>>.>.<<<.>.>.<...<.<.>>>.>.<<<<..>>>.<<<.....>>>.>.<<<<..>.>.....<.
\$\endgroup\$
1
\$\begingroup\$

Staq, 109 chars

&iiiqi{1" "}{211}{c"| "}{fcc}{o"+-|"}{p"+--"}{r"---+"}{ec;}22pr;22c22epr21ec2f2ecor1effcefor;f2ceor1e2c22e2pr

output:

Executing D:\codegolf\Staq borromean rings.txt

    +-----+
    |     |
+-----+   |
|   | |   |
| +-|---+ |
| | | | | |
| | +-|---+
| |   | |
+-|---+ |
  |     |
  +-----+

Execution complete.
>
\$\endgroup\$
0
\$\begingroup\$

Python 3, 139 bytes

This is the closest I can get to printing it directly (which would be 134 bytes) without actually doing so.... I'm not sure how to shorten it any more.

a='+-----+'
b='+-|---+'
c=' '*4
d='| '
e=c+d
print(c+a,e*2,a+e[1:],"|   | "*2,d+b+" |",d*6,d+d+b,"| |   "*2,b+" |",e[2:]+e,"  "+a,sep='\n')
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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