Byte Heist Home Leaderboard
Join the Heist (with Github)
Solve View

Sorta Sorted Strings

For each line in the input, output its Sorta Sorted Score.

The Sorta Sorted Score of a string is the sum of the costs of its bytes according to these rules:

Your score on this challenge, instead of being the raw byte length of your source code, will be the Sorta Sorted Score of your source code.

In order to make this scoring scheme work, please calculate your Sorta Sorted Score and pad your solution with an appropriate amount of spaces to give yourself that score. For example, if your source is abdcdd, append 58 spaces: 49 spaces for the “c” plus 9 spaces for the last “d”.

NOTE: Input lines will consist entirely of printable ASCII.

Judge

(async function*(context: Context): Challenge {
	
    const enc = new TextEncoder(),
		trimmedCode = context.code.replace(/ *$/, ''),
        sortaSortedScore = function(str: String): Number {
            let score = 0,
                prevByte = -1;
            for (let byte of enc.encode(str)) {
                score += byte > prevByte ? 1 : byte < prevByte ? 50 : 10;
                prevByte = byte;
            }
            return score;
        },
        expectedScore = sortaSortedScore(trimmedCode),
        actualScore = enc.encode(context.code).length;
    yield context.registerTestCase(new TestCase("Correctly scored", expectedScore <= actualScore ? "Pass" : "Fail", {
        "Diff": {
            "expected": expectedScore + " bytes",
            "output": actualScore + " bytes"
        }
    }));
    const testCases =
        yield* context.runTestCases(
            ["abdcdd", "Hello, World!", " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~",
                "~}|{zyxwvutsrqponmlkjihgfedcba`_^]\\[ZYXWVUTSRQPONMLKJIHGFEDCBA@?>=<;:9876543210/.-,+*)('&%$#\"! ", "c%{3/xIIRx2[gS:ob$Rm;1 VNP)t#@@q+;RF }83-li1mG8jSi", "/]'>&n|Viz|/h0XO2ULl9{LQEmGTfkDkDafa3C1[BohyR8* i ]Ii#KD4rw`r'XQa`djK6", "0001111220120011220011234567789989112233445566", "!!!!!!!!!!!!!!!!", "(6GZ(((T0g;!9y)@>bfTl!bCac4H<\\k eh", "t?:A:[~w26 %?l11r/Pa~|G6<IBxO14{b7L=h,pZt;.t",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pD)", "#T>C#7r~#q:D", "@okHiQ<HlDfv50es,[`vCob(FP>SO\\95HduM+O@/8:q[", "M]k pmpS-~B*=ME;[", "S;R.aabbaabbbaaabbb)|.~pjh.2J6", "j;>u'AfLXDZ&9'S.!^3c4ia?T~eXcsT/>pdeg[# xl@SgaG+!n)\"jG^ieNZ.S)\"De'T%UE-BF*f", "=~ynL]3B~<iJCU9q#PZ),$N.`nuNC9dC[n7'J=/7-9\\c1LXEaUfK|c,#3UhQ1r", "ud?C<~VwkqM.4!6w4:kI\"AjLeOOMUbi=,w", "  "
            ].map(s => [s, "" + sortaSortedScore(s)])
        );
    return context.noFailures();
})

Example Code

const sortaSortedScore = function(str) {
    let score = 0,
        prevByte = -1;
    for (let byte of new TextEncoder().encode(str)) {
        score += byte > prevByte ? 1 : byte < prevByte ? 50 : 10;
        prevByte = byte;
    }
    return score;
};

var fs = require("fs");
var stdinBuffer = fs.readFileSync(0); // STDIN_FILENO = 0
console.log(stdinBuffer.toString().split("\n").map(sortaSortedScore).join("\n"));                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          

Comments