-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetinput.py
executable file
·171 lines (118 loc) · 4.06 KB
/
getinput.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python3
import argparse
import requests
import os, os.path
import textwrap
from subprocess import call
def main(args):
destination_parts = [
args.aoc_path,
str(args.year),
f"{args.day:02}"
]
destination = "/".join(destination_parts)
print(f"Ensuring destination exists: {destination}")
for i in range(len(destination_parts)):
path = "/".join(destination_parts[0:i + 1])
if not os.path.exists(path):
os.mkdir(path, mode=0o755)
session = requests.Session()
requests.utils.add_dict_to_cookiejar(session.cookies, dict(session=args.session_token))
event_url = f"https://adventofcode.com/{args.year}/day/{args.day}"
print(f"Getting todays puzzle input")
r = session.get(f"{event_url}/input", stream=True)
r.raise_for_status()
print(f"Saving to {destination}/input.aoc")
with open(f"{destination}/input.aoc", "wb") as f:
for content in r.iter_content():
f.write(content)
if args.open:
os.system(f"open {event_url}")
filename = "puzzle.{ext}".format(ext=LANG_CONFIG[args.lang]["ext"])
with open(f"{destination}/{filename}", "w") as f:
f.write(textwrap.dedent(LANG_CONFIG[args.lang]["template"]))
os.system(f"chmod +x {destination}/{filename}")
os.chdir(destination)
call(["vim", filename])
def parse_args():
parser = argparse.ArgumentParser(description="Utility to get input from Advent of Code")
parser.add_argument("--aoc-path", help="The path to Advent of Code")
parser.add_argument("--session-token", help="Session token override.")
parser.add_argument("--day", "-d", type=int, help="Which day to get the input for, defaults to 'today'")
parser.add_argument("--year", "-y", type=int, help="Which year to get the input for, defaults to 'this year'")
parser.add_argument("--open", "-o", action="store_true", help="Whether or not to open the URL")
parser.add_argument("--lang", "-l", choices=["python","typescript"], default="python", help="What language to use for the template")
args = parser.parse_args()
fill_in_default_args(args)
return args
def fill_in_default_args(args):
from datetime import datetime
today = datetime.today()
if not args.year:
args.year = today.year
if not args.day:
args.day = today.day
if not args.session_token:
with open(os.path.expanduser("~/.secrets")) as f:
for line in f.readlines():
parts = line.strip().split("=")
key = parts[0]
val = "=".join(parts[1:])
if key == "ADVENT_OF_CODE_SESSION_TOKEN":
args.session_token = val
if not args.aoc_path:
args.aoc_path = "~/Projects/advent-of-code-solutions"
args.aoc_path = os.path.expanduser(args.aoc_path)
TYPESCRIPT_TEMPLATE = """\
#!bun
import * as fs from "fs";
const USE_SAMPLE_DATA = process.argv[2] === "sample";
async function main(args) {
console.log(`Part One: ${solvePuzzle(args.data)}`);
console.log(`Part Two: ${solvePuzzle(args.data, true)}`);
}
function solvePuzzle(data, partTwo = false) {
if (partTwo) { return ; }
console.log(data);
}
async function loadPuzzle() {
const sampleData = `
> DATA HERE <
`;
const data = (
USE_SAMPLE_DATA ?
sampleData :
fs.readFileSync(`${__dirname}/input.aoc`, "utf8")
)
.trim()
.split("\\n")
.map(s => s.trim());
const processed = data;
return processed;
}
await main({ data: await loadPuzzle() })"""
PYTHON_TEMPLATE = """\
#!/usr/bin/env python3
def main():
data = parse_input()
print(data)
def parse_input():
with open("input.aoc") as f:
data = [x.strip() for x in f.readlines()]
# process data
processed = data
return processed
if __name__ == "__main__":
main()"""
LANG_CONFIG = {
"python": {
"template": PYTHON_TEMPLATE,
"ext": "py",
},
"typescript": {
"template": TYPESCRIPT_TEMPLATE,
"ext": "ts",
},
}
if __name__ == "__main__":
main(parse_args())