advent-of-code-2022/download-input.py

173 lines
3.8 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/python3
2022-12-05 17:03:16 +00:00
from abc import ABC, abstractmethod
import argparse
2022-12-05 17:03:16 +00:00
from os import getenv
from os.path import exists, join
import shutil
2022-12-05 17:03:16 +00:00
from tempfile import TemporaryFile
from urllib.request import Request, urlopen
import webbrowser
2022-12-05 17:03:16 +00:00
class Language(ABC):
2022-12-05 17:24:33 +00:00
input_path: str
2022-12-05 17:03:16 +00:00
src_path: str
2022-12-05 17:24:33 +00:00
@abstractmethod
def src_file_name(self, day):
pass
2022-12-05 17:03:16 +00:00
@abstractmethod
def format_src(self, day):
pass
class Kotlin(Language):
src_path = 'kotlin/src'
2022-12-05 17:24:33 +00:00
input_path = src_path
def src_file_name(self, day):
return "Day{}.kt".format(day)
2022-12-05 17:03:16 +00:00
def format_src(self, day):
return """fun main() {{
fun part1(input: List<String>): Int {{
return 0
}}
fun part2(input: List<String>): Int {{
return 0
}}
val testInput = readInput("Day{day}_test")
check(part1(testInput) == 0)
check(part2(testInput) == 0)
val input = readInput("Day{day}")
println(part1(input))
println(part2(input))
2022-12-05 17:03:16 +00:00
}}
""".format(day=day)
2022-12-07 04:59:46 +00:00
class Python(Language):
input_path = 'python'
src_path = 'python'
def src_file_name(self, day):
return "day{}.py".format(day)
def format_src(self, day):
return """#!/usr/bin/env python3
from util import read_input
def part1(input):
pass
def part2(input):
pass
test_input = read_input("Day{day}_test")
assert 0 == part1(test_input)
assert 0 == part2(test_input)
real_input = read_input('Day{day}')
print(part1(real_input))
print(part2(real_input))
""".format(day=day)
2022-12-05 17:24:33 +00:00
class Rust(Language):
input_path = 'rust'
src_path = 'rust/src/bin'
def src_file_name(self, day):
return "day{}.rs".format(day)
def format_src(self, day):
return """use rust::util::read_input;
fn part1(input: &Vec<String>) -> u32 {{
0
}}
fn part2(input: &Vec<String>) -> u32 {{
0
}}
fn main() {{
let test_input = read_input("Day{day}_test").expect("Failed to open test input");
assert_eq!(0, part1(&test_input));
assert_eq!(0, part2(&test_input));
let input = read_input("Day{day}").expect("Failed to open input");
2022-12-05 17:52:48 +00:00
println!("{{:?}}", part1(&input));
println!("{{:?}}", part2(&input));
2022-12-05 17:24:33 +00:00
}}
""".format(day=day)
2022-12-05 17:03:16 +00:00
session_cookie = getenv('AOC_SESSION')
if not session_cookie:
print('AOC_SESSION environment variable not set, aborting')
exit(1)
if not session_cookie.startswith('session='):
session_cookie = 'session=' + session_cookie
parser = argparse.ArgumentParser()
2022-12-05 17:03:16 +00:00
parser.add_argument('-d', '--day', type=int, required=True)
parser.add_argument('--kotlin', action='store_true')
2022-12-07 04:59:46 +00:00
parser.add_argument('--python', action='store_true')
2022-12-05 17:24:33 +00:00
parser.add_argument('--rust', action='store_true')
args = parser.parse_args()
challenge_url = "https://adventofcode.com/2022/day/{}".format(args.day)
input_url = "{}/input".format(challenge_url)
webbrowser.open(challenge_url)
2022-12-05 17:03:16 +00:00
languages = []
2022-12-05 17:03:16 +00:00
if args.kotlin:
languages.append(Kotlin())
2022-12-07 04:59:46 +00:00
if args.python:
languages.append(Python())
2022-12-05 17:24:33 +00:00
if args.rust:
languages.append(Rust())
2022-12-05 17:03:16 +00:00
if len(languages) == 0:
print("error: at least one language is required")
parser.print_usage()
exit(1)
2022-12-05 17:24:33 +00:00
daystr = str(args.day).zfill(2)
day_file_base = 'Day{}'.format(daystr)
2022-12-05 17:03:16 +00:00
day_file_test = '{}_test.txt'.format(day_file_base)
request = Request(input_url, headers={'Cookie': session_cookie})
2022-12-05 17:03:16 +00:00
tmp_file = TemporaryFile()
with urlopen(request) as response:
2022-12-05 17:03:16 +00:00
shutil.copyfileobj(response, tmp_file)
for language in languages:
tmp_file.seek(0)
with open(join(language.input_path, day_file_test), 'a'):
2022-12-05 17:03:16 +00:00
pass
2022-12-05 17:24:33 +00:00
with open(join(language.input_path, '{}.txt'.format(day_file_base)), 'wb') as input_file:
2022-12-05 17:03:16 +00:00
shutil.copyfileobj(tmp_file, input_file)
2022-12-05 17:24:33 +00:00
src_file = join(language.src_path, language.src_file_name(daystr))
2022-12-05 17:03:16 +00:00
if not exists(src_file):
with open(src_file, 'w') as src:
src.write(language.format_src(str(args.day).zfill(2)))
2022-12-05 18:04:44 +00:00
tmp_file.close()