4
\$\begingroup\$

It seems flags are a trend now, so what better place to start an account? I thought bars could use a twist to them, so I found this flag.

Produce the following in the console output the fewest possible bytes:

+----------------------------------+
|XXXXX                             |
|XXXXXXXX                          |
|XXXXXXXXX-------------------------+
|XXXXXXXXXXX                       |
|XXXXXXXXXXX                       |
|XXXXXXXXX-------------------------+
|XXXXXXXX                          |
|XXXXX                             |
+----------------------------------+

This crude rendition of the flag is 36 x 10 characters. A trailing newline on the last line is optional. You may use any language you choose. If it is an obscure language, please post a link to the language in your answer.

\$\endgroup\$
  • 23
    \$\begingroup\$ I am not a fan of the arbitrary "The answer must have fewer bytes than this flag..." as it discourages some really neat languages. They won't win anyway, so let them have their fun :) \$\endgroup\$ – BrainSteel Jul 7 '15 at 1:03
  • 1
    \$\begingroup\$ You have a point. Editing! \$\endgroup\$ – CamelCaseCode Jul 7 '15 at 1:04
  • 13
    \$\begingroup\$ Why did you accept an answer after 1 hour? That's way too short, IMHO. You should give people some time to submit answers before you pick a winner. \$\endgroup\$ – Reto Koradi Jul 7 '15 at 2:46
  • 9
    \$\begingroup\$ It seems that every successive flag question has approximately half the votes of the previous one. If this keeps up, we should soon have a question with 1/1024 of a vote. \$\endgroup\$ – Level River St Jul 7 '15 at 10:20
  • 3
    \$\begingroup\$ i.imgur.com/R7bK9rc.png \$\endgroup\$ – DJMcMayhem Jul 7 '15 at 22:23

14 Answers 14

9
\$\begingroup\$

CJam, 44 bytes

8T5@9B]_W%+{I'|'+?'XI*34"-  "I=e]"+||"I=N}fI

(Thanks to @Dennis, @Jakube and @Optimizer for -1 byte each)

8T5@9B]        Push [0 5 8 9 11]
_W%+           Add its reverse, to give [0 5 8 9 11 11 9 8 5 0]
{...}fI        For each I in the above array...
  I'|'+?         Push "+" if I == 0 else "|"
  'XI*             Push I "X"s
  34"-  "I=e]      Pad the "X"s to length 34 with "-" if I%3 == 0 else " "
  "+||"I=          Push "+" if I%3 == 0 else "|"
  N                Push a newline
\$\endgroup\$
  • \$\begingroup\$ "|+"I!= -> I'|'+? . And something similar for the "- "I= and "+||"I= too \$\endgroup\$ – Optimizer Jul 7 '15 at 7:33
14
\$\begingroup\$

SQL (PostgreSQL flavour), 284 244 232 210 175 bytes

A HUGE Thank You to @manatwork for essentially the whole thing now.

A nice verbose language that still came in under the byte size of the flag, with a bit of a margin :)

Basically builds the half of the flag with using parameters. This is cross joined with 1 and -1 giving us both halfs of the flag and a means to order it by negating the number of #'s in the row.

SELECT rpad(rpad(L,X,'X'),35,P)||R FROM(VALUES('|',12,' ','|'),('|',10,'-','+'),('|',9,' ','|'),('|',6,' ','|'),('+',1,'-','+'))F(L,X,P,R),(values(-1),(1))G(O)ORDER BY(12-X)*O

Formatted and commented

SELECT rpad(rpad(L,X,'X'),35,P)||R --Build each row from parameters
FROM(VALUES           -- Parameters for each row
    ('|',12,' ','|'), -- Start Char, Num of #, Pad Char, End Char
    ('|',10,'-','+'),
    ('|',9,' ','|'),
    ('|',6,' ','|'),
    ('+',1,'-','+')
    )F(L,X,P,R),
    (values(-1),(1))G(O) -- Cross join with order modifier
