48
\$\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\$
18
  • 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
    Commented Jul 31, 2017 at 22:25
  • 1
    \$\begingroup\$ Right, only the widely known image formats are allowed as well as output on the screen. \$\endgroup\$
    – flawr
    Commented Jul 31, 2017 at 22:26
  • 23
    \$\begingroup\$ echo 🇨🇭 - I assume this is not allowed \$\endgroup\$ Commented Jul 31, 2017 at 23:03
  • \$\begingroup\$ I see a blue CH, I don't see why this should be allowed. \$\endgroup\$
    – flawr
    Commented Jul 31, 2017 at 23:18
  • 1
    \$\begingroup\$ @Tutleman I think it does \$\endgroup\$
    – Luis Mendo
    Commented Aug 1, 2017 at 0:33

55 Answers 55

1
2
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\$
1
  • \$\begingroup\$ Welcome to PPCG! \$\endgroup\$
    – Stephen
    Commented Aug 3, 2017 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\$
5
  • 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
    Commented Aug 2, 2017 at 10:29
  • \$\begingroup\$ @manatwork note taken \$\endgroup\$
    – Khaled.K
    Commented Aug 2, 2017 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\$ Commented Aug 3, 2017 at 5:40
  • \$\begingroup\$ @SteveBennett note taken \$\endgroup\$
    – Khaled.K
    Commented Aug 3, 2017 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\$ Commented Aug 3, 2017 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\$
3
  • 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\$ Commented Aug 2, 2017 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\$ Commented Aug 2, 2017 at 20:34
  • \$\begingroup\$ Now there is also the LET(...) excel command - with it you can remove the replace command, and simplify all of the row commands and get you code down to 289 bytes \$\endgroup\$ Commented Mar 22, 2021 at 20:57
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\$
1
  • \$\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\$
    – Adalynn
    Commented Jan 8, 2018 at 16:07
2
\$\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\$
2
\$\begingroup\$

Desmos, 90 69 bytes

c=hsv(0,[1,0,0],1)
[0,6,13]<=x<=[32,26,19]\{[0,13,6]<=y<=[32,19,26]\}

Try it on Desmos!

-21 bytes thanks to Aiden Chow

\$\endgroup\$
1
\$\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\$
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\$
1
  • \$\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\$ Commented Aug 4, 2017 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\$
2
  • 1
    \$\begingroup\$ Welcome on the site! \$\endgroup\$
    – Dada
    Commented Aug 4, 2017 at 8:24
  • \$\begingroup\$ @Dada Thank you. Seems like fun here :-) \$\endgroup\$ Commented Aug 4, 2017 at 9:01
1
\$\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\$
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
  • 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
    Commented Oct 15, 2017 at 18:42
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\$
1
  • \$\begingroup\$ Welcome to the site! :) \$\endgroup\$
    – DJMcMayhem
    Commented Aug 3, 2017 at 15:21
1
\$\begingroup\$

APL (dzaima/APL), 83 bytes

P5.size←2⌿32
g←P5.G
a←13 6 19 26
P5.draw←{r←g.rect
g.bg'f00'
g.stroke←⍬
r a
r 2⌽⌽a}

32x32 canvas:

enter image description here

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

Applesoft BASIC, 144 bytes

0GR
1COLOR=1
2X=0
3VLIN0,31ATX
4X=X+1
5IFX<32GOTO3
6COLOR=15
7X=12
8VLIN6,25ATX
9X=X+1
10IFX<19GOTO8
11Y=12
12HLIN6,25ATY
13Y=Y+1
14IFY<19GOTO12
\$\endgroup\$
1
\$\begingroup\$

Pure bash with utf-8, 262

printf -vv %32s;w=${v:19} d=${w:6} e=${d:1} r=$'\e[31;47m' m=$'\e[0m\n' c=${e:3}
c=${c// /${r} $m} b=${c:14} g=${c// /${v// /█}} h=${c// /${w// /█}$e${w// /█}}
i=$r${e// /█}${d// /▀}$e${d// /▀}${e// /█}$m
echo "$g$h$i${b// /${e// /█}$w$d${e// /█}}${i//▀/▄}$h$g"

enter image description here

Pure bash, without utf-8, 283

printf -vf %14s;f=("$f" "${f:2}") w=$'\e[47m' r=${w/7/1}
for v in 6:rrr 7:rwr 6:www 7:rwr 6:rrr;do l=$r$f c=r s=${v%:*} m=${v#*:}
for((h=0;h<3;h++)){ [ $c != ${m:h:1} ]&&c=${m:h:1}&&l+=${!c};l+=${f[h&1]};}
[ $c = r ]||l+=$r;l+=$f;printf -vo %${s}s;printf "${o// /$l$'\e[0m\n'}";done

Will render:

Bash swiss cross

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

C (GCC), 133 130 125 bytes

#define a(x)labs(i x 32-15.5)<
i;main(c){for(puts("P6 32 32 1");i<1024;++i)printf("\1%c%c",c=a(%)10&a(/)10&(a(%)3|a(/)3),c);}

Attempt This Online!

Outputs a PPM image.

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

R, 71 bytes

par(mar=1:4*0+.059,bg="red",cex=107)
frame();text(.5,.5,"+",co="white")

Try it on rdrr.io

Just found this challenge and since the Swiss National Day was just four days ago, I am adding my answer in R to the existing four answers in R (75 bytes, 109 bytes, 112& 125 bytes and 140 bytes).

This program outputs a white + sign on the empty red plot. It might be possible to reduce this code further by 4 bytes by using an older 3.x version of R, where the background color bg=2 was exactly red as required (apparently, this isn't the case anymore).

Swiss Flag in R

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

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

PNG - 101 bytes :p

âPNG


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

Swiss Flag .png

Explanation :

Ctrl+C -> Ctrl+V

\$\endgroup\$
6
  • \$\begingroup\$ "PNG" or "keyboard" are not programming languages, as they cannot add two inputs or check the primality of an input. \$\endgroup\$
    – Stephen
    Commented Aug 2, 2017 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\$ Commented Aug 2, 2017 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
    Commented Aug 3, 2017 at 9:03
  • \$\begingroup\$ Why is PNG unacceptable while GIF is not? \$\endgroup\$
    – JollyJoker
    Commented Aug 4, 2017 at 8:42
  • 1
    \$\begingroup\$ Actually, I think it's kind of interesting that a generic commonly used image format like PNG can come close in size to the solutions using code tailor-made to this particular image. Also, people regularly submit Bubblegum solutions, which is designed to be used without input (and as a commentary on Stephen's criteria). \$\endgroup\$
    – Anonymous
    Commented Oct 15, 2017 at 18:33
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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