91
\$\begingroup\$

As you probably know, there is a war going on in Ukraine. I noticed that it seems nobody has posted a Ukrainian flag challenge yet, so I thought I'd do it myself in support of Ukraine.

The Challenge

Here is the flag: Flag of Ukraine

  • The flag is divided horizontally across the middle into two equally sized stripes.
  • Stripe colors: (0, 87, 183) or #0057B7 (blue, top) and (255, 215, 0) or #FFD700 (yellow, bottom).
  • Colors must be exact if possible, otherwise use the closest available blue and yellow.
  • The image can be saved to a file or piped raw to STDOUT in any common image file format, or it can be displayed in a window.
  • The image must be sized at a 3:2 ratio, and at least 78 by 52 pixels.
  • Alternatively, output a block of text at least 78 characters wide made of non-whitespace characters that depicts the flag, using ANSI color codes to color it. (Use standard blue and yellow.)
  • Built-in flag images, flag-drawing libraries, or horrendously upscaling the Ukrainian flag emoji are prohibited.

This is , so shortest code wins!

\$\endgroup\$
16
  • 1
    \$\begingroup\$ If the ANSI color code option is chosen, what aspect ratio should we assume each character has? \$\endgroup\$
    – Nitrodon
    Commented Mar 19, 2022 at 16:38
  • 2
    \$\begingroup\$ @des54321 It must be exact. \$\endgroup\$
    – Ginger
    Commented Mar 19, 2022 at 18:53
  • 8
    \$\begingroup\$ Unless I'm misunderstanding the QBasic answer already posted, I think that answer has inexact colors due to limitations of the language, but it is certainly likely other ancient languages could also have too-limited color-spaces to display the correct colors \$\endgroup\$
    – des54321
    Commented Mar 19, 2022 at 19:42
  • 3
    \$\begingroup\$ Related: codegolf.stackexchange.com/questions/230438/create-a-pride-flag \$\endgroup\$
    – tsh
    Commented Mar 20, 2022 at 5:10
  • 2
    \$\begingroup\$ Just pointing out that the yellow in the image above is #fbd02a and the blue is #1a54b2, as opposed to the #ffd700 and #0057b7 specified in the text. \$\endgroup\$ Commented Mar 21, 2022 at 22:48

84 Answers 84

1 2
3
5
\$\begingroup\$

Ly, 39 bytes

99*:3*&s*:[r009r,]+[r990r,]+'Po9lf2*f3r

Try it online!

This code pushes RGB values compatible with PPM ASCII formatted image into the stack, then creates a minimum PPM header before exiting. The numbers on the stack are printed automatically when the code exits.

Generate width/height numbers, figure out "half the pixels" count

 99*         - Push "81" on stack (will be 1/2 height, 1/3 width)
    :        - Duplicate top of stack
     3*      - Multiple by 3 (yields width)
       &s    - Save stack to backup (needed later for header)
         *   - Multiple to get "half the pixels"
          :  - Duplicate top of stack

Generate blue pixels, pushed onto the stack

 [      ]+  - Loops one per pixel
  r         - Reverse stack
   009      - Push "0 0 9" onto stack
      r     - Reverse to get pixel count back to top
       ,    - Decrement counter

Generate yellow pixels, pushed onto the stack

 [      ]+  - Loops once per pixel
  r         - Reverse stack
   990      - Push "9 9 0" onto stack
      r     - Reverse to get pixel count back to top
       ,    - Decrement counter

Print PPM header

 'Po          - Print "P"
    9         - Push 9 as "max RGB value"
     l        - Load "width and height" from backup cell
      f       - Flip top two entries (width, height)
       2*     - Double to get the height
         f    - Flip top two entries back
          3   - Push 3, the "PPM image type"
           r  - Reverse stack, stack prints automatically
\$\endgroup\$
5
\$\begingroup\$

C++, GCC 227216 chars

Hardly golf considering the language/tools, but still worth a try

#import<boost/gil/extension/io/pnm.hpp>
int n;int main(){boost::gil::rgb8_image_t i{999,666};for_each_pixel(view(i),[](auto&p){n++/999<333?p={0,87,183}:p={255,215,0};});write_view("U",view(i),boost::gil::pnm_tag{});}

It's not even obfuscated if reformatted:

#import <boost/gil/extension/io/pnm.hpp>
int n;
int main() {
    boost::gil::rgb8_image_t i{999, 666};
    for_each_pixel(view(i), [](auto& p) {
        n++ / 999 < 333 ? p = {0, 87, 183} : p = {255, 215, 0};
    });
    write_view("U", view(i), boost::gil::pnm_tag{});
}

The U file is in PNM format, meaning no link dependencies. Built with GCC-10 results in 19K binary.

Live On Compiler Explorer

Resulting file:

U: Netpbm image data, size = 999 x 666, rawbits, pixmap

enter image description here

\$\endgroup\$
2
  • 1
    \$\begingroup\$ 216 bytes \$\endgroup\$
    – gastropner
    Commented May 4, 2022 at 21:34
  • \$\begingroup\$ @gastropner Hah. Who'dve-thunk that you can simply #import header files :) Also, nice with the ternary around the assignment expression. \$\endgroup\$
    – sehe
    Commented May 4, 2022 at 21:54
5
\$\begingroup\$

Applesoft Basic, 88 bytes

1Y=0
2M=13
3GR
4COLOR=2
5HLIN0,38ATY
6Y=Y+1
7IFY<M GOTO5
8COLOR=13
9M=26
10Y=13
11GOTO5

Kind of cheating because it only is 39:26. I don't know if this is actually valid but it works here. A high res but worse color answer for 93 bytes:

1Y=0
2M=26
3HGR
4HCOLOR=6
5HPLOT0,Y TO78,Y
6Y=Y+1
7IFY<M GOTO5
8HCOLOR=5
9M=52
10Y=26
11GOTO5
\$\endgroup\$
5
\$\begingroup\$

Perl 5, 36 bytes

$#a=78;print".[${_}m@a
"x13for 44,43

Try it online!

Outputs using ANSI escape sequences.

\$\endgroup\$
4
  • \$\begingroup\$ For some reason, the link seems to show an 'L'-shaped flag with a long extension at the bottom right. Is this just a side-effect of the 'code sandbox'? \$\endgroup\$ Commented May 11, 2022 at 12:58
  • \$\begingroup\$ @DominicvanEssen Yeah, it's a side effect of ANSI escape codes being used in a window that's not long enough for the content, the same happens in a local terminal if you're most of the way down. It's annoying. I did briefly look at auto-sizing STDOUT for code-sandbox, but it would need to be resized before the content makes it there... \$\endgroup\$ Commented May 11, 2022 at 14:44
  • \$\begingroup\$ @DominicvanEssen Thanks for the nudge, I've just made a fix so that the xterm window will resize on input (not sure how quickly the cache is invalidated on GitHub pages, but thanks again for the prompt!) \$\endgroup\$ Commented May 11, 2022 at 19:53
  • \$\begingroup\$ Works perfectly now. Nice one. \$\endgroup\$ Commented May 11, 2022 at 20:50
5
\$\begingroup\$

BBC BASIC, 72 bytes

V. 19,0,16,255,215|25,97,78;26;25,4,78;27;19,0,16,0,87,183;25,97,-78;26;

Output

Output

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

Python + Pygame (122 bytes)

import pygame.display as p
s=p.set_mode((78,52))
s.fill((0,87,183),(0,0,78,26))
s.fill((255,215,0),(0,26,78,26))
p.flip()

Briefly flashes the flag on the screen before exiting.

enter image description here

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

C# (.NET Framework), 163 chars

Shorter version of Björn Larsson's answer.

void Draw(){var f=new Bitmap(78,52);for(int y=0;y<52;y++)for(int x=0;x<78;x++)f.SetPixel(x,y,Color.FromArgb(y>25?255:0,y>25?215:87,y>25?0:183));f.Save("f.bmp");}

Effective version with explanation:

using System.Drawing;

namespace Ukraine {
    internal class Program {
        static void Main() {
            // Create a bitmap.
            var f = new Bitmap(78, 52);

            // Iterate over the bitmap's pixels.
            for (int y = 0; y < 52; y++) {
                for (int x = 0; x < 78; x++) {
                    f.SetPixel(                  // Set the pixel...
                        x, y,                    // at the current x and y...
                        Color.FromArgb(
                            y > 25 ? 255 : 0,    // selecting the R value with a ternary expression...
                            y > 25 ? 215 : 87,   // selecting the G value with a ternary expression...
                            y > 25 ? 0 : 183)    // and selecting the B value with a ternary expression.
                        );
                }
            }

            // Save the bitmap.
            f.Save("f.bmp");
        }
    }
}

Produces an image in the exe's folder:

enter image description here

