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

41
\$\begingroup\$

SVG (HTML5), 94 83 82 64 bytes

<svg><path d=M0,0V32H32V0M6,13h7V6h6v7h7v6H19v6H13V19H6 fill=red

Saved 2 bytes thanks to @ThePirateBay. Saved 1 byte thanks to @ATaco. Saved 18 bytes thanks to @CraigAyre pointing out that a 32×32 image was acceptable. As pointed out by @Shaggy, this assumes your default background is white; add 22 bytes for a flag that works on any background:

<svg><path d=M0,0V32H32V0 fill=red /><path d=M6,13h7V6h6v7h7v6H19v6H13V19H6 fill=#FFF

\$\endgroup\$
10
  • \$\begingroup\$ No need to close the path. You can save 4 bytes by leaving the path open. \$\endgroup\$
    – user72349
    Commented Aug 1, 2017 at 0:22
  • \$\begingroup\$ As ThePirateBay said, the right > in your path tag is optional. \$\endgroup\$
    – ATaco
    Commented Aug 1, 2017 at 0:26
  • 7
    \$\begingroup\$ SVG has its own "golfing language"? O.o \$\endgroup\$ Commented Aug 1, 2017 at 1:09
  • 17
    \$\begingroup\$ Being used to writing XML syntax, this code makes my eyes bleed \$\endgroup\$
    – Nayuki
    Commented Aug 1, 2017 at 3:13
  • 3
    \$\begingroup\$ Can you remove viewBox for the minimal version? <svg><path d=M0,0V32H32V0M6,13h7V6h6v7h7v6H19v6H13V19H6 fill=red \$\endgroup\$
    – Craig Ayre
    Commented Aug 1, 2017 at 9:08
34
\$\begingroup\$

x86-16 Machine Code for DOS, 43 bytes

               ; Draw 32x32 red square
B8 0C 28          mov  ax, 0x0C28   ; AL == color red, AH == set pixel function
B9 00 1F          mov  cx, 31
               DrawBox:
BA 00 1F          mov  dx, 31
               DrawRow:
CD 10             int  0x10
4A                dec  dx
75 FB             jnz  DrawRow
E2 F6             loop DrawBox

               ; Draw the interior white cross
B0 0F             mov  al, 0x0F    ; high byte already set
B1 06             mov  cl, 6       ; high byte already 0
               DrawCross:
B2 0D             mov  dl, 13      ; high byte already 0
               DrawCrossInner:
CD 10             int  0x10        ; plot CX, DX
87 D1             xchg dx, cx
CD 10             int  0x10        ; plot DX, CX
87 D1             xchg dx, cx
42                inc  dx
80 FA 12          cmp  dl, 13+6
75 F2             jne  DrawCrossInner
41                inc  cx
80 F9 19          cmp  cl, 6+(32-6-6)
75 EA             jne  DrawCross

C3                ret

The above code is designed to be assembled as a COM file and then run under DOS. It invokes the ROM BIOS video services to plot the individual pixels of the flag, forming a 32×32 representation of the Swiss flag in the upper-left corner of the screen.

The code assumes that the video mode is already set to mode 0x13, which also means that it requires a VGA display. Other video modes could be used, but requiring VGA gives you two things: (1) square pixels, and (2) a default palette that includes a true 100% red (0xFF0000) (meaning you don't have to waste bytes changing the palette colors). Before running this program, you will therefore need to switch your display to mode 0x13; the following code is all you need to do that:

mov  ax, 0x13
int  0x10

The code makes no other assumptions, and should run on any x86-compatible hardware under a DOS-compatible operating system.

However, the program terminates immediately after drawing the flag, so the DOS prompt will normally be re-printed at the top-left corner of the screen, covering up the top few lines of the flag. Therefore, if you want to marvel at the true output, you need to hang/pause the system before it RETurns. Here's a screenshot of what that looks like, running in a FreeDOS virtual machine:

It might be possible to golf this down further by writing pixel data directly to the video buffer, especially since I'm already assuming mode 0x13. I haven't tried that yet. INT 0x10 is already a pretty short instruction, but if I can use the one-byte string instructions to write pixel data directly to memory, then that could result in a significant code savings.

\$\endgroup\$
3
  • \$\begingroup\$ Impressive! I don't see asm a lot but when I do it's always very interesting \$\endgroup\$
    – adrian
    Commented Aug 1, 2017 at 21:36
  • \$\begingroup\$ Nice to see fans of ASM. \$\endgroup\$ Commented Aug 4, 2017 at 6:07
  • \$\begingroup\$ You could do it with three 32 pixel wide tiles iterating over all the rows. It uses more data, but saves in the number of operations. Alternatively, you could use three 16 pixel wide tiles for the left side and perform a symetrical operation by reversing the left side. \$\endgroup\$
    – ATL_DEV
    Commented Aug 4, 2017 at 12:42
21
\$\begingroup\$

MATL, 29 bytes

2Y66t&Y"OJQht3$)6thYaQK7hBoYG

Try it at MATL Online!

Explanation

2Y6     % Push predefined literal: 3×3 matrix [0 1 0; 1 1 1; 0 1 0]
6t&Y"   % Repeat each entry 6 times in the two dimensions. Gives an 18×18 matrix
OJQh    % Push [0 1j+1]. Used as an index, this means [0 1 ... end+1]. Indexing
        % is 1-based and modular, so 0 is the same as end
t3$)    % Apply that index in the two dimensions. This extends by 1 entry in 
        % each dimension, so that the arms have length 7. Gives a 20×20 matrix
