GithubHelp home page GithubHelp logo

issotm / 1234-golf Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 1.0 19 KB

Code golfing contest: print "1234"

License: Other

Brainfuck 0.73% Shell 2.75% Python 22.94% Assembly 5.92% C 23.02% C++ 9.44% HTML 2.28% JavaScript 6.38% PHP 0.36% Java 7.47% MATLAB 0.96% Rust 17.10% Ruby 0.65%

1234-golf's Introduction

Hi!

The name's Eldred, Eldred Habert.

I mainly deal with lower-level stuff, and making tools to help others make great stuff out of them. I'm a regular over at gbdev, if you're interested in making a game on a retro platform for fun, this might be worth checking out!

I'm the main maintainer of RGBDS, the main toolchain for programming on the Game Boy. If you're interested in contributing, we have some beginner-friendly tasks!

I have a Game Boy programming tutorial in progress; if you'd be interested in translating it, you can help! Please open an issue to let me know, and we can get started :)

Thanks for stopping by, have fun on the rest of your browsing journey o/

1234-golf's People

Contributors

issotm avatar meithecatte avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

meithecatte

1234-golf's Issues

1234 - C (56 characters)

Here ya go~

Compiles with gcc main.c

// a=argc (1)
main(a)
{
    // b=1, c=4
    // Loop until a == 5
    for(int b=a,c=b+b<<b;a<=c;)

        // c-b<<c  ==  3<<4  ==  48 (ASCII '0')
        // Add a to print its ASCII value
        putchar(c-b<<c|a++);
}

Minimal version:

main(a){for(int b=a,c=b+b<<b;a<=c;)putchar(c-b<<c|a++);}

Rust: 115 Characters

extern { fn putchar(x: usize); } // links to putchar() in libc.

fn main() {
    let x = true as usize;       // x = 1
    let y = x + x + x;           // y = 3
    let z = (y << x + y) + x;    // z = 49
    for c in z..=z + y {         // loop c from 49 (z) to 52 (z + y)
        unsafe { putchar(c); }   // write c to stdout. extern functions are unsafe
    }
}

My dumbass at 1am forgot true existed and I ended up shaving off about 20 characters.
I highly doubt this can get any smaller without a different approach. x could have been reused by making it mutable but that wouldn't affect the character count.

Another C Submission

Saw this from someone I know so I thought I'd try my hand at it, this is what I ended up with:

a,b; // Initialize a/b to 0

main() {
  ++a; // Generate 1
  b = a <<= a + a; // Generate 4 from 1 << 2 or 1 << (1 + 1), store to a/b
  b *= a + a + a; // Generate 48 from 4 * 12 or 4 * (4 + 4 + 4), store to b

  while (a--) // Loop 4 times
    putchar(++b); // Print out characters starting from ASCII 41
}

Which when compacted is 58 characters:
a,b;main(){++a;b=a<<=a+a;b*=a+a+a;while(a--)putchar(++b);}

Note that compiles fine in a C compiler like gcc/clang if one actually tells it the language is C (the online compiler you linked on the project page assumes the code is C++ due to the extension) since types on global variables are not required (unlike in ISO C++ apparently). This can be seen here (also note that this code has no undefined behavior unlike some of the other submissions, only warnings due to the horrid language hacks for default int in various contexts as expected).

If that's not acceptable then this should be fine as 62 characters, but it shouldn't need to be this way in C as mentioned before:
int a,b;main(){++a;b=a<<=a+a;b*=a+a+a;while(a--)putchar(++b);}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.