lis05's blog

By lis05, 3 months ago, In English

So for the last couple of months I've been busy making a simple (and unfortunately slow) interpreted programming language called Cotton. Today, I was able to compress its entire source code into a single file and solve an atcoder problem using a small program in Cotton written in a huge interpreter in C++.

UPD: I rewrote the "glueing" script in Cotton. You can compare python and cotton scripts if you want (they work almost identically. however, i dont have regex library, so i could not remove comments using re like in python).

Why you need this information? You don't. I just wanted to share my excitement with someone.

The program in Cotton:

arr = make(Array)
    .resize(read(Integer))
    .apply(function(x){x=read(Integer);});

target = arr.copy().sort(function(a,b){a>b;})[1];

// don't have a function that would return element's position yet
for i = 0; i < arr.size(); i++; {
    if arr[i] == target; {
        println(i + 1);
        return;
    }
}

If you want to look at the beautiful obfuscated code of the interpreter, it's available here

UPD: I managed to optimize the code a bit, and therefore I was able to solve atcoder frog1 problem in 1.998 seconds :)

The solution:


arr = make(Array) .resize(read(Integer)) .apply(function(x){x=read(Integer);}) .append(0, 0); n = arr.size() - 2; dp = make(Array) .resize(arr.size()) .apply(function(x){x=100000000000000;}); dp[0] = 0; for i = 0; i < n; i++; { dp[i + 1] = min(dp[i + 1], dp[i] + abs(arr[i] - arr[i + 1])); dp[i + 2] = min(dp[i + 2], dp[i] + abs(arr[i] - arr[i + 2])); } println(dp[n - 1]);
  • Vote: I like it
  • +201
  • Vote: I do not like it

»
3 months ago, # |
Rev. 2   Vote: I like it +5 Vote: I do not like it

That is pretty cool. It kind of looks like Rust with all those methods.

btw, is it faster or slower than Python?

  • »
    »
    3 months ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    "Cotton is a learning project. It is not meant to be used in the real life. Cotton is incredibly slow (10 times slower than Python on simple loops)." (GitHub)

    • »
      »
      »
      3 months ago, # ^ |
        Vote: I like it +1 Vote: I do not like it

      ty. honestly, the language just looks like it'd be fast, so that's kinda weird

      • »
        »
        »
        »
        3 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Cotton is a "pure" interpreted language. It means that it doesn't have a virtual machine, and is not compiled to any sort of byte code or intermediate language. It is literally interpreted. This gives a huge slowdown factor, as each Cotton instruction must be passed through a handful of functions, and a lot of work has to be done in order for that instruction to get executed.

        (Also, the design of the language could be improved a lot, but I didn't know that at the time :))

        • »
          »
          »
          »
          »
          3 months ago, # ^ |
            Vote: I like it -8 Vote: I do not like it

          It certainly looks like one of those non-python languages, so good job there.

          Since you're here, I must ask, have you fulfilled your promise yet?

          • »
            »
            »
            »
            »
            »
            3 months ago, # ^ |
              Vote: I like it +1 Vote: I do not like it

            I'll text you when I get married

            (no)

»
3 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Why didn't you solve codeforces problem then? :D

  • »
    »
    3 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    When "glued" into one file, Cotton interpreter is about 800kb of code. Even with compressions and obfuscations that I did, it still weights more that 200kb. As you know, Codeforces doesn't allow submissions larger than 64kb.

    Atcoder, on the other hand, has the limit of 512kb, which is more than enough.

    • »
      »
      »
      3 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      What tool did you use to compress the code?

      • »
        »
        »
        »
        3 months ago, # ^ |
        Rev. 2   Vote: I like it 0 Vote: I do not like it

        I wrote a tiny python script that basically replaces a lot of big tokens with smaller ones.

        Should probably rewrite it in Cotton tho.

      • »
        »
        »
        »
        3 months ago, # ^ |
          Vote: I like it +5 Vote: I do not like it

        Now i have a Cotton script as well xd

»
3 months ago, # |
  Vote: I like it +1 Vote: I do not like it

nice

»
3 months ago, # |
  Vote: I like it 0 Vote: I do not like it

A madlad indeed!