AOC_2022/5/script.js

139 lines
3.7 KiB
JavaScript
Raw Normal View History

let output = document.getElementById("output");
2022-12-08 12:46:04 +01:00
let errorOutput = document.getElementById("error");
let input;
let linesArr;
2022-12-09 21:59:43 +01:00
let parsedArr = [[], [], [], [], [], [], [], [], []];
let cmdArr = [];
2022-12-09 21:59:43 +01:00
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", () => {
2022-12-08 12:46:04 +01:00
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();
2022-12-09 21:59:43 +01:00
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]);
}
2022-12-09 21:59:43 +01:00
let answer = "";
for(let i=0; i<parsedArr.length; i++) {
2022-12-09 21:59:43 +01:00
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);
2022-12-09 21:59:43 +01:00
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++) {
2022-12-08 12:46:04 +01:00
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);
2022-12-08 12:46:04 +01:00
for(let i=0; i<cleanArr.length; i++) {
2022-12-09 21:59:43 +01:00
let c=8;
for(let j=0; j<8; j++) {
2022-12-09 21:59:43 +01:00
if(cleanArr[j][i] != undefined) {
parsedArr[i][c] = cleanArr[j][i];
}
c--;
}
2022-12-09 21:59:43 +01:00
parsedArr[i].splice(0,1);
}
2022-12-06 20:14:08 +01:00
console.log("parsedArr", parsedArr);
}
function move(a, s, d) {
for(let i=0; i<a; i++) {
2022-12-09 21:59:43 +01:00
parsedArr[d-1].push(parsedArr[s-1].pop());
}
}
2022-12-09 21:59:43 +01:00
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++) {
2022-12-06 20:14:08 +01:00
let tmpLine = [];
let tmpBool = true;
for(let j=0; j<linesArr[i].length; j++) {
if(linesArr[i].charAt(j)=='m' && tmpBool) {
2022-12-06 20:14:08 +01:00
let tmp = 1;
tmpBool = false;
2022-12-06 20:14:08 +01:00
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);
}
2022-12-06 20:14:08 +01:00
cmdArr[i-10] = tmpLine;
}
2022-12-06 20:14:08 +01:00
console.log("cmdArr", cmdArr);
2022-12-09 21:59:43 +01:00
}