AOC_2022/5/script.js
2022-12-09 22:08:42 +01:00

139 lines
3.7 KiB
JavaScript

let output = document.getElementById("output");
let errorOutput = document.getElementById("error");
let input;
let linesArr;
let parsedArr = [[], [], [], [], [], [], [], [], []];
let cmdArr = [];
let copy9001 = [];
function readFile(input) {
let file = input.files[0];
let fileReader = new FileReader();
fileReader.readAsText(file);
fileReader.onload = function() {
input = fileReader.result;
linesArr = input.split("\n");
};
fileReader.onerror = function() {
alert(fileReader.error);
};
}
let start = document.getElementById("start");
start.addEventListener("mouseover", () => {
start.innerHTML = "pwease pwess mee hawwd mastaaa! ~^o^~"
});
start.addEventListener("mouseout", () => {
start.innerHTML = "sowwy mastaa! Ow0;"
});
start.addEventListener("click", () => {
try {
parseArr();
}
catch(e) {
console.error(e);
errorOutput.innerHTML = "^^^ pwease gibe me file fiwwst!!1! :3";
return 1;
}
if (errorOutput.innerHTML != "") {
errorOutput.innerHTML = "senk ju~ *UwU~";
}
parseCmd();
copy9001 = [].concat(parsedArr);
console.log("🚀 ~ file: script.js:44 ~ start.addEventListener ~ copy9001", copy9001)
for(let i=0; i<cmdArr.length; i++) {
//move(cmdArr[i][0], cmdArr[i][1], cmdArr[i][2]); //uncomment this, you lazy fuck
move9001(cmdArr[i][0], cmdArr[i][1], cmdArr[i][2]);
}
let answer = "";
for(let i=0; i<parsedArr.length; i++) {
answer += parsedArr[i][parsedArr[i].length - 1];
}
let answer2 = "";
for(let i=0; i<copy9001.length; i++) {
answer2 += copy9001[i][copy9001[i].length - 1];
}
console.log("parsedArr after move()", parsedArr);
console.log("ANSWER UwU", answer, answer2);
output.innerHTML += " " + answer + ", " + answer2 + " &nbsp;&nbsp;&nbsp; ╭∩╮UwU╭∩╮";
});
function parseArr() { //parsing is hard
let stackArr = [];
let cleanArr = [];
for(let l=0; l<9; l++) {
let tmpLine = [];
for(let i=0; i<linesArr[l].length; i++) {
if(linesArr[l].charAt(i)=='[') tmpLine[i+1] = linesArr[l].charAt(i+1);
if(l==8) {
if(typeof(parseInt(linesArr[l].charAt(i)))=="number") tmpLine[i] = linesArr[l].charAt(i);
if(tmpLine[i] == ' ') tmpLine[i]=undefined;
}
}
stackArr[l] = tmpLine;
}
console.log("stackArr", stackArr);
for(let i=0; i<stackArr.length; i++) {
let tmpLine = [];
let c=0;
for(let j=0; j<stackArr[i].length; j++) {
if(stackArr[8][j]!=undefined) {
tmpLine[c] = stackArr[i][j]
c++;
}
}
cleanArr[i] = tmpLine;
}
console.log("cleanArr", cleanArr);
for(let i=0; i<cleanArr.length; i++) {
let c=8;
for(let j=0; j<8; j++) {
if(cleanArr[j][i] != undefined) {
parsedArr[i][c] = cleanArr[j][i];
}
c--;
}
parsedArr[i].splice(0,1);
}
console.log("parsedArr", parsedArr);
}
function move(a, s, d) {
for(let i=0; i<a; i++) {
parsedArr[d-1].push(parsedArr[s-1].pop());
}
}
function move9001(a, s, d) {
let tmpArr = [];
for(let i=0; i<a; i++) {
tmpArr[i] = copy9001[s-1].pop();
}
for(let i=a-1; i>=0; i--) {
copy9001[d-1].push(tmpArr[i]);
}
}
function parseCmd() {
for(let i=10; i<linesArr.length; i++) {
let tmpLine = [];
let tmpBool = true;
for(let j=0; j<linesArr[i].length; j++) {
if(linesArr[i].charAt(j)=='m' && tmpBool) {
let tmp = 1;
tmpBool = false;
if(linesArr[i][j+1]!=' ') tmp = 2
tmpLine[tmpLine.length] = linesArr[i].substr(j+5,tmp);
}
if(linesArr[i].charAt(j)=='f') tmpLine[tmpLine.length] = linesArr[i].substr(j+5,1);
if(linesArr[i].charAt(j)=='t') tmpLine[tmpLine.length] = linesArr[i].substr(j+3,1);
}
cmdArr[i-10] = tmpLine;
}
console.log("cmdArr", cmdArr);
}