-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_day.py
84 lines (59 loc) · 1.5 KB
/
add_day.py
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
import os
import sys
import requests
PY_TEMPLATE = """with open('{}/input.txt', 'r') as f:
lines = f.readlines()
def part1():
...
def part2():
...
print("Part 1:", part1())
print("Part 2:", part2())
"""
CPP_TEMPLATE = """#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int part1() {
ifstream f("<DIRNAME>/input.txt");
string line;
while (getline(f, line)) {
// do something...
}
}
int part2() {
ifstream f("<DIRNAME>/input.txt");
string line;
while (getline(f, line)) {
// do something...
}
}
int main() {
cout << "Part 1: " << part1() << endl;
cout << "Part 2: " << part2() << endl;
}
"""
def download(day: str) -> bytes:
with open(".session", "r") as f:
session = f.read().strip()
cookies = {"session": session}
resp = requests.get(
f"https://adventofcode.com/2024/day/{day}/input", cookies=cookies)
resp.raise_for_status()
return resp.content
def setup(day: str) -> None:
puzzle_input = download(day)
dirname = "day{:02}".format(int(day))
os.mkdir(dirname)
os.chdir(dirname)
with open("solution.py", "w") as f:
f.write(PY_TEMPLATE.format(dirname))
with open("solution.cpp", "w") as f:
f.write(CPP_TEMPLATE.replace("<DIRNAME>", dirname))
with open("input.txt", "wb") as f:
f.write(puzzle_input) # type: ignore
with open("example.txt", "w") as _:
pass
day = sys.argv[-1]
setup(day)
print("Setup complete...")