Created
March 6, 2024 23:19
-
-
Save tamx/1565aef2da5db4b3b1c14ce1a29465d2 to your computer and use it in GitHub Desktop.
Alpha Judge0 sample.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as ts from "typescript"; | |
const code: string = ` | |
const solve = (heights: number[]): number => { | |
let max = 0; | |
for (let x1 = 0; x1 < heights.length; x1++) { | |
for (let x2 = 0; x2 < heights.length; x2++) { | |
const h = Math.min(heights[x1], heights[x2]); | |
const w = Math.abs(x2 - x1); | |
max = Math.max(max, h * w); | |
} | |
} | |
return max; | |
}`; | |
const jscode = ts.transpile(code); | |
console.log(jscode); | |
const HOST = "alpha-1:2358" | |
// Get yours for free at https://judge0.com/ce or https://judge0.com/extra-ce | |
const API_KEY = ""; | |
var language_to_id = { | |
"Bash": 46, | |
"C": 50, | |
"C#": 51, | |
"C++": 54, | |
"Java": 62, | |
"Python3": 71, | |
"Ruby": 72, | |
"TypeScript": 74, | |
}; | |
function encode(str: string): string { | |
const bytes = new TextEncoder().encode(str); | |
const base64 = base64encode(bytes); | |
return base64 | |
} | |
function decode(base64: string): string { | |
const bytes = base64decode(base64); | |
const text = new TextDecoder().decode(bytes); | |
return text; | |
} | |
function base64encode(data: Uint8Array) { | |
return btoa([...data].map(n => String.fromCharCode(n)).join("")); | |
} | |
function base64decode(data: string) { | |
return new Uint8Array([...atob(data)].map(s => s.charCodeAt(0))); | |
} | |
function check(token: string) { | |
console.log("Checking submission status..."); | |
fetch(`http://${HOST}/submissions/${token}?base64_encoded=true`, | |
{ | |
method: "GET", | |
headers: { | |
"x-rapidapi-host": HOST, | |
"x-rapidapi-key": API_KEY | |
}, | |
}).then(data => { | |
// console.log(data); | |
return data.json(); | |
}).then(json => { | |
if ([1, 2].includes(json["status"]["id"])) { | |
console.log("Status: " + json["status"]["description"]); | |
setTimeout(function () { | |
check(token) | |
}, 1000); | |
} else { | |
const output = decode(json["stdout"]); | |
const out = json["status"]["description"] + '\n' + | |
output; | |
console.log(out); | |
} | |
}) | |
} | |
function run(tscode: string, expected: string) { | |
fetch(`http://${HOST}/submissions?base64_encoded=true&wait=false`, | |
{ | |
method: "POST", | |
headers: { | |
'Content-Type': "application/json", | |
"x-rapidapi-host": HOST, | |
"x-rapidapi-key": API_KEY | |
}, | |
body: JSON.stringify({ | |
"language_id": language_to_id["TypeScript"], | |
"source_code": encode(tscode), | |
"stdin": "", | |
"expected_output": encode(expected), | |
"redirect_stderr_to_stdout": true | |
}) | |
}).then(data => { | |
// console.log(data); | |
return data.json(); | |
}).then(json => { | |
const token = json["token"]; | |
// console.log(token); | |
setTimeout(function () { | |
check(token); | |
}, 1000); | |
}) | |
} | |
const tscode = ` | |
const runnable: any = new Function(\`${jscode}\` + ';return solve;')(); | |
const result = runnable([1, 8, 6, 2, 5, 4, 8, 3, 7]); | |
console.log(result);`; | |
run(tscode, "49"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment