AOC_2022/5/script.js
2022-12-06 12:42:16 +01:00

100 lines
2.4 KiB
JavaScript

let output = document.getElementById("output");
let input;
let linesArr;
let parsedArr = [];
let cmdArr = [];
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", () => {
parseArr(); //continue with parsedArr
parseCmd();
});
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-1; 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++) {
tmpLine = [];
let c=7;
for(let j=0; j<8; j++) {
if(cleanArr[j][i] != undefined) {
tmpLine[c] = cleanArr[j][i];
}
c--;
}
parsedArr[i] = tmpLine;
}
}
function move(a, s, d) {
for(let i=0; i<a; i++) {
parsedArr[s-1][getTopPos()] = parsedArr[d-1][getTopPos()];
}
}
function getTopPos(s) {
let o=0;
for(let i=0; i<parsedArr[s].length; i++) {
if(parsedArr[s][i]==undefined) {
o=i-1;
break;
}
}
return o;
}
function parseCmd() {
for(let i=10; i<linesArr.length; i++) {
for(let j=0; j<linesArr[i].length; j++) {
if(linesArr[i].charAt(j)=='m') cmdArr[i-10] = linesArr[i].substr(j+2,1);
if(linesArr[i].charAt(j)=='f') cmdArr[i-10] = linesArr[i].substr(j+2,1);
if(linesArr[i].charAt(j)=='t') cmdArr[i-10] = linesArr[i].substr(j+2,1);
}
}
}