Блог пользователя lis05

Автор lis05, 3 месяца назад, По-английски

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]);
  • Проголосовать: нравится
  • +201
  • Проголосовать: не нравится

»
3 месяца назад, # |
Rev. 2   Проголосовать: нравится +5 Проголосовать: не нравится

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

btw, is it faster or slower than Python?

  • »
    »
    3 месяца назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    "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 месяца назад, # ^ |
        Проголосовать: нравится +1 Проголосовать: не нравится

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

      • »
        »
        »
        »
        3 месяца назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        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 месяца назад, # ^ |
            Проголосовать: нравится -8 Проголосовать: не нравится

          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 месяца назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

  • »
    »
    3 месяца назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    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 месяца назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

nice

»
3 месяца назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

A madlad indeed!