46
\$\begingroup\$

Task: Output the Swiss flag.

Happy Swiss National Day / Schweizer Bundesfeiertag / Fête nationale suisse / Festa nazionale svizzera / Fiasta naziunala svizra!

Details: The flag consists of a white (#FFFFFF) cross on a red (#FF0000) background. The cross consists of a 6x6 square with arms of size 6x7 glued to each side. The cross is in the center of the 32x32 square background having a distance of 6 units to each side. (Source)

The output size can be chosen by the participant, but the image must exactly match these proportions, this means its size must be a multiple of 32.

A minimal version would look like so:

\$\endgroup\$
  • 1
    \$\begingroup\$ In some versions of the flag challenges, colored ASCII output was allowed, but you have not included ascii-art. Just to confirm, ASCII output is not allowed? \$\endgroup\$ – Stephen Jul 31 '17 at 22:25
  • 1
    \$\begingroup\$ Right, only the widely known image formats are allowed as well as output on the screen. \$\endgroup\$ – flawr Jul 31 '17 at 22:26
  • 20
    \$\begingroup\$ echo 🇨🇭 - I assume this is not allowed \$\endgroup\$ – Digital Trauma Jul 31 '17 at 23:03
  • \$\begingroup\$ I see a blue CH, I don't see why this should be allowed. \$\endgroup\$ – flawr Jul 31 '17 at 23:18
  • 1
    \$\begingroup\$ @Tutleman I think it does \$\endgroup\$ – Luis Mendo Aug 1 '17 at 0:33

49 Answers 49

2
\$\begingroup\$

Ruby + ruby2d, 106+8 = 114 bytes

Uses the -rruby2d flag.

[[32,32],[6,20,13,6],[20,6,6,13]].map{|w,h,x,y|Rectangle.new width:w,height:h,x:x,y:y}[0].color='red'
show

Image output:

Ruby 2D output

\$\endgroup\$
2
\$\begingroup\$

LibreLogo, 58 bytes

Code:

pc [3]fc [5]square 25 fc [3]rectangle[4,15]rectangle[15,4]

Result:

enter image description here

Explanation:

pc [3]                   ; Pen Color  = #FFFFFF
fc [5]                   ; Fill Color = #FF0000
square 25                ; Square     = 25 pt
fc [3]                   ; Fill Color = #FFFFFF
rectangle [4, 15]        ; Rectangle  = 4 x 15 pt
rectangle [15, 4]        ; Rectangle  = 15 x 4 pt
\$\endgroup\$
2
\$\begingroup\$

Processing, 85 bytes

size(32,32);background(#FF0000);fill(255);noStroke();rect(6,13,20,6);rect(13,6,6,20);

A very straightforward implementation. Open the window to the minimum size, draw a red background, set the fill to white and shapes to not have borders, and draw two rectangles.

Swiss flag in a Processing drawing window.

\$\endgroup\$
2
\$\begingroup\$

Haskell, 112 bytes

import Graphics.Gloss
(#)=rectangleSolid
main=display(InWindow""(32,32)(0,0))red$color white$pictures[6#20,20#6]

A full program that uses the Gloss library and opens a window of size 32x32 with red background and two white rectangles (size 6x20 and size 20x6) placed at the center of the window.

\$\endgroup\$
2
\$\begingroup\$

C#, 195 bytes

Try it online(Rextester)!

Hi, this is my first code golf and I'm a C# programmer, so I thought I might submit this entry that I quickly made. It correctly outputs a 32 x 32 bitmap image called "s" in the directory of the executable.

var b=new Bitmap(32,32);var g=Graphics.FromImage(b);g.Clear(Color.Red);g.FillRectangle(Brushes.White,new Rectangle(13,0,6,32));g.FillRectangle(Brushes.White,new Rectangle(0,13,32,6));b.Save("s");

Explanation:

C# offers an extensive GDI+ image manipulation APIs built into the default .NET framework. The System.Drawing namespace contains two such very powerful wrapper classes: Bitmap and Graphics. I merely used those two classes to generate a Swiss flag bitmap.

Ungolfed:

// Creates a new 32 x 32 bitmap object.
var b = new Bitmap(32, 32);
// Creates a new graphics object from the bitmap image.
var g = Graphics.FromImage(b);
// Clears the bitmap with a red(ARGB: FFFF0000) fill color.
g.Clear(Color.Red);
// Draws the vertical rectangle with a white(ARGB: FFFFFFFF) color.
g.FillRectangle(Brushes.White, new Rectangle(13, 0, 6, 32));
// Draws the horizontal rectangle with a white(ARGB: FFFFFFFF) color.
g.FillRectangle(Brushes.White, new Rectangle(0, 13, 32, 6));
// Save the bitmap to a file named: "s."
b.Save("s");
\$\endgroup\$
  • \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ – Stephen Aug 3 '17 at 18:58
2
\$\begingroup\$

HTML + CSS 215+80 bytes

Try Online

CSS

table{ all:unset } th{ background:red }
   #a{ height: 6 } #b{ height: 7 }
   #c{  width: 6 } #d{  width: 7 }

HTML

<table>
    <tr id=a> <th                                    colspan=5>
    <tr id=b> <th id=c> <th id=d> <td id=c> <th id=d> <th id=c>
    <tr id=a> <th id=c> <td id=d> <td id=c> <td id=d> <th id=c>
    <tr id=b> <th id=c> <th id=d> <td id=c> <th id=d> <th id=c>
    <tr id=a> <th                                    colspan=5>

enter image description here

Inaccurate Version 223+40 179+40 139+40 bytes

  • saved 44 bytes, thanks to @manatwork
  • saved 40 bytes, thanks to @SteveBennett

Try Online

CSS

table{ all:unset } th{ background:red }

HTML

<table>
    <tr> <th                     colspan=5>
    <tr> <th colspan=2> <td> <th colspan=2>
    <tr> <th>   <td>    <td>    <td>   <th>
    <tr> <th colspan=2> <td> <th colspan=2>
    <tr> <th                     colspan=5>

enter image description here

\$\endgroup\$
  • 3
    \$\begingroup\$ Use shorthand properties where possible: backgroud-colorbackground; merge cells: <td><td><td><td><td><td colspan=5>; write crappy HTML (most of the world does it anyway and they are not even codegolfing): classid. liveweave.com/oVLiqH \$\endgroup\$ – manatwork Aug 2 '17 at 10:29
  • \$\begingroup\$ @manatwork note taken \$\endgroup\$ – Khaled.K Aug 2 '17 at 10:47
  • 1
    \$\begingroup\$ Save a lot of characters by using th for the red cells, instead of r class: <table> <tr><th colspan=5> <tr><th colspan=2><td><th colspan=2> <tr><th><td><td><td><th> <tr><th colspan=2><td><th colspan=2> <tr><th colspan=5> liveweave.com/0WQ3Vm \$\endgroup\$ – Steve Bennett Aug 3 '17 at 5:40
  • \$\begingroup\$ @SteveBennett note taken \$\endgroup\$ – Khaled.K Aug 3 '17 at 15:25
  • \$\begingroup\$ Also I have to point out that it sadly doesn't technically meet the requirements, because the ratios aren't exactly right. The arms of the cross need to be slightly longer (7/6th) than their widths. \$\endgroup\$ – Steve Bennett Aug 3 '17 at 23:42
2
\$\begingroup\$

Excel VBA, 315 313 bytes

2 bytes saved thanks to Taylor Scott!

Golfed

Sub m
Set r=[A1:AF32]
r.RowHeight=48
r.NumberFormat=";"
Set p=r.FormatConditions.AddColorScale(2).ColorScaleCriteria
s 1,255,p
s 2,-1,p
r.Formula=Replace("=IFS(OR(ROW()<6,ROW()>26,|<6,|>26),0,OR(AND(ROW()>13,ROW()<19),AND(|>13,|<19)),1,1,0)", "|", "COLUMN()")
End Sub
Sub s(n,v,p)
p(n).FormatColor.Color=v
End Sub

And commented

Sub m
Set r=[A1:AF32]          'Evaluates range reference to return 32 square range object
r.RowHeight=48           'set row height to make grid cells square - assumes default font
r.NumberFormat=";"       'make text in cells invisible
Set p=r.FormatConditions.AddColorScale(2).ColorScaleCriteria 
                         'add 2-colour scale conditional formatting
s 1,255,p                'where lowest value is red
s 2,-1,p                 'highest is white
r.Formula=Replace("=IFS(OR(ROW()<6,ROW()>26,|<6,|>26),0,OR(AND(ROW()>13,ROW()<19),AND(|>13,|<19)),1,1,0)", "|", "COLUMN()") ''#apply grid of 1s and 0s is flag shape
End Sub
Sub s(n,v,p)             'exploits ByRef default (thanks Gaffi)
p(n).FormatColor.Color=v 'set colour for 2-colour scale conditional formatting
End Sub

I wanted to do something that didn't involve looping

Assumes font is the default Calibri size 11

\$\endgroup\$
  • 1
    \$\begingroup\$ My preliminary measurements suggest that you would be better off using a row height of 48 rather than 42 as this (so far as I can see) makes the individual cells square - aside from that, you ought to remove the () in m() as it is not necessary; You ought to put in an uncommented version of your code as well, so that others may easily verify the functionality and length of your code - Cheers \$\endgroup\$ – Taylor Scott Aug 2 '17 at 17:46
  • \$\begingroup\$ You can make some small optimizations to this for -6 bytes : ... Set c=Cells c.RowHeight=48 c.NumberFormat=";" Set p=c.FormatConditions..... ...[A1:AF32]=Replace(... \$\endgroup\$ – Taylor Scott Aug 2 '17 at 20:34
2
\$\begingroup\$

C++ with OpenGL, 373 Bytes.

This is my first time really working with C++ and OpenGL on a challenge, so let me know what I can improve.

#include<GL/glut.h>
#define v glVertex2d
#define r(w,x,y,z) v(w,x);v(w,z);v(y,z);v(y,x);
void d(){glScaled(1/16.0f,1/16.0f,1);glBegin(GL_QUADS);glColor3f(1,0,0);r(-16,-16,16,16)glColor3f(1,1,1);r(-3,-10,3,10)r(-10,-3,10,3)glEnd();glFlush();}int main(int c,char**a){glutInit(&c,a);glutInitWindowSize(320,320);glutCreateWindow("");glutDisplayFunc(d);glutMainLoop();return 0;}

Compiled with the MinGW version of g++ on Windows with FreeGlut.

Creates a 320 x 320 screen displaying the Flag. flag

Whenever a redraw happens, it spawns a smaller flag inside itself. This is because I never clear the screen, nor reset the matrix.

bug

However, a redraw does not happen automatically.

Ungolfed Code

#include <GL/glut.h>
#define rect(w,x,y,z) glVertex2d(w,x);glVertex2d(w,z);glVertex2d(y,z);glVertex2d(y,x);
void display(){
    glClear();
    glScaled(1/16.0f,1/16.0f,1);
    glBegin(GL_QUADS);
        glColor3f(1,0,0);
        rect(-16,-16,16,16)
        glColor3f(1,1,1);
        rect(-3,-10,3,10)
        rect(-10,-3,10,3)
    glEnd();
    glFlush();
}

int main(int argc, char **argv){
    glutInit(&argc, argv);
    glutInitWindowSize(320,320);
    glutCreateWindow("Swiss Flag");
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}

Link to the Compiled Executable

\$\endgroup\$
  • \$\begingroup\$ You can remove the space between the r macro and the definition. Also, r's definition can be changed to v(-w,-x);v(-w,z);v(y,z);v(y,-x); if you remove all -s in the calls to r. \$\endgroup\$ – Zacharý Jan 8 '18 at 16:07
1
\$\begingroup\$

Processing, 74 bytes

size(32,32);background(-65536);noStroke();rect(13,6,6,20);rect(6,13,20,6);

I have honestly no idea if that's legal here but here it is.

Outputs:

Window output in windows 10 64bit

Explanation:

size(32,32);            //Set the window size to 32x32
background(-65536);     //Draw the background as red. This is a hack that works because the color datatype uses an int but does not use it as a number. The int equivalent to #FF0000 is -65536 apparently. and it saves me a byte.
noStroke();             //Disable the stroke when drawing shapes like rect
rect(13,6,6,20);        //Draws a rectangle of size 6x20, starting the top-left corner at position (13, 6)
rect(6,13,20,6);        //Draws a rectangle of size 20x6, starting the top-left corner at position (6, 13)
\$\endgroup\$
  • \$\begingroup\$ Note that I could have gotten -65536 by using a bitshift operator, -1<<16, though that gives us the same amount of bytes. I'm just leaving this here to lead someone to a better solution. \$\endgroup\$ – Raphaël Côté Aug 4 '17 at 4:37
1
\$\begingroup\$

R , 75 bytes

x=matrix(2,32,32);x[7:26,14:19]=NA;x[14:19,7:26]=NA;image(x,c=2,ax=F,as=1)

Results in:

enter image description here With aspect ratio 1:1 and no axes are drawn.

\$\endgroup\$
  • 1
    \$\begingroup\$ Welcome on the site! \$\endgroup\$ – Dada Aug 4 '17 at 8:24
  • \$\begingroup\$ @Dada Thank you. Seems like fun here :-) \$\endgroup\$ – R.Jagdhuber Aug 4 '17 at 9:01
1
\$\begingroup\$

SmileBASIC, 42 bytes

GCLS #RED
GFILL 6,13,25,18GFILL 13,6,18,25

Alternatively -1<<16 or -8<<16 can be used instead of #RED for the same length.

\$\endgroup\$
  • 1
    \$\begingroup\$ The output isn't correct. The cross is too small and not centred in the red field, which itself is rectangular (5:3) and not square like it should be. \$\endgroup\$ – Anonymous Oct 15 '17 at 18:42
1
\$\begingroup\$

GW-Basic, 68 bytes (tokenised)

0 SCREEN 9:PALETTE 9,36:FOR I=0 TO 3:LINE(X,Y)-(31-X,31-Y),Y+9,BF
  ↪ :Y=I*7 MOD 14+6:SWAP X,Y:NEXT:LOCATE 4

To get it down to 68 bytes you have to manually edit the file GW-Basic saves to remove all spaces and the trailing end of file and null characters. The PALETTE statement is necessary because GW-Basic has no VGA support and the reds in the default EGA palette are too dark (4) and too washed out (12). The final LOCATE statement ensures that GW-Basic's Ok isn't printed on top of the flag.

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

PowerShell, 108 bytes

$1=($r='2 0 0 ')*192
$2=($r*13+($w='2 '*3)*6+$r*13)*7
$3=($r*6+$w*20+$r*6)*6
"P3 32 32 2 $1$2$3$2$1"|sc .ppm

Outputs a PPM file called .ppm (checked with IrfanView). It sets '2' as the maximum value for each channel, so $r is 2 0 0 for red, $w is 2 2 2 for white, and the rest is string multiplication and a file header; $1 is a horizontal red line for the very top and bottom of the flag, $2 is wide red, narrow white, wide red for the top and bottom parts of the cross, 3 is the centre narrow red, wide white, narrow red. sc is set-content and used because > makes a 2-byte+BOM Unicode file which doesn't work.

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

PostScript, 85 80 bytes

Code:

1 0 0 setrgbcolor
0 0 32 32 rectfill
1 setgray
[13 6 6 20 6 13 20 6] rectfill

Result:

enter image description here

\$\endgroup\$
  • \$\begingroup\$ Welcome to the site! :) \$\endgroup\$ – DJMcMayhem Aug 3 '17 at 15:21
0
\$\begingroup\$

BBC BASIC, 70 bytes

V.19;16,255|25,97,64;64;25,4,12;26;25,99,40;12;25,4,26;12;25,99,12;40;

Download interpreter at http://www.bbcbasic.co.uk/bbcwin/download.html

Program runs in default mode for this interpreter (white background, black foreground.) 2x2 units = 1 pixel. Flag is 64x64 units = 32 x 32 pixels.

The VDU or V. command sends characters to the screen controller. These include a number of machine specific control characters for graphics. Number followed by , sends 1 byte, by ; sends 2 bytes. Number followed by | sends "sufficient" bytes to finish a multibyte command.

Ungolfed

  VDU 19,0,16,255|   :REM Reprogram colour 0 (foreground) to #0000FF red (BBC BASIC is little endian.)
  VDU 25,97,64;64;   :REM Cursor is already at 0,0. Move to 64,64 and draw a rectangle in foreground colour (red.)
  VDU 25,4,12;26;    :REM Move to 12,26
  VDU 25,99,40;12;   :REM and draw a rectangle in backgorund colour (white) 40 units wide x 12 units high,
  VDU 25,4,26;12;    :REM Then move to 26,12
  VDU 25,99,12;40;   :REM and draw a vertical rectangle of simiar dimensions.
\$\endgroup\$
0
\$\begingroup\$

Batch File, 584 497 bytes

EDIT: -87 bytes! Replaced rem with hh as it is a no-op when no arguments are passed, removed a bunch of quotation marks and some other stuff.

I'm sure this can be compressed some more, but for the moment it's the best I got.

@!! 2>nul||cmd/q/v/c%0&&exit/b
for /f "tokens=1,2delims=#" %%a in ('"prompt #$H#&for %%b in (1)do hh"')do set K=%%a
set s=aaaaaa
set g=%s%%s%b
:d
set/ai+=1
set t=%i%
if %t%==5 set t=1
if %t%==4 set t=2
if %t%==1 for /l %%a in (1,1,6)do call:c 44 %g%%g%%s%&echo(
if %t%==2 for /l %%a in (1,1,7)do call:c 44 %g%&call:c ff %s%&call:c 44 %g%&echo(
if %t%==3 for /l %%a in (1,1,6)do call:c 44 %s%&call:c ff %g%%s%c&call:c 44 %s%&echo(
if %t%==6 exit
goto d
:c
<nul set/p.=%K%>%~2&findstr/a:%1 . %~2 nul

This code produces the following output (tested on Windows version 10.0.15063):

Beautiful flag screenshot

As a negative side-effect, it creates 4 different files on the same directory with some weird names and gibberish in them. You're free to delete them.

Also, in order for this to look right, CMD's properties must be changed (if not already set correctly):

CMD configuration

This code requires that the font chosen has a 1:1 width to height ratio.

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

Bash + core utils, 147 bytes

echo H4sICBkuh1kAA2ZsYWcueHBtAFNUiAjwNeIyNlIAIiMFQ64ihWQFZTc3AyDgKoewQYCLq4gAGIIKysFg+CooxwKGngI6BNQgUoAFDA0FXABVWXDvSwQAAA|base64 -d|gunzip>1.xpm

Decodes base64-encoded GZipped image data, unGZips it and writes into 1.xpm.

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

Python 2, 170 bytes

Uses Pillow 2.8.0 with PIL 1.1.7 for the old (and deprecated) offset method.

from PIL.ImageDraw import*
i=Image.new('RGB',(20,20),-1)
Draw(i).rectangle((3,3,18,18),'red',-1)
k=Image.new('RGB',(32,32),'red')
k.paste(i.offset(9),(6,6))
k.show()
\$\endgroup\$
-3
\$\begingroup\$

PNG - 101 bytes :p

âPNG


IHDR  I¥Ë∑PLTEˇˇˇˇA4IDAT◊c`¿ÿ"òˇˇ?Äõ ¬º)’   «√IENDÆB`Ç

Swiss Flag .png

Explanation :

Ctrl+C -> Ctrl+V

\$\endgroup\$
  • \$\begingroup\$ "PNG" or "keyboard" are not programming languages, as they cannot add two inputs or check the primality of an input. \$\endgroup\$ – Stephen Aug 2 '17 at 11:08
  • 2
    \$\begingroup\$ According to meta consensus on Do submissions have to be answered with a programming language? there is no longer a requirement that answers be in a programming language. \$\endgroup\$ – trichoplax Aug 2 '17 at 22:39
  • \$\begingroup\$ Clearly PNG isn't a programming language, but –at the risk of stating the obvious– this isn't "drawing" anything, it's just the pre-drawn output file handed out on a platter. That makes a profoundly un-interesting submission. Whereas most submissions could be tweaked for different parameters, this would just have to be redrawn and and the output re-pasted into the answer. \$\endgroup\$ – Caleb Aug 3 '17 at 9:03
  • \$\begingroup\$ Why is PNG unacceptable while GIF is not? \$\endgroup\$ – JollyJoker Aug 4 '17 at 8:42
  • \$\begingroup\$ Both are, see the comment above. \$\endgroup\$ – flawr Aug 4 '17 at 10:19

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.