2021-12-30 16:31:29 +01:00
|
|
|
import
|
2022-01-05 09:09:10 +01:00
|
|
|
std/httpclient,
|
|
|
|
std/os,
|
|
|
|
std/strformat,
|
|
|
|
std/strutils
|
2021-12-30 16:31:29 +01:00
|
|
|
|
|
|
|
proc getInput*(year, day: int): string =
|
|
|
|
try:
|
|
|
|
let input = readFile(getCurrentDir() / "input.txt")
|
|
|
|
|
2022-01-02 10:50:00 +01:00
|
|
|
return input.strip(chars = {'\n'})
|
2021-12-30 16:31:29 +01:00
|
|
|
except IOError:
|
2022-01-01 13:56:01 +01:00
|
|
|
echo "adventofcode> Missing input.txt. Downloading..."
|
|
|
|
let session = os.getEnv("SESSION")
|
|
|
|
if session == "":
|
|
|
|
echo "adventofcode> Unable to download without the SESSION environment variable."
|
|
|
|
quit 1
|
|
|
|
|
2021-12-30 16:31:29 +01:00
|
|
|
let client = newHttpClient()
|
|
|
|
client.headers = newHttpHeaders({ "Cookie": fmt"session={session}" })
|
|
|
|
|
|
|
|
let input = client.getContent fmt"https://adventofcode.com/{year}/day/{day}/input"
|
|
|
|
|
|
|
|
writeFile(getCurrentDir() / "input.txt", input)
|
|
|
|
|
2022-01-02 10:50:00 +01:00
|
|
|
return input.strip(chars = {'\n'})
|