>>107152351
i didnt swap anything in part 2
function two() {
const lines = await readLines("two.txt");
const names = lines[0]?.split(",") ?? [];
const actions = lines[1]?.split(",") ?? [];
let index = 0;
const moveLeft = (index: number, length_of_list: number) => {
if (index === 0) {
index = length_of_list - 1;
} else {
index--;
}
return index;
};
const moveRight = (index: number, length_of_list: number) => {
if (index === length_of_list - 1) {
index = 0;
} else {
index++;
}
return index;
};
for (const e of actions) {
if (e[0] === "L") {
let moveTimes = parseInt(e.slice(1), 10);
for (let i = 0; i < moveTimes; i++) {
index = moveLeft(index, names.length);
}
} else if (e[0] === "R") {
let moveTimes = parseInt(e.slice(1), 10);
for (let i = 0; i < moveTimes; i++) {
index = moveRight(index, names.length);
}
}
}
console.log("Name: ", names[index]);
}