aoc-2023/day_14/solve.ts
2024-12-09 22:41:02 +09:00

38 lines
1.0 KiB
TypeScript

const input = await Deno.readTextFile("example.txt");
const lines = input.split("\n").map(x => x.trim()).filter(x => x.length > 0).map(x=>x.split(""));
const width = lines[0].length;
const height = lines.length;
for (let i = 0; i < width; i++) {
let place = 0;
for (let j = 0; j < height; j++) {
const ch = lines[j][i];
if(ch === "O"){
//swap
const tmp = lines[place][i];
lines[place][i] = ch;
lines[j][i] = tmp;
place++;
}
else if(ch === "."){
// do nothing
}
else if(ch === "#"){
place = j + 1;
}
else{
throw new Error(`unknown char ${ch}`);
}
}
}
await Deno.writeTextFile("tf.txt", lines.map(x=> x.join("")).join("\n"));
const answer = lines.map((x, i) => {
// count O
const count = x.map(y=> y === "O" ? 1 : 0 as number).reduce((acc, x) => acc + x, 0);
return (lines.length-i) * count;
}).reduce((a, b) => a + b, 0);
console.log(answer);