ORDER BY(12-X)*O -- Order by modified number of #
\$\endgroup\$
  • \$\begingroup\$ Change those '|XXXXX'||repeat(' ',29) into rpad('|XXXXX',35) to reduce it to 263 characters. \$\endgroup\$ – manatwork Jul 14 '15 at 20:26
  • \$\begingroup\$ By the way, the sub-select is pointless, just the parenthesis does it: SELECT S FROM F UNION ALL(SELECT S FROM F ORDER BY I DESC) to reduce it to 250 characters. (At least in 9.4.4.) \$\endgroup\$ – manatwork Jul 14 '15 at 20:31
  • \$\begingroup\$ @manatwork Thanks for the tips. Worked out really nicely. \$\endgroup\$ – MickyT Jul 14 '15 at 20:47
  • \$\begingroup\$ The I field is not necessary, as the strings that compose the flag are already in ascending order. Getting rid of that auxiliary field has the benefit of not needing the aliases anymore. And as asc is ``shorter than desc, you can squeeze one more characters by declaring the flag in reverse order, then reverse the top half using the shorter keyword: WITH F AS(SELECT*FROM(VALUES(rpad('|XXXXXXXXXXX',35)||'|'),(rpad('|XXXXXXXXX',35,'-')||'+'),(rpad('|XXXXXXXX',35)||'|'),(rpad('|XXXXX',35)||'|'),(rpad('+',35,'-')||'+'))F)(SELECT*FROM F ORDER BY 1ASC)UNION ALL SELECT*FROM F \$\endgroup\$ – manatwork Jul 15 '15 at 7:26
  • \$\begingroup\$ @manatwork Unfortunately the row starting with '+' gets ordered into the wrong position. Otherwise that would have worked a treat. I could have also dropped the ASC. But I have adapted your suggestion to get a few more bytes. \$\endgroup\$ – MickyT Jul 15 '15 at 19:26
8
\$\begingroup\$

Python 2, 80

for n in 0,5,8,9,11,11,9,8,5,0:print'|+'[n<1]+'X'*n+'-  '[n%3]*(34-n)+'+||'[n%3]

Instead of iterating over the row numbers, iterate over the number of X-symbols directly. The stripes happen where n is 0 or 9, which are the only multiples of 3, and so can be checked by the value n%3 (thanks Sp3000).

This can surely be shortened in Python 3 by converting the list of numbers to a bytes object.

\$\endgroup\$
  • \$\begingroup\$ I was all proud of mine, and yours is just the same except better :P \$\endgroup\$ – undergroundmonorail Jul 8 '15 at 18:33
8
\$\begingroup\$

CJam, 52 51 50 bytes

8U5@9B]34X$f-]z87Zb"X ||X-++X-+|"4/f=..*1fm>_W%+N*

Thanks to @Sp3000 for golfing off 1 byte!

Try it online in the CJam interpreter.

How it works

8U5@9B] e# Push [0 5 8 9 11] (`@` rotates on top).
34X$f-  e# Subtract each integer from 34.
]z      e# Wrap and zip. This pushes [[0 34] [5 29] [8 26] [9 25] [11 23]].
87Zb    e# Push [1 0 0 2 0] (87 in base 3).

"X ||X-++X-+|"4/

        e# Push ["X ||" "X-++" "X-+|"].

f=      e# Select for each. This pushes ["X-++" "X ||" "X ||" "X-+|" "X ||"].

..*     e# Twofold vectorized repetition.
        e# EXAMPLE: [9 25]"X-+|".* -> "XXXXXXXXX-------------------------+|"

1fm>    e# Rotate each string one character to the right.  
_W%     e# Copy the resulting array and reverse the order of its lines.
+N*     e# Concatenate and join by linefeeds.
\$\endgroup\$
5
\$\begingroup\$

JavaScript (ES6), 147 132 127 111 106 bytes

[0,5,8,9,11,11,9,8,5,0].map(v=>console.log('|+'[+!v]+'X'.repeat(v)+' -'[z=+!(v%3)].repeat(36-v)+'|+'[z]))

Demo

As it is ES6, it only works in Firefox at the moment.

// DEMO: redefine console.log to have output inside the snippet
// Taken from https://codegolf.stackexchange.com/a/52411/22867
console.log = (...x) => O.innerHTML += x + '\n';

// Actual program
[0,5,8,9,11,11,9,8,5,0].map(v=>console.log('|+'[+!v]+'X'.repeat(v)+' -'[z=+!(v%3)].repeat(36-v)+'|+'[z]))
<pre><output id=O></output></pre>

