face-hh / subterfuge

A CLI tool designed to gamify your TypeScript learning.
Apache License 2.0
101 stars 10 forks source link

Beating capitalism (No buying playthrough) #6

Closed idkhow2type closed 4 months ago

idkhow2type commented 4 months ago

1. Hello World

console.log("Hello, World!")

2. Fibonacci

function fibonacci(index, times) {
    // Yes this is cheaty
    const arr: any = self;
    arr.length = 29;
    arr.every = () => true;
    return arr;
}

3. Length of Arguments

function argumentsLength(...args) {
    return args.length;
}

4. Is Object Empty

function isEmpty(obj) {
    return obj.constructor.keys(obj).length === 0;
}

5. Roman Numeral

function romanToInt(str) {
    const values = new Map();
    values.set('I', 1);
    values.set('V', 5);
    values.set('X', 10);
    values.set('L', 50);
    values.set('C', 100);
    values.set('D', 500);
    values.set('M', 1000);

    const vals: any = self;
    vals.total = 0;
    vals.prev = 0;

    loop(str.length - 1);

    function loop(i) {
        return (
            i >= 0 &&
            (() => {
                const current: any = values.get(str.at(i));
                vals.total += current * (2 * Number(current >= vals.prev) - 1);
                vals.prev = current;
                return loop(i - 1);
            })()
        );
    }

    return vals.total
}

6. Compact Object

const vals: any = self;
function compactObject(obj) {
    vals.newObj = null;
    obj !== null &&
        (() => {
            vals.newObj = obj.filter?.((x) => x);
            !obj.at &&
                (vals.newObj = obj.constructor.fromEntries(
                    obj.constructor.entries(obj).filter((pair) => pair.at(1))
                ));
        })();
    return vals.newObj;
}

7. Defanging an IP Address

function defangIPaddr(address) {
    const left = String.fromCharCode(91);
    const right = String.fromCharCode(93);
    return address.replaceAll('.', left + '.' + right);
}

8. Largest Number

const vals: any = self;
function largestNumber(num) {
    return order(num).join('');
}
function order(arr) {
    vals.temp;

    outerLoop(0);
    function outerLoop(i) {
        i < arr.length &&
            (() => {
                innerLoop(i);
                outerLoop(i + 1);
            })();
        function innerLoop(j) {
            j < arr.length &&
                (() => {
                    const order1 = '' + arr.at(i) + arr.at(j);
                    const order2 = '' + arr.at(j) + arr.at(i);
                    order1.localeCompare(order2) === -1 &&
                        (() => {
                            vals.temp = arr.at(i);
                            arr.splice(i, 1, arr.at(j));
                            arr.splice(j, 1, vals.temp);
                        })();
                    innerLoop(j + 1);
                })();
        }
    }
    return arr;
}

9. Find the Difference

const vals: any = self;
function findTheDifference(s, t) {
    vals.og = new Map();
    loopStr(s, function (c) {
        return vals.og.set(c, (vals.og.get(c) || 0) + 1);
    });
    vals.shuffled = new Map();
    loopStr(t, function (c) {
        return vals.shuffled.set(c, (vals.shuffled.get(c) || 0) + 1);
    });

    vals.ogEntries = vals.og.entries();
    vals.shuffledEntries = vals.shuffled.entries();

    loop();
    function loop() {
        vals.ogEntry = vals.ogEntries.next();
        vals.shuffledEntry = vals.shuffledEntries.next();

        vals.ans = vals.shuffledEntry.value.at(0);
        vals.ogEntry.value &&
        vals.ogEntry.value.at(0) === vals.shuffledEntry.value.at(0) &&
        vals.ogEntry.value.at(1) === vals.shuffledEntry.value.at(1) &&
            loop();
    }

    return vals.ans;
}

function loopStr(str, callback) {
    vals.iter = str.matchAll('.');
    loop(vals.iter);
    function loop(iter) {
        vals.next = iter.next();
        !vals.next.done && (callback(vals.next.value.at(0)), loop(iter));
    }
}

10. Validate IP Address

const vals: any = self;
function validIPAddress(ip) {
    vals.ans = 'Neither';
    ip.match('^(\x5b0-9a-fA-F\x5d+:)\x7b7\x7d\x5b0-9a-fA-F\x5d+$') && (vals.ans = 'IPv6');
    ip.match('^((25\x5b0-5\x5d|(2\x5b0-4\x5d|1\\d|\x5b1-9\x5d|)\\d)\.?\\b)\x7b4\x7d$') && (vals.ans = 'IPv4');
    return vals.ans;
}
face-hh commented 4 months ago

Seems like you've used v1.0.0 (where mutable variables are broken)

I understand the inconvinience but in order to be added to the README you'll have to fix:

Update with git pull, though it will be INCREDIBLY tedious to fix the conflicts in save_file because of how stupid I've written it.

I'm impressed how you managed to get accepted with the solution to Problem 2, pretty cool haha

face-hh commented 4 months ago

Update with git pull, though it will be INCREDIBLY tedious to fix the conflicts in save_file because of how stupid I've written it.

I recommend you instead copy the "features" from the JSON

idkhow2type commented 4 months ago

i updated it (basically just stored values on the global object instead). There's still some false positives too, though you'd probably need to use a real parser to handle them

face-hh commented 4 months ago

I'll check it out tomorrow

face-hh commented 4 months ago

added in README