6thYa   % Pad with 6 zeros in the two dimensions. Gives a 32×32 matrix
Q       % Add 1. The matrix now contains values 1 (will become red) and 2 (white)
K7hBo   % Convert array [4 7] to binary. Gives [1 0 0; 1 1 1], to be used as
        % colormap. First row is red, second is white
YG      % Show image using colormap
\$\endgroup\$
18
\$\begingroup\$

Excel VBA, 86 85 79 Bytes

Code

Anonymous VBE immediate window function that takes no input and outputs the Swiss flag onto the range [A1:F32] of the ActiveSheet object. This works by first making the cells square, then drawing the red region and finally drawing in the white cross.

Cells.RowHeight=48:[A1:AF32].Interior.Color=255:[G14:Z19,N7:S26].Interior.Color=-1

Note that arrangements which that remove the step of coloring the "cross" of the Swiss flag after putting in the red background are actually longer than the above configuration, as they require at least 16 cell addresses, which coordinate to the 8 distinct regions of the flag

-1 Byte thanks to @Greedo's for RowHeight=48 over ColumnWidth=2

-6 Bytes thanks to @Greedo's for -1 instead rgbWhite

Output

enter image description here

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Fantastic; but you can still save 6 bytes with Interior.Color=-1 instead of Interior.Color=rgbWhite \$\endgroup\$
    – Greedo
    Commented Aug 3, 2017 at 9:48
  • \$\begingroup\$ @Greedo, huh, what do you know - that's fantastic. I had used rgbWhite over its decimal value of 16777215 for clarity, I had no idea that -1 would have the same effect. Thanks! \$\endgroup\$ Commented Aug 3, 2017 at 11:43
  • 1
    \$\begingroup\$ according to this link: (reddit.com/r/compsci/comments/62x9g9/…) PowerPoint is Turing complete so I wonder how could this be ported to PowerPoint \$\endgroup\$ Commented Oct 29, 2017 at 15:22
17
\$\begingroup\$

Java 8, 246 + 42 = 288 bytes

Thanks to @Aaron for -64 bytes.
-4 bytes by outputting to file a without extension.

import java.awt.*;import java.awt.image.*;
x->{BufferedImage i=new BufferedImage(32,32,1);Graphics g=i.getGraphics();g.setColor(Color.RED);g.fillRect(0,0,32,32);g.setColor(Color.WHITE);g.fillRect(13,6,6,20);g.fillRect(6,13,20,6);javax.imageio.ImageIO.write(i,"png",new java.io.File("a"));}

A lamba expression that can be assigned to a functional interface method that throws an exception. Creates an image file named a (an image file) in the directory running this file.

Surrounding code used to run it: Try it online!

Ungolfed

x->{
    BufferedImage i=new BufferedImage(32,32,1);
    Graphics g=i.getGraphics();
    g.setColor(Color.RED);
    g.fillRect(0,0,32,32);
    g.setColor(Color.WHITE);
    g.fillRect(13,6,6,20);
    g.fillRect(6,13,20,6);
    javax.imageio.ImageIO.write(i,"png",new java.io.File("a"));
}

Result

Grid provided by the IntelliJ IDEA image preview (apparently).

\$\endgroup\$
11
  • \$\begingroup\$ You can shave quite a few bytes by changing your fillPolygon call into two fillRect calls. I'm not sure I respected the dimensions but I came up with g.fillRect(7,13,18,6);g.fillRect(13,7,6,18) which is shorter by 64 bytes \$\endgroup\$
    – Aaron
    Commented Aug 1, 2017 at 14:28
  • \$\begingroup\$ @Aaron Good call, for some reason I thought that'd be longer... Thanks! \$\endgroup\$ Commented Aug 1, 2017 at 21:15
  • \$\begingroup\$ You could name the file .png instead of a.png and save a byte. \$\endgroup\$ Commented Aug 2, 2017 at 9:55
  • \$\begingroup\$ @user2428118 Nice, would it be allowed to take it further and just name it a, without an extension? It can still be opened, just needs to be told to open in a photo viewer. \$\endgroup\$ Commented Aug 2, 2017 at 10:17
  • 1
    \$\begingroup\$ @OlivierGrégoire I feel that's too far from the idea of my solution, but you're welcome to post that as your own \$\endgroup\$ Commented Aug 2, 2017 at 14:20
13
\$\begingroup\$

C++, SFML, 406 399 394 bytes

-2 bytes thanks to pdpi
-10 bytes thanks to Zacharý

SFML ( Simple and Fast Multimedia Library ) is a C++ library written to ease the developpement of video games and multimedia programs

The code :

#include<SFML\Graphics.hpp>
using namespace sf;int main(){for(RenderWindow t(VideoMode(128,128),"");t.isOpen();){t.clear();RectangleShape r({128.f,128.f});r.setFillColor(Color::Red);t.draw(r);r.setFillColor(Color::White);r.setPosition({24.f,52.f});r.setSize({80.f,24.f});t.draw(r);r.rotate(90);r.setPosition({76.f,24.f});t.draw(r);t.display();for(Event e;t.pollEvent(e);)if(!e.type)t.close();}}