\$\endgroup\$
  • \$\begingroup\$ -6: [0,5,8,9,11,11,9,8,5,0].map(v=>console.log('|+'[+!v]+'X'.repeat(v)+' -'[z=+!(v%9)].repeat(36-v)+'|+'[z])) \$\endgroup\$ – nderscore Jul 7 '15 at 20:45
4
\$\begingroup\$

C, 157 155 151 146 bytes

i;f(){for(char b[36];i<10;printf("%c%.34s%c\n",i++%9?124:43,b,i%3?124:43))memset(b,i%3?32:45,34),i%9?memset(b,120,35253>>((i<5?5-i:i)%4*4)&15):0;}

edit: I moved the string length inside of the format string and found two bytes.

edit: Added Cool Guy's recommendation and found four bytes.

edit: Removed the array and replaced it with a 4-bit packed short that's shifted to get the 'x' lengths

\$\endgroup\$
  • \$\begingroup\$ I'm having issues compiling this. \$\endgroup\$ – CamelCaseCode Jul 7 '15 at 2:13
  • \$\begingroup\$ Let me know what error you're receiving and I will see if I can recreate it. I'm compiling using Cygwin GCC 4.8.3 using no compiler flags other than -o. \$\endgroup\$ – openaddr Jul 7 '15 at 2:51
  • \$\begingroup\$ @CamelCaseCode This requires C99 or above. BTW, don't care about the warnings... \$\endgroup\$ – Spikatrix Jul 7 '15 at 6:17
  • 1
    \$\begingroup\$ @openaddr for(;i<10;) --> for(char b[36];i<10;printf("%c%.34s%c\n",i++%9?124:43,b,i%3?124:43) saves two bytes. Also, you can remove the {...} of the loop and change ;i%9? to ,i%9?. +1 anyway. Nice solution! \$\endgroup\$ – Spikatrix Jul 7 '15 at 6:40
  • 1
    \$\begingroup\$ Apparently, That does not matter. In code-golf, portability is not required. And (int[]){5,11,9,8} is a C99 feature. \$\endgroup\$ – Spikatrix Jul 7 '15 at 6:57
4
\$\begingroup\$

CJam, 60 54 50 48 45 bytes

Compared to the previous version, I realized that one of the 5 entries in my descriptors was always the same, which was kind of pointless. Using a fixed value for it:

"++-0|| 5|| 8|+-9|| B"4/_W%+{1/~~'X*34@e]\N}/

Try it online

Thanks to @aditsu and @Sp3000 for some related help in chat this morning.

The basic approach here is that I have 4 character descriptors for the pattern of each line, where the characters in the descriptor are:

  • First character.
  • Last character.
  • Right fill character.
  • Repeat count for left fill character.

The left fill character, which was part of the descriptor in earlier versions of this solution, is always X, so it is now not part of the descriptor anymore.

These descriptors are specified for the top 5 lines, and repeated for the bottom 5 lines in reverse order.

Explanation:

"++-0|| 5|| 8|+-9|| B"
        Descriptors for top 5 lines, as described above.
4/      Split into 5 descriptors of 4 characters each.
_W%     Repeat the 5 descriptors in reverse order.
{       Loop over lines/descriptors.
    1/    Split descriptor string into array with 5 one-character strings.
    ~     Unwrap the array.
    ~     Evaluate last entry in descriptor, which is the count.
    'X    Left fill character.
    *     Repeat left fill character by given count.
    34@e] Pad to 34 characters using right fill character.
    \     Swap last character to top.
    N     Add a newline.
}/    End loop over lines/descriptors.
\$\endgroup\$
2
\$\begingroup\$

Python 3, 282 bytes/characters

f,l,h=lambda a:a in{0,35}and '+'or'-',lambda a:a%3 and'|'or'+',lambda a:a and(a<10 and 'X'or'-')or'|';print("\n".join("".join(x)for x in[[k in{0,9}and f(j)or j==35 and l(k)or k%3 and(j==0 and'|'or j<[0,6,9,0,12,12,0,9,6][k]and'X'or' ')or h(j)for j in range(36)]for k in range(10)]))

Output is, of course:

+----------------------------------+
|XXXXX                             |
|XXXXXXXX                          |
|XXXXXXXXX-------------------------+
|XXXXXXXXXXX                       |
|XXXXXXXXXXX                       |
|XXXXXXXXX-------------------------+
|XXXXXXXX                          |
|XXXXX                             |
+----------------------------------+

I'm sure there's a much shorter way to do this in Python.

\$\endgroup\$
  • \$\begingroup\$ You can use indexing to reduce a fair bit of the ... and ... or ..., e.g. "+||"[a%3] \$\endgroup\$ – Sp3000 Jul 7 '15 at 3:20
  • 2
    \$\begingroup\$ Also, I'm not sure the lambdas are necessary... I don't think you call them enough times for it to be worth it \$\endgroup\$ – Sp3000 Jul 7 '15 at 3:22
  • \$\begingroup\$ @Sp3000, you're probably right. If I remember to I'll refactor it later. \$\endgroup\$ – Fox Wilson Jul 7 '15 at 22:13
2
\$\begingroup\$

Ruby, 78

"@EHIKKIHE@".each_byte{|i|puts"+|"[i/65]+?X*(i-64)+" -"[i%3]*(98-i)+"|+"[i%3]}

A copy of Rinkattendant's / Xnor's idea of iterating over the lengths of the strings of X's. Except instead of using an array, I have compressed them into a string.

Ruby, 96

Original plain vanilla approach

(0..9).each{|i|puts (i%9<1??+:?|)+(?X*("@EHIKKIHE@"[i].ord-64)).ljust(34,"-  "[i%3])+"+||"[i%3]}

Ungolfed

(0..9).each{|i|
   puts (i%9<1??+:?|)                                          #first character + or |
   +(?X*("@EHIKKIHE@"[i].ord-64)).ljust(34,"-  "[i%3])         #print quantity of X's according to string, then right justify with - or space
   +"+||"[i%3]}                                                #last character + or |
\$\endgroup\$
2
\$\begingroup\$

Ruby, 156 155 chars

h=[5,8,9,11];l=34;t=?++(?-*l)+?+;puts t;2.times{h.each{|c|if c==9 then j='-+' else j=' |';end;print(?|,((?X*c).ljust(l,j[0])),j[1],?\n)};h.reverse!};puts t

I used an array, h, with the values [5, 8, 9, 11], so I can use 2.times { h.each { code }; h.reverse } because the flag is symmetrical horizontally. I also use 34 many times in the code, so it is assigned to l. ?x is shorthand for "x".

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

Swift 1.2, 157 bytes

Can be run in a playground, or from a file using swift file.swift in Terminal.

let z="-----",x="XXX",y="   ",g=y+y,w=z+z+z+z+"----+",e=g+g+g+y+"  |",c="|XX"+x,d=["+"+z+z+w,c+g+e,c+x+y+e,c+x+"X-"+w,c+x+x+e]
print("\n".join(d+reverse(d)))
\$\endgroup\$
1
\$\begingroup\$

PHP, 135 bytes

Lets play around with printf:

<?$A='|%-34s|
|%-34s|';$B="|%'--34s|";printf("+%'-34s+
$A
$B
$A
$B
$A
+%'-34s+",'',$a=XXXXX,$b=$a.XXX,$c=$b.X,$d=$b.XX,$d,$c,$b,$a,'');

Maybe there are some edges that I've left behind that can be cut.

But it was fun!

\$\endgroup\$
0
\$\begingroup\$

Sed - 228

s/.*/eeeeee\neeeeee/
s/e/------/g
s/^-/+/
s/-$/+/
s/-\n-/+\n+/
s/\n/\nabcddcba/
s/\([a-d]\)/|XXXXX\1\1\1\1\1\1\n/g
s/\([a-d]\)/\1\1\1\1\1/g
s/Xbbb/XXXX/g
s/Xcccc/XXXXX/g
s/Xdddddd/XXXXXXX/g
y/abcd/  - /
s/ \n/|\n/g
s/-\n/+\n/g
q

Because of the way sed works, this requires a line of input to output the flag.

\$\endgroup\$
0
\$\begingroup\$

Haskell, 233 bytes

putStr$uncurry((.toEnum).replicate)=<<foldl1(++)(((++)=<<reverse)$zipWith zip[[1,11,23,1,1],[1,9,25,1,1],[1,8,26,1,1],[1,5,29,1,1],[1,34,1,1]][[124,88,32,124,10],[124,88,45,43,10],[124,88,32,124,10],[124,88,32,124,10],[43,45,43,10]])
\$\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.