71 lines
1.5 KiB
TypeScript
71 lines
1.5 KiB
TypeScript
import { readFileSync } from 'fs';
|
|
import { join } from 'path';
|
|
|
|
|
|
function transpose(matrix) {
|
|
return matrix.reduce((prev, next) => next.map((item, i) =>
|
|
(prev[i] || []).concat(next[i])
|
|
), []);
|
|
}
|
|
|
|
const fileString = readFileSync(join(__dirname, "input"), 'utf-8');
|
|
const problemParts = fileString.split("\n\n");
|
|
|
|
const rows = problemParts[0].split("\n");
|
|
var rotRows = [];
|
|
for (const row of rows) {
|
|
rotRows.push(row.match(/.{1,4}/g));
|
|
}
|
|
|
|
rotRows.pop();
|
|
|
|
rotRows = transpose(rotRows);
|
|
|
|
rotRows = rotRows.map(element => {
|
|
return element.map(val => {
|
|
return val.trim();
|
|
});
|
|
})
|
|
|
|
rotRows = rotRows.map(element => {
|
|
return element.filter(val => {
|
|
return val.length != 0;
|
|
});
|
|
})
|
|
|
|
rotRows = rotRows.map(element => {
|
|
return element.reverse()
|
|
});
|
|
|
|
const movesRawText = problemParts[1].trim();
|
|
var movesRaw = movesRawText.split("\n");
|
|
movesRaw = movesRaw.filter(val => {
|
|
return val.length != 0;
|
|
});
|
|
|
|
var parsedMoves = [];
|
|
|
|
for (const rawMove of movesRaw) {
|
|
const cr = rawMove.split('move')[1].split("from");
|
|
const count: number = +cr[0].trim();
|
|
const mr = cr[1].split("to");
|
|
const start: number = +mr[0].trim();
|
|
const end: number = +mr[1].trim();
|
|
parsedMoves.push([count, start - 1, end - 1])
|
|
}
|
|
console.table(rotRows);
|
|
|
|
for (const move of parsedMoves) {
|
|
var tmp = [];
|
|
for (var i = 0; i < move[0]; i++) {
|
|
tmp.push(rotRows[move[1]].pop());
|
|
}
|
|
tmp = tmp.reverse();
|
|
rotRows[move[2]] = rotRows[move[2]].concat(tmp);
|
|
}
|
|
console.table(rotRows);
|
|
|
|
|
|
|
|
|