The flag get displayed in a 128x128 window. Your OS have to be able to display a 128x128 window ( my Win8.1 can't display with smaller width )

WARNING : you may want to add these 2 lines of code : t.setFramerateLimit(60);t.setVerticalSyncEnabled(true);, so your CPU won't heat when you run the code. I did not put them in the original code for golfing reasons.

\$\endgroup\$
3
  • \$\begingroup\$ You can shrink this a bit by replacing your whiles with fors, and initialising t and e in there: int main(){for(sf::RenderWindow t(sf::VideoMode(128,128),"");t.isOpen();){t.clear();sf::RectangleShape r({128.f,128.f});r.setFillColor(sf::Color::Red);t.draw(r);r.setFillColor(sf::Color::White);r.setPosition({24.f,52.f});r.setSize({80.f,24.f});t.draw(r);r.rotate(90);r.setPosition({76.f,24.f });t.draw(r);t.display();for(sf::Event e;t.pollEvent(e);)if(e.type == 0)t.close();}} \$\endgroup\$
    – pdpi
    Commented Aug 1, 2017 at 12:23
  • \$\begingroup\$ Also, you can save two bytes by removing spaces: e.type == 0 => e.type==0, maybe even !e.type (don't remember the precedence). 24.f } => 24.f} \$\endgroup\$
    – Adalynn
    Commented Aug 1, 2017 at 12:25
  • \$\begingroup\$ Oh, and is using namespace sf; possible? \$\endgroup\$
    – Adalynn
    Commented Aug 1, 2017 at 13:42
13
\$\begingroup\$

Braindraw, 227 bytes (Non-competing) (Updated to actually work)

Ungolfed version (241 bytes)

r<<^++++++++[-^++++v]^[->>[>]>++++++++[-<++++>]<[<]<]>>[[-vv[v]-[^]^]>]vv<[<]>>>>>>>>>>>>>>vvvvvvg<<^^++++++[->>[>]>+++++[-<++++>]<[<]<]>>[[-vv[v]b-g-[^]^]>]<vv[<]><<<<<<<vvvvvvv<<^^++++++[-vv[v]++++++++++++++[^]^]vv[[->>[>]b[+]-g[+]-[<]<]v]

Lightly golfed version (227 bytes)

r<<^++++++++[-^++++v]^[->>[>]>++++++++[-<++++>]<[<]<]>>[[-vv[v]-[^]^]>]vv<[<]g>>>>>>>>>>>>vvvv++++++[->>[>]>+++++[-<++++>]<[<]<]>>[[-vv[v]b-g-[^]^]>]<vv[<]<<<<<<vvvvv<<++++++[-vv[v]++++++++++++++[^]^]vv[[->>[>]b[+]-g[+]-[<]<]v]

Braindraw is a programming language of my own design. As of 3:08 pm today, I do have a working interpreter and have checked that the program runs properly. However, the interpreter did not exist before this competition began, so I am not competing.

Braindraw is the same as Brainf***, except that it operates on three two-dimensional arrays, rather than one one-dimensional array. These arrays are called r, g, and b, and act as the red green and blue color channels. The operator r moves the pointer to the red channel, etc etc. Up is ^, down is v.

\$\endgroup\$
17
  • 9
    \$\begingroup\$ Looking forward to seeing an interpreter for this, could be fun to try out \$\endgroup\$
    – Darren H
    Commented Aug 1, 2017 at 8:07
  • 1
    \$\begingroup\$ It certainly a fun idea, I'm looking forward to an interpreter too! (Technically we only allow languages that have been implemented, and we consider the interpreter as their definition.) \$\endgroup\$
    – flawr
    Commented Aug 1, 2017 at 8:26
  • \$\begingroup\$ Is it correct that you do not explicitly output anything, but just output the whole "canvas" in the end? Do you assume the canvas is infinite? \$\endgroup\$
    – flawr
    Commented Aug 1, 2017 at 8:29
  • \$\begingroup\$ In re: no interpreter, I guess that means my entry is non-competing. Oh well. But yes, this program has no output per se, the computing space is displayed while and after the program is run. The canvas is theoretically infinite, but in implementation, I will probably just make it some large-ish fixed width like maybe 512x512 \$\endgroup\$
    – benzene
    Commented Aug 1, 2017 at 14:36
  • \$\begingroup\$ This language seems capable of kolmogorov complexity image challenges. And are you currently working on an interpreter? \$\endgroup\$
    – Adalynn
    Commented Aug 1, 2017 at 14:51
11
\$\begingroup\$

Mathematica, 83 bytes

R=Rectangle;Graphics[{Red,{0,0}~R~{32,32},White,{6,13}~R~{26,19},{13,6}~R~{19,26}}]

next one is from @ASCII-only (I thought it was a joke but it works!)

Mathematica, 23 bytes

"CH"~CountryData~"Flag"

-10 bytes from @Jonathan Frech

\$\endgroup\$
8
  • 4
    \$\begingroup\$ Mathematica doesn't have a built-in for the Swiss flag? \$\endgroup\$
    – CJ Dennis
    Commented Aug 1, 2017 at 0:47
  • 4
    \$\begingroup\$ CountryData["Switzerland","Flag"] but it might not have the right proportions \$\endgroup\$
    – ASCII-only
    Commented Aug 1, 2017 at 0:49
  • 6
    \$\begingroup\$ Mathematica has a built-in for everything, but this? \$\endgroup\$
    – sergiol
    Commented Aug 1, 2017 at 7:39
  • 10
    \$\begingroup\$ "Switzerland"~CountryData~"Flag" saves you some more \$\endgroup\$
    – A Simmons
    Commented Aug 1, 2017 at 13:51
  • 1
    \$\begingroup\$ "CH"~CountryData~"Flag" only takes 23 bytes. \$\endgroup\$ Commented Aug 24, 2017 at 21:47
10
\$\begingroup\$

TI-BASIC (TI-84 Plus C(S)E only), 58 bytes

:For(A,0,31
:For(B,0,31
:Pxl-On(A,B,RED
:End
:End
:For(A,6,25
:For(B,13,18
:Pxl-Off(A,B
:Pxl-Off(B,A
:End
:End
\$\endgroup\$
2
  • \$\begingroup\$ This is an awesome answer. I just made an account to upvote it. TI-BASIC is great \$\endgroup\$
    – Zags
    Commented Aug 3, 2017 at 12:17
  • 1
    \$\begingroup\$ Oh what a deluxe third parameter Pxl-On has now... it makes me cry only having had monochrome :( \$\endgroup\$
    – Ray
    Commented Aug 5, 2017 at 15:12
9
\$\begingroup\$

HTML + CSS, 15 + 117 bytes = 132 bytes

Using a flexbox wrapper with an inset box-shadow.

b{display:flex;box-shadow:red 0 0 0 6px inset;padding:2px;width:28px;flex-wrap:wrap}a{border:solid red 4px;margin:3px
<b><a><a><a><a>


HTML + CSS, 18 + 139 137 122 bytes = 140 bytes

Previous answer using border and an intermediate flexbox wrapper with negative margin.

i,b,a{display:inline-flex}i{border:solid red 6px}b{margin:-4px;width:28px;flex-wrap:wrap}a{border:solid red 4px;margin:3px
<i><b><a><a><a><a>

\$\endgroup\$
8
\$\begingroup\$

JavaScript + HTML, 99 + 13 = 112 bytes

Saved 2 bytes thanks to user2428118
Saved 2 bytes thanks to Matheus Avellar

JSFiddle

with(c.getContext`2d`)fillStyle='red',fillRect(0,0,32,32),clearRect(6,13,20,6),clearRect(13,6,6,20)
<canvas id=c>


Pure JavaScript, 194 129 bytes

JSFiddle

with(document.write`<canvas id=c>`,c.getContext`2d`)fillStyle='red',fillRect(0,0,32,32),clearRect(6,13,20,6),clearRect(13,6,6,20)

\$\endgroup\$
13
  • 2
    \$\begingroup\$ I believe you have to do this from scratch. \$\endgroup\$
    – Stephen
    Commented Jul 31, 2017 at 22:45
  • \$\begingroup\$ @StepHen. Ok, I added it too. \$\endgroup\$
    – user72349
    Commented Jul 31, 2017 at 22:50
  • 1
    \$\begingroup\$ ... You need to include it in the bytecount \$\endgroup\$
    – ASCII-only
    Commented Aug 1, 2017 at 0:48
  • \$\begingroup\$ @ASCII-only. Included. \$\endgroup\$
    – user72349
    Commented Aug 1, 2017 at 0:54
  • 1
    \$\begingroup\$ For future reference, whenever you need a <canvas> you can include the HTML directly in your answer. See codegolf.stackexchange.com/a/136972/47097 \$\endgroup\$
    – darrylyeo
    Commented Aug 1, 2017 at 2:03
7
\$\begingroup\$

GraphicsMagick (?), 96 bytes

gm convert -size 32x32 xc:red -fill #fff -draw "rectangle 13,6 18,25 rectangle 6,13 25,18" 1.bmp

Not sure how to golf with GM (should this be considered as a language?). Also not sure how to count bytes here...

With GM installed, type the given code in windows cmd, you will get an image with name 1.bmp. You may want change double quote to single quote if you are using bash (it should work, but i had not tested this).

Thanks to Digital Trauma. use only one -draw save 8 bytes

\$\endgroup\$
2
  • \$\begingroup\$ Not sure if its the same in GraphicsMagick, but with ImageMagick you can put both rectangles in the same -draw string: convert -size 32x32 xc:red -fill white -draw "rectangle 13,6 18,25 rectangle 6,13 25,18" i.png \$\endgroup\$ Commented Aug 2, 2017 at 17:14
  • \$\begingroup\$ @DigitalTrauma it is same. edited. \$\endgroup\$
    – tsh
    Commented Aug 3, 2017 at 2:37
7
\$\begingroup\$

GFA BASIC (Atari ST),  62  55 bytes

A manually edited listing in .LST format. All lines end with CR, including the last one.

DEFF 2
PB 0,0,31,31
DEFF 0
PB 13,6,18,25
PB 6,13,25,18

Expanded and commented:

DEFFILL 2         ! set fill color: default system color #2 is red (&H700)
PBOX 0,0,31,31    ! draw the red square
DEFFILL 0         ! set fill color: default system color #0 is white (&H777)
PBOX 13,6,18,25   ! draw a vertical white rectangle
PBOX 6,13,25,18   ! draw a horizontal white rectangle

###Output

The output is 32x32.

GFA output

NB: This is indeed using the 'white' color, although it's gray-looking on the emulator (and on the real thing as well, unless you turn the brightness quite high).

\$\endgroup\$
6
\$\begingroup\$

Python 3, 190 bytes

Save output as image.xpm and open with your favorite text editor.

r=range;print('! XPM2\n32 32 2 1\nr c #FF0000\nw c #FFFFFF\n');d=[bytearray(b'r'*32)for _ in r(32)]
for i in r(20):
 for j in r(6):d[i+6][j+13]=d[j+13][i+6]=119
print(b'\n'.join(d).decode())

Try it online!

Output:

! XPM2
32 32 2 1
r c #FF0000
w c #FFFFFF

rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
rrrrrrrrrrrrrwwwwwwrrrrrrrrrrrrr
rrrrrrrrrrrrrwwwwwwrrrrrrrrrrrrr
rrrrrrrrrrrrrwwwwwwrrrrrrrrrrrrr
rrrrrrrrrrrrrwwwwwwrrrrrrrrrrrrr
rrrrrrrrrrrrrwwwwwwrrrrrrrrrrrrr
rrrrrrrrrrrrrwwwwwwrrrrrrrrrrrrr
rrrrrrrrrrrrrwwwwwwrrrrrrrrrrrrr
rrrrrrwwwwwwwwwwwwwwwwwwwwrrrrrr
rrrrrrwwwwwwwwwwwwwwwwwwwwrrrrrr
rrrrrrwwwwwwwwwwwwwwwwwwwwrrrrrr
rrrrrrwwwwwwwwwwwwwwwwwwwwrrrrrr
rrrrrrwwwwwwwwwwwwwwwwwwwwrrrrrr
rrrrrrwwwwwwwwwwwwwwwwwwwwrrrrrr
rrrrrrrrrrrrrwwwwwwrrrrrrrrrrrrr
rrrrrrrrrrrrrwwwwwwrrrrrrrrrrrrr
rrrrrrrrrrrrrwwwwwwrrrrrrrrrrrrr
rrrrrrrrrrrrrwwwwwwrrrrrrrrrrrrr
rrrrrrrrrrrrrwwwwwwrrrrrrrrrrrrr
rrrrrrrrrrrrrwwwwwwrrrrrrrrrrrrr
rrrrrrrrrrrrrwwwwwwrrrrrrrrrrrrr
rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
\$\endgroup\$
1
  • \$\begingroup\$ I had to remove the empty 5th line to open it with gimp on linux. \$\endgroup\$
    – Tim
    Commented Aug 5, 2017 at 7:16
6
\$\begingroup\$

JavaScript (ES6) in HTML4(?), 178 176 bytes

document.write('<table cellspacing=0>'+[...Array(32)].map((_,y,p)=>p.map((_,x)=>`<td bgcolor=${x>5&&x<26&&y>12&&y<19||y>5&&y<26&&x>12&&x<19?'#fff':'red'}>`).join``).join`<tr>`)

Yes! We do not need <canvas> nor <svg> or even <img>!

Thanks to Justin Mariner, save 2 bytes.

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

Perl 5 + -p0513, 80 bytes

print"P6 32 32 1",$_=($}="A..")x192,$\=($}x13 .1x18 .$}x13)x7,($}x6,1x60,$}x6)x6

Try it online!

Outputs a PBM file.


Perl 5 + -p0513 -M5.10.0, 82 bytes

say"P6 32 32 1",map{($|--||"A..")x$_}205,@a=((18,26)x6,18),20,(54,14)x6,0,6,@a,205

Try it online!

Also outputs a PBM file. This is a slightly more interesting approach, but I can't get it to beat the above.


Perl 5 + -Mutf8 -p0513 -M5.10.0, 149 bytes

say"",$\=($l=$"x32 ."
")x3,$_=(($s=$"x13).($}='█')x6 ."$s
")x3,$l=($"x=6).($a='▄'x7).$}x6 .$a.$",("
",$",$}x20,$")x2,"
",$l=~y;▄;▀;r

Try it online!

Output is to the terminal via ANSI escape codes and looks like this:

swiss swoo

Proportions should be to scale based on one character space being two vertical 'pixels'.

You can preview the output by copying from TIO and pasting in here (this shows the control characters when hovered over the element).

\$\endgroup\$
5
\$\begingroup\$

Löve2D, 139 138 Bytes

l=love g=l.graphics c=g.setColor r=g.rectangle f="fill"w=255 l.draw=loadstring"c(w,0,0)r(f,0,0,32,32)c(w,w,w)r(f,13,6,6,20)r(f,6,13,20,6)"

Saved one byte thanks to ThePirateBay

Ungolfed

function love.draw()
    love.graphics.setColor(255,0,0)
    love.graphics.rectangle("fill",0,0,32,32)
    love.graphics.setColor(255,255,255)
    love.graphics.rectangle("fill",13,6,6,20)
    love.graphics.rectangle("fill",6,13,20,6)
end
\$\endgroup\$
0
5
\$\begingroup\$

R, 171 140 bytes.

x = rep(c(6,13,19,26,19,13),e=2)
l=c(0,32)
par(bg="red",mar=0*1:4)
plot(0,0,'n',l,l,xaxs="i",yaxs="i")
polygon(x,c(x,x)[4:15],c="white",b=0)

(somewhat) ungolfed:

x <- rep(c(6,13,19,26,19,13),each=2)
y <- c(x,x)[4:15] # index by 3 positions
par(bg="red",mar=c(0,0,0,0)) # prepare the plot space
plot(0,type='n', xaxs="i",yaxs="i",xlim=c(0,32),ylim=c(0,32))
polygon(x,y,col="white",border=0)

Thanks to @gregor for saving some bytes

Run it!

\$\endgroup\$
6
  • 1
    \$\begingroup\$ Welcome to the site! :) \$\endgroup\$
    – DJMcMayhem
    Commented Aug 1, 2017 at 17:50
  • \$\begingroup\$ Nice! You can save quite a few bytes with partial matching of argument names: e=2 instead of each=2, t instead of type, c for col, b for border. And 0*1:4 is shorter than c(0,0,0,0). \$\endgroup\$ Commented Aug 1, 2017 at 18:00
  • \$\begingroup\$ Actually you can get away with positional matching on plot: plot(0,0,'n',l,l,xaxs="i",yaxs="i") (assigning m="i" actually costs you one byte). With all of that it gets down to 141 bytes. \$\endgroup\$ Commented Aug 1, 2017 at 18:06
  • \$\begingroup\$ I forgot entirely about partial matching. Thanks! \$\endgroup\$ Commented Aug 1, 2017 at 20:46
  • \$\begingroup\$ Couple more bytes off: l and the xlim/ylim aren't needed, just plot(0:32,0:32,...). I also adapted your approach and got it down to 117 here, using rect instead of polygon. \$\endgroup\$ Commented Aug 1, 2017 at 22:41
5
\$\begingroup\$

Octave, 105 bytes 73 bytes

Note: line feeds added for clarity, not included in byte count

x(32,32,3)=0;
x(:,:,1)=1;
x(7:26,14:19,:)=1;
x(14:19,7:26,:)=1;
imshow(x*255)

EDIT: thanks to flawr for saving quite a lot of bytes!

Here is the result on octave-online.net, thanks to sanchises

Results

\$\endgroup\$
4
  • \$\begingroup\$ I'm not sure about Octave, but in MATLAB following should work: x(32,32,3)=0; x(:,:,1)=1; x(7:26,14:19,:)=1; x(14:19,7:26,:)=1; imshow(x*255) \$\endgroup\$
    – flawr
    Commented Aug 1, 2017 at 15:53
  • \$\begingroup\$ BTW: Welcome to PPCG!!! \$\endgroup\$
    – flawr
    Commented Aug 1, 2017 at 15:54
  • \$\begingroup\$ Octave-online.net does show pictures. \$\endgroup\$
    – Sanchises
    Commented Aug 1, 2017 at 18:43
  • \$\begingroup\$ You can save 4 bytes, multiplying by 255 is redundant, since you're using double type arrays, not uint8, so x(32,32,3)=0; x(:,:,1)=1; x(7:26,14:19,:)=1; x(14:19,7:26,:)=1; imshow(x) does exactly the same thing. (for doubles and floats imshow treats everything >= 1 as white/full color, everything <= 0 as black/no color and (0:1) are inbetweens) \$\endgroup\$
    – elementiro
    Commented Sep 24, 2020 at 19:40
5
\$\begingroup\$

R, 109 bytes

par(mar=0*1:4,bg=2,xaxs="i",yaxs="i")
frame()
rect(a<-c(6,13)/32,rev(a),b<-c(26,19)/32,rev(b),c="white",b=NA)

Uses frame to avoid presetting a plot, but default plotting region is on interval [0,1] instead of [0,32]. Also uses the fact that rect is vectorized. The default size of the plotting window is 7in x 7in. It outputs the following:

Swiss flag in plotting window

R, 125 bytes

png()
par(mar=0*1:4,bg=2,xaxs="i",yaxs="i")
frame()
rect(a<-c(6,13)/32,rev(a),b<-c(26,19)/32,rev(b),c="white",b=NA)
dev.off()

Default for png is a square of 480x480 pixels. Output is the following png file:

Swiss flag

\$\endgroup\$
5
\$\begingroup\$

GIF, 97 Bytes

GIF87a    ð  ÿ  ÿÿÿ,         @„©Ëí£œ´Ú;ƒÞXmÞ!ŸŠcyŒj¨,àZê<Cô
:8nïtï‹é‚BJ1tì$1ËKSvb=_ÔªõŠe  ;

Cheap and byte-heavy, but worth it.

EDIT: Some characters aren't in the code above, here's a pastebin for the GIF's hexdump.

EDIT: compressed down to 97 bytes with online tool.

\$\endgroup\$
7
  • 1
    \$\begingroup\$ It seems that some unprintable characters were removed from your code snippet. Maybe try posting a hex dump. \$\endgroup\$
    – user72349
    Commented Aug 1, 2017 at 1:49
  • 19
    \$\begingroup\$ How do you pronounce the language? \$\endgroup\$
    – Oliver
    Commented Aug 1, 2017 at 16:16
  • 1
    \$\begingroup\$ @Oliver ZHAIF \$\endgroup\$
    – Neil
    Commented Aug 1, 2017 at 17:22
  • 1
    \$\begingroup\$ Gimp produces a GIF file that looks identical in only 95 bytes (less than many other solutions). Here's a hex dump of it. \$\endgroup\$
    – Armin Rigo
    Commented Aug 1, 2017 at 22:39
  • 7
    \$\begingroup\$ Since when is GIF a programming language? \$\endgroup\$
    – Adám
    Commented Aug 2, 2017 at 8:44
4
\$\begingroup\$

HTML + JavaScript (ES6), 13 + 112 = 125 bytes

C=c.getContext`2d`
C[s='fillStyle']='red'
C[r='fillRect'](0,0,32,32)
C[s]='#fff'
C[r](6,13,20,6)
C[r](13,6,6,20)
<canvas id=c>

HTML + JavaScript (ES6), 13 + 114 = 127 bytes

with(c.getContext`2d`)fillStyle='red',fillRect(0,0,32,32),fillStyle='#fff',fillRect(6,13,20,6),fillRect(13,6,6,20)
<canvas id=c>

\$\endgroup\$
5
  • \$\begingroup\$ I've stolen your rectangle approach and improved it to 117 bytes using clearRect, I hope you're ok with that? \$\endgroup\$
    – user72349
    Commented Aug 1, 2017 at 21:51
  • \$\begingroup\$ That's clever, but unfortunately the OP says transparent pixels aren't allowed. codegolf.stackexchange.com/questions/136927/draw-the-swiss-flag/… \$\endgroup\$
    – darrylyeo
    Commented Aug 2, 2017 at 4:30
  • \$\begingroup\$ Actually, it is white. By default, all webpages have white background and the flag is transparent itself, but it is just an internal definition of transparency. External definition of transparency would be if pixels in that region (white plus) may change its color in some conditions (or may be set randomly). However, because I'm using both HTML and JavaScript, the whole output is the red flag with white cross. You cannot consider the canvas and background separately. How canvas is displayed on the background is well defined and the result always contains white cross by spec definitions. \$\endgroup\$
    – user72349
    Commented Aug 2, 2017 at 9:41
  • \$\begingroup\$ I hope its a bit more clear now, I mean you may disagree, but if you really think about it I am sure you'll find my answer fully consistent with OP's requirements. \$\endgroup\$
    – user72349
    Commented Aug 2, 2017 at 9:42
  • \$\begingroup\$ @ThePirateBay Fair enough. \$\endgroup\$
    – darrylyeo
    Commented Aug 2, 2017 at 17:47
4
\$\begingroup\$

C# 265/266 209 bytes

 using System;using c=System.Console;class P{static void Main(){for(int i=0,j;i<32;i++){for(j=0;j<32;j++){if((6<i&&i<26&&j>13&&j<19)||(6<j&&j<26&&i>13&&i<19))c.BackgroundColor=ConsoleColor.White;else c.BackgroundColor=ConsoleColor.Red;c.Write(" ");}c.WriteLine();}}}     

I wrote "space" characters into console while changing Background Color. But if you think it is too tall and you count single character as 8 x 16 pixels, you simply double values near "j" like this (+ 1 byte and + c.ReadKey(); so it stops):

  using System;using c=System.Console;class P{static void Main(){for(int i=0,j;i<32;i++){for(j=0;j<64;j++){if((6<i&&i<26&&j>26&&j<38)||(12<j&&j<52&&i>13&&i<19))c.BackgroundColor=ConsoleColor.White;else c.BackgroundColor=ConsoleColor.Red;c.Write(" ");}c.WriteLine();} c.ReadKey(); }}     

56 bytes saved thanks to TheLethalCoder

 namespace System{using static Console;class P{static void Main(){for(int i=0,j;i<32;i++,WriteLine())for(j=0;j<32;Write(""))BackgroundColor=(ConsoleColor)((6<i&i<26&j>13&j<19)|(j++<26&i>13&i<19&6<j​)?15:12);}}}
\$\endgroup\$
1
  • \$\begingroup\$ 209 bytes namespace System{using static Console;class P{static void Main(){for(int i=0,j;i<32;i++,WriteLine())for(j=0;j<32;Write(" "))BackgroundColor=(ConsoleColor)((6<i&i<26&j>13&j<19)|(6<j&j++<26&i>13&i<19)?15:12);}}} \$\endgroup\$ Commented Aug 1, 2017 at 9:34
4
\$\begingroup\$

Tikz, 145 143 bytes

\documentclass[tikz]{standalone}\begin{document}\tikz{\def~{)rectangle(}\fill[red](,~33,33);\fill[white](7,14~27,20)(14,7~20,27)}\end{document}

Here it is "ungolfed"

\documentclass[tikz]{standalone}
\begin{document}
\tikz{
\def~{)rectangle(}
  \fill[red](,~33,33);
  \fill[white](7,14~27,20)(14,7~20,27)
}\end{document}

Here it is ungolfed a little more

\documentclass[tikz]{standalone}
\begin{document}
\tikz{
  \fill[red](1,1)rectangle(33,33);
  \fill[white](7,14)rectangle(27,20)(14,7)rectangle(20,27)
}\end{document}

This makes 3 rectangles a big red one and two smaller white ones for the plus.

Here's what it looks like. It doesn't really look any different than the others.

flag

\$\endgroup\$
1
  • \$\begingroup\$ I put a golfing documentclass on github a few weeks ago. and there doesn't seem to be any window background requirement. With that \documentclass{g}\input tikz should work to save 5B. You can probably save more by working in plain TeX, even with TikZ \$\endgroup\$
    – Chris H
    Commented Aug 1, 2017 at 15:35
4
\$\begingroup\$

R, 112 bytes

par(bg=2,mar=0*1:4)
plot(0:32,0:32,'n',xaxs="i",yaxs="i")
rect(c(6,13),c(13,6),c(26,19),c(19,26),c="white",b=NA)

Though, to guarantee the aspect ratio, we need 5 more bytes for a total of 117:

par(bg=2,mar=0*1:4)
plot(0:32,0:32,'n',xaxs="i",yaxs="i",as=1)
rect(c(6,13),c(13,6),c(26,19),c(19,26),c="white",b=NA)

With lots of inspiration from Alex Axthelm's answer, and thanks to Rift for saving a few more bytes.

\$\endgroup\$
1
  • 3
    \$\begingroup\$ you could save 4 bytes changing bg="red" to bg=2 which is the same in the default palette \$\endgroup\$
    – Rift
    Commented Aug 2, 2017 at 8:22
4
\$\begingroup\$

Postscript, 301 286 187 bytes

/m{moveto}def
/s{setrgbcolor}def
/r{rlineto}def
/c{closepath fill}def
1 0 0 s
0 0 m
0 320 r
320 0 r
0 -320 r
c
1 1 1 s
130 60 m
60 0 r
0 200 r
-60 0 r
c
60 130 m
0 60 r
200 0 r
0 -60 r c

Thanks to the anonymous editor who suggested a 99 byte reduction!

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

Red, 79 Bytes

Code:

view[base 32x32 red draw[fill-pen white pen off box 6x13 26x19 box 13x6 19x26]]

Result:

window

Explanation:

view [              ; activate view engine
base 32x32 red      ; draw a 32 by 32 red box as base
 draw [             ; draw on top of the base
  fill-pen white    ; fill color is white
  pen off           ; turn off outline, alternatively could use white
  box 6x13 26x19    ; draw a horizontal rectangle, specified as 2 corners XxY
  box 13x6 19x26    ; draw vertical rectangle
]]
\$\endgroup\$
3
\$\begingroup\$

Bitmap, 160 bytes

Here's a bitmap with 1-bit colour depth, using a 24-bit colour definition. The last six bytes of the second line is the palette and following that is the pixel array. Every four bytes contains a whole row of pixels.

42 4D A0 00 00 00 00 00 00 00 20 00 00 00 0C 00
00 00 20 00 20 00 01 00 01 00 00 00 FF FF FF FF
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 07 E0 00 00 07 E0 00
00 07 E0 00 00 07 E0 00 00 07 E0 00 00 07 E0 00
00 07 E0 00 03 FF FF C0 03 FF FF C0 03 FF FF C0
03 FF FF C0 03 FF FF C0 03 FF FF C0 00 07 E0 00
00 07 E0 00 00 07 E0 00 00 07 E0 00 00 07 E0 00
00 07 E0 00 00 07 E0 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

I tried to truncate the trailing zeroes, but image viewers require there are enough pixels defined for the entire canvas.

Swiss.bmp

\$\endgroup\$
8
  • 3
    \$\begingroup\$ "Any suggestions where I can save arbitrary files permanently?" - on GitHub. \$\endgroup\$
    – user72349
    Commented Aug 1, 2017 at 6:31
  • 1
    \$\begingroup\$ Can you use RLE encoding? \$\endgroup\$
    – Neil
    Commented Aug 1, 2017 at 17:24
  • 1
    \$\begingroup\$ Haha, you've 1up'd my GIF example. \$\endgroup\$
    – adrian
    Commented Aug 1, 2017 at 22:05
  • 1
    \$\begingroup\$ I think this is not a valid programming language. IFAICT, BMP can neither check primeness, nor add numbers. \$\endgroup\$
    – Adám
    Commented Aug 2, 2017 at 8:46
  • 3
    \$\begingroup\$ Please everyone note that we allow non-programming-language submissions as per this consensus, so this submission is perfectly fine. (And IMO a nice benchmark to have.) \$\endgroup\$
    – flawr
    Commented Aug 3, 2017 at 9:56
3
\$\begingroup\$

Python 2, 92 91 89 bytes

r,w=' \0\0',' '*3
A,B=192*r,(13*r+6*w+13*r)*7
print"P6 "+"32 "*3+A+B+(6*r+20*w+6*r)*6+B+A

Output as binary ppm, usage:

python golf_swiss.py > swiss.ppm

Increasing the color depth allowed me to use space for maximum value

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

Tcl/Tk, 94

If it runs on the interactive shell, one can abbreviate canvas to can and grid to gri

gri [can .c -bg red -w 32 -he 32]
lmap R {{8 15 28 21} {15 8 21 28}} {.c cr r $R -f #FFF -w 0}

Tcl/Tk, 98

If it runs on the interactive shell, one can abbreviate canvas to can

grid [can .c -bg red -w 32 -he 32]
.c cr r 8 15 28 21 -f #FFF -w 0
.c cr r 15 8 21 28 -f #FFF -w 0

Tcl/Tk, 101

pack [canvas .c -bg red -w 32 -he 32]
.c cr r 8 15 28 21 -f #FFF -w 0
.c cr r 15 8 21 28 -f #FFF -w 0

enter image description here

\$\endgroup\$
0

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.