\$\endgroup\$
3
  • \$\begingroup\$ Welcome to Code Golf! One thing to note is that snippets are not allowed by our rules, so you would have to wrap this in a full program (all the Main stuff and whatnot). \$\endgroup\$
    – Seggan
    Commented Jul 7, 2022 at 16:27
  • 1
    \$\begingroup\$ A function would also be allowed \$\endgroup\$
    – mousetail
    Commented Jul 7, 2022 at 16:30
  • \$\begingroup\$ @Seggan C# supports top-level code nowadays. \$\endgroup\$
    – naffetS
    Commented Sep 25, 2022 at 18:37
5
\$\begingroup\$

SVG, 132 bytes

<svg width="300"height="200"><rect width="300"height="100"fill="#0057b7"/><rect y="100"width="300"height="100"fill="#ffd700"/></svg>

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

JavaScript (Node.js), 94 bytes

Prints a solid flag (or not quite, depending on your font) in the specified rgb colors.

for(i=1;i<4108;i++)process.stdout.write(i%79?`\x1b[38;2;${i<2054?'0;87;183':'255;215;0'}m█`:`
`)
\$\endgroup\$
5
\$\begingroup\$

WP2, 32 bytes

VWtsR1JpSUFBQUJYUlVKUVZsQTRUQlVBQUFBdlRjQU1BQkNRSkVQYjNUUmgvbWUzLzVuVUgvVUEK

WP2 (WebP2) is an image format based on AVIF. It's state-of-the-art in the field of DCT-based image formats, as far as I'm aware. It's also really good at lossless compression.

Encoded in Base64 RFC4648

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

Ruby + paint, 73 bytes

You do need the paint gem installed, from rubygems: gem install paint

Update:

Thanks to manatwork for the much shorter:

require'paint';['#0057b7','#ffd700'].map{|c|5.times{puts Paint[?█*37,c]}}

Original attempt (127 bytes):

require'paint';c=['#0057b7','#ffd700'];(0..1).each do |i|;(0..4).each do;(0..36).each do;print Paint['█',c[i]];end;puts;end;end

console output of Ukrainian flag from code.

\$\endgroup\$
3
  • 2
    \$\begingroup\$ Nice try, but not really matches the goal of code-golf challenges. May I suggest the mostly equivalent require'paint';['#0057b7','#ffd700'].map{|c|5.times{puts Paint[?█*37,c]}}? \$\endgroup\$
    – manatwork
    Commented Sep 25, 2022 at 12:14
  • \$\begingroup\$ Thanks @manatwork! I haven't used Ruby in a bit, but that's much shorter. \$\endgroup\$ Commented Sep 25, 2022 at 18:26
  • 1
    \$\begingroup\$ You can use a word array to save 2 bytes require'paint';%w(#0057b7 #ffd700).map{|c|5.times{puts Paint[?█*37,c]}} \$\endgroup\$
    – south
    Commented Oct 4, 2022 at 23:31
5
\$\begingroup\$

Ruby with Shoes, 96 characters

Shoes.app(width:78,height:52){background rgb 0,87,183
stroke fill rgb 255,215,0
rect 0,26,78,52}

Sample output:

flag of Ukraina

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

Lua + LÖVE, 154 characters

l=love
g=l.graphics
c=255
l.window.setMode(78,52)function l.draw()g.setBackgroundColor(0,87/c,183/c)g.setColor(1,215/c,0)g.rectangle("fill",0,26,78,52)end

Sample output:

flag of Ukraina

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

FLTK, 153 characters

Function{}{}{Fl_Window{}{xywh{9 9 78 52}}{Fl_Box{}{xywh{0 0 78 26}box FLAT_BOX color 0x0057b7ff}Fl_Box{}{xywh{0 26 78 52}box FLAT_BOX color 0xffd700ff}}}

Ungolfed:

Function {} {} {
    Fl_Window {} {
        xywh {9 9 78 52}
    } {
        Fl_Box {} {
            xywh {0 0 78 26}
            box FLAT_BOX
            color 0x0057b7ff
        }
        Fl_Box {} {
            xywh {0 26 78 52}
            box FLAT_BOX
            color 0xffd700ff
        }
    }
}

Sample output:

flag of Ukraina

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

C (MSVC) + Win32, 106 bytes

r[4];p(c){FillRect(GetDC(0),r,CreateSolidBrush(c));}main(){*r=600;r[1]=200;p(12344064);r[3]=400;p(55039);}

Draws the flag onto the top window: enter image description here

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

ARM Thumb machine code, Game Boy Advance, 36 bytes

04 20 00 06 05 49 01 80 48 06 09 0c 44 18 04 4a
13 0c 89 1e 42 52 63 52 fb d1 fe e7 03 04 00 96
40 59 5f 03

Thumb function that runs at a 4 byte aligned address.

ok the colors are actually a bit darker due to the GBA's messed up gamma

Assembler source:

    .syntax unified
    .arch armv4t
    .globl ukraine
    .thumb
    .thumb_func
    // Runs at a 4 byte aligned address
    .p2align 2,0
ukraine:
    // 0x04000000 = REG_DISPCNT
    movs    r0, #0x04
    lsls    r0, r0, #24
    // Load packed halfword constants
    ldr     r1, consts
    // REG_DISPCNT = MODE_3 | BG2_ENABLE
    // Set to display mode 3, enable BG2 layer
    strh    r1, [r0]
    // r0 = 0x...0403 << 25 = 0x06000000 = VRAM address
    lsls    r0, r1, #25
    // Shift in loop counter/offset (0x9600)
    lsrs    r1, r1, #16
    // r4 = 0x06009600 = second half of screen
    adds    r4, r0, r1
    // Load packed colors
    ldr     r2, colors
    // Unpack
    lsrs    r3, r2, #16
    // Double memset16 loop
.Lloop:
    // Decrement counter, set flags
    subs    r1, r1, #2
    // Store blue
    strh    r2, [r0, r1]
    // Store yellow
    strh    r3, [r4, r1]
    // Loop while not 0
    bne     .Lloop
    // Idle spin
.Lspin:
    b       .Lspin

    .p2align 2,0
consts:
    // MODE_3 | BG2_ENABLE
    // Also used for vram address
    .hword  0x0403
    // Half of the screen in halfwords
    .hword  240 * (160 / 2) * 2

    // Converts r8, g8, b8 to RGB5
    .macro RGB8 r, g, b
        .hword  ((\r >> 3) << 0)  | ((\g >> 3) << 5) | ((\b >> 3) << 10)
    .endm

colors:
    RGB8    0, 87, 183   // blue
    RGB8    255, 215, 0  // yellow

This function switches the GBA to display mode 3, where VRAM is treated as a flat bitmap of 16-bit RGB5 pixels. This is the easiest to manipulate as there is no need for tilemaps or palettes, I can just fill VRAM with colors.

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

Game Boy Color assembly, 99 97 bytes

Must be placed at $0150, to which it is jumped from $0100 (standard Game Boy stuff).

0E 40 3C E0 41 E6 02 E0 FF 76 F2 CB BF E2 3E 80
E0 68 21 69 FF AF 36 60 36 59 77 77 77 77 36 5F
36 03 3E 40 06 02 21 00 98 CD AB 01 3E 20 06 00
CD AB 01 AF 57 04 CD AB 01 04 CD AB 01 3E 10 42
26 80 CD AB 01 3E 20 06 FF CD AB 01 3E 30 70 23
72 23 BD 20 F9 F2 CB FF E2 18 FE 70 23 BD 20 FB
C9

Disassembly:

ROMCode:
    ld c, $40
; first we turn off the LCD so the LCD doesn't tell our mother[board] that it's their turn to access the VRAM

; wait for LCD Status to be at vblank to avoid hardware damage
; removing this "saves" eight bytes
; this is also our non-CGB/GBA/SGB2 filter: those will hang, because the way their A register is initialized, this will put them in an endless halt
.vblankWait:
    inc a ; on CGB and compatible, A is initialized to $11. in fact, that is how software is supposed to detect CGB compatibility.
    ldh [$ff41], a
    and $02
    ldh [$ffff], a
    db $76 ; halt. saves byte over "halt", which auto-inserts nop
           ; if you do this in a project, beware the halt bug.

; turn off the LCD
    ldh a, [c]
    res 7, a
    ldh [c], a

; now we fill the palette.

; the palette ram is indirectly accessed using a control register with an address and auto-increment bit and a data register
    ld a, $80
    ldh [$ff68], a
    ld hl, $ff69
    xor a

    ld [hl], $60
    ld [hl], $59
rept 4
    ld [hl], a
endr
    ld [hl], $5f
    ld [hl], $03

; now we fill the tile map.
    ld a, $40
    ld b, 2
    ld hl, $9800
    call FillWithB

    ld a, $20
    ld b, 0
    call FillWithB

    xor a
    ld d, a ; for later
    inc b
    call FillWithB

    inc b
    call FillWithB

; and now we have tile 0 be all color 0b00, tile 1 be all color 0b11, and tile 2 be all color 0b10
    ld a, $10
    ld b, d
    ld h, $80
    call FillWithB
    
    ld a, $20
    ld b, $ff
    call FillWithB

    ld a, $30
.fillTwoValues:
    ld [hl], b
    inc hl
    ld [hl], d
    inc hl
    cp l
    jr nz, .fillTwoValues

; turn on the LCD
    ldh a, [c]
    set 7, a
    ldh [c], a

; and now that we're done let's spin.
    jr @

FillWithB:
    ld [hl], b
    inc hl
    cp l
    jr nz, FillWithB
    ret

demonstration on BGB

Game Boy Color colors are wonky. It's also 10:7, but that's close enough to 3:2.

Edit 1: Missed completing an optimization I had already partially done (ldh with [c]). -2 bytes.

Edit 2: More safe vblank wait code. No size change.

Edit 3: Edit 2 broke the second halt, which is now replaced with an infinite spin because fixing it requires more code. No size change, +1 embarrassment from not checking thoroughly enough.

\$\endgroup\$
4
\$\begingroup\$

Python (Thonny 4.0 REPL), 419 bytes

print("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAARMAAAC3CAMAAAAGjUrGAAAAD1BMVEUAW7v/1QAAVb+ln3n/2QAG3wIgAAAAzUlEQVR4nO3QsQGAMAzAsBT4/2b2eO0onaAZAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACASx62ednmY5vD5qSclJNyUk7KSTkpJ+WknJSTclJOykk5KSflpJyUk3JSTspJOSkn5aSclJNyUk7KSTkpJ+WknJSTclJOykk5KSflpJyUk3JSTspJOSkn5aSclJNyUk7KSTkpJ+WknJSTclJOykk5KSflpJyUk3JSTspJOSkn5aSclJNyUk7KSTkpJ+WknJSTclJOykk5KSflpH7zk2pa0LCuDAAAAABJRU5ErkJggg==")

Longer than a standard answer but ok. Thonny supports printing images if they’re in base64 form.

enter image description here

\$\endgroup\$
1
  • \$\begingroup\$ Your first answer is valid, if a little long (although I'd change the title to something like "Python 3 (Thonny REPL), 419 bytes"; your second answer isn't valid as it's not actually a program which outputs the image. (And the image isn't correctly formatted anyway.) \$\endgroup\$
    – Ginger
    Commented Dec 27, 2022 at 17:10
4
\$\begingroup\$

HTML, 200 Bytes

<mark style="background-color:blue"id="a"></mark><mark id="b"></mark><script>document.getElementById("a").innerHTML=document.getElementById("b").innerHTML=(" ".repeat(78)+"<br>").repeat(26)</script>

Those poor Ukrainians... 😔

You should click on the link to render the full page after running the code.

Prints the Ukrainian flag (although it's missing a trident unfortunately) using <mark> tags to render the colours. Uses a script to insert the text. Uses U+2001 EM QUAD as a blank character.

\$\endgroup\$
1
  • \$\begingroup\$ You can use background in place of background-color. \$\endgroup\$
    – corvus_192
    Commented Sep 10, 2023 at 16:57
4
\$\begingroup\$

Uiua, 34 bytes

&ims÷255⊂∩(↯26_78_3)0_87_183 255_215_0

Try it in the pad!

Explanation

0_87_183 255_215_0: Push the RGB values on the stack

∩(…): Apply this function to both values:

  • ↯26_78_3: Reshape them so they become stripes

&ims÷255⊂: Join them together and divide by 255, then show as an image

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

Chipmunk BASIC, 116 bytes

Chipmunk Basic v368b2.02 on MacOS. This fun wee lang adjusts RGB values by a factor0.392157.

graphics 0
graphics color 0,34,72
graphics fillrect 0,0,132,44
graphics color 100,84,0
graphics fillrect 0,44,132,88
\$\endgroup\$
3
\$\begingroup\$

SVG - Scalable Vector Graphics. 151 bytes

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 3 2"><path d="M0 0H3V1H0ZM0 1H3V2H0Z" fill="#005BBB"/><path d="M0 1H3V2H0Z" fill="#FFD500"/></svg>

Try it online

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

Python 3, 152 bytes

A repurposing of my answer to the Pride flag challenge, with some small improvements to the visuals.

from curses import*
s=initscr();start_color();y=1
for x in[12,11,0]:w=newwin(13,78,~-y*13,0);init_pair(y,1,x);w.bkgd(' ',color_pair(y));w.refresh();y+=1

The flag of Ukraine, in a terminal window

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

JSL (JMP Scripting Language), 110 bytes

NewWindow("",Graph(FillColor({1,43/51,0});Rect(0,33,99,66,1);FillColor({0,29/85,183/255});Rect(0,66,99,99,1)))

enter image description here

\$\endgroup\$
1 2
3

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.