This repository was archived by the owner on Apr 23, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.ts
More file actions
156 lines (126 loc) Β· 4.55 KB
/
run.ts
File metadata and controls
156 lines (126 loc) Β· 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import fs from "fs";
import path from "path";
import dotenv from "dotenv";
import fetch from "node-fetch";
import { JSDOM } from "jsdom";
import readline from "readline";
dotenv.config();
const [, , yearArg, dayArg] = process.argv;
if (!yearArg || !dayArg) {
console.error("Usage: ts-node run.ts <year> <day>");
process.exit(1);
}
const year = yearArg;
const day = dayArg.padStart(2, "0");
const dayPath = path.join(year, `day${day}`);
const inputPath = path.join(dayPath, "input.txt");
if (!fs.existsSync(inputPath)) {
console.error(`input.txt not found for ${year}/day${day}`);
process.exit(1);
}
const input = fs.readFileSync(inputPath, "utf-8");
const metaPath = path.join(dayPath, "meta.json");
let meta: { part1Submitted: boolean, part2Submitted: boolean } = {
part1Submitted: false,
part2Submitted: false
};
if (fs.existsSync(metaPath)) {
meta = JSON.parse(fs.readFileSync(metaPath, "utf-8"));
}
function saveMeta() {
fs.writeFileSync(metaPath, JSON.stringify(meta, null, 2));
}
function ask(prompt: string): Promise<string> {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
return new Promise(resolve => {
rl.question(prompt, answer => {
rl.close();
resolve(answer.trim().toLowerCase());
});
});
}
async function submitAnswer(year: string, day: string, level: "1" | "2", answer: string): Promise<boolean> {
const session = process.env.AOC_SESSION;
if (!session) {
console.error("SESSION token missing in .env");
return false;
}
const res = await fetch(`https://adventofcode.com/${year}/day/${Number(day)}/answer`, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Cookie": `session=${session}`,
},
body: `level=${level}&answer=${encodeURIComponent(answer)}`
});
const text = await res.text();
if (text.includes("That's the right answer")) {
console.log(`Correct answer for part ${level}!`);
if (level === "1") {
// Fetch Part 2
const puzzleRes = await fetch(`https://adventofcode.com/${year}/day/${Number(day)}`, {
headers: {
"Cookie": `session=${session}`,
}
});
const html = await puzzleRes.text();
const dom = new JSDOM(html);
const articles = dom.window.document.querySelectorAll("article");
const fullPuzzle = Array.from(articles)
.map(a => a.textContent?.trim() || "")
.join("\n\n");
const readmePath = path.join(dayPath, "README.md");
fs.writeFileSync(readmePath, fullPuzzle);
console.log("README.md updated with part 2.");
}
return true;
} else if (text.includes("That's not the right answer")) {
console.log("β Incorrect answer.");
} else if (text.includes("You gave an answer too recently")) {
console.log("β³ Rate limited. Wait before submitting again.");
} else {
console.log("β Unexpected response:");
console.log(text.slice(0, 300));
}
return false;
}
const runPart = async (part: "part1" | "part2") => {
const level = part === "part1" ? "1" : "2";
const submitted = meta[`${part}Submitted` as keyof typeof meta];
const modulePath = path.resolve(`${dayPath}/${part}.ts`);
const partModule = await import(modulePath);
const result = partModule.default(input);
console.log(`Output from ${part}:`, result);
if (submitted) {
console.log(`${part} already submitted. Skipping submission prompt.`);
if (part === "part1") {
const proceed = await ask("Run part2? (Y/n): ");
if (proceed !== "n") {
await runPart("part2");
}
}
return;
} else {
const submit = await ask(`Submit ${part}? (y/N): `);
if (submit === "y") {
const correct = await submitAnswer(year, day, level as "1" | "2", result.toString());
if (correct) {
meta[`${part}Submitted` as keyof typeof meta] = true;
saveMeta();
if (part === "part1") {
const proceed = await ask("Proceed to part2? (Y/n): ");
if (proceed !== "n") {
await runPart("part2");
}
}
}
}
}
};
(async () => {
console.log(`π AoC ${year} Day ${day}`);
await runPart("part1");
})();