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

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

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

»
21 месяц назад, скрыть # |
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?

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

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

»
21 месяц назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

nice

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

A madlad indeed!