-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreamble.jl
More file actions
52 lines (47 loc) · 1.95 KB
/
Copy pathpreamble.jl
File metadata and controls
52 lines (47 loc) · 1.95 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
import HTTP
if abspath(PROGRAM_FILE) == @__FILE__
print("""
This script downloads your input text from adventofcode.com.
The day # is extracted from the name of the program file.
The year # is extracted from the name of the program file's base dir.
Your input will be saved as input<day>.txt.
This script reads your Advent of Code session ID from a file named `cookie.txt`
located in the repository root (or parent directory). Keep `cookie.txt`
private and do not commit it to a public repo.
If you use a repository-level Julia project (a `Project.toml` + `Manifest.toml`
at the repo root), run day scripts with the `--project` flag pointing at the
repository root so dependencies are found. Example (from the repo root):
`julia --project=. 2025/d01.jl` or when inside a year's directory:
`julia --project=.. d01.jl`.
Be careful to not push the session cookie file to a public repository.
""")
else
global progdir = dirname(abspath(joinpath(".", PROGRAM_FILE)))
local year = splitpath(progdir)[end]
local sday = match(r"\d+", basename(PROGRAM_FILE)).match
local iday = parse(Int, sday)
local fname = joinpath(progdir, "input$(sday).txt")
local function _find_cookie(startdir)
p = startdir
while true
f = joinpath(p, "cookie.txt")
if isfile(f)
return strip(read(f, String))
end
parent = dirname(p)
if parent == p
break
end
p = parent
end
return nothing
end
session = begin
s = _find_cookie(progdir)
s === nothing && error("cookie.txt not found: create a file named `cookie.txt` containing your session ID in the repository root or a parent directory.")
s
end
isfile(fname) || open(fname, "w") do f
write(f, HTTP.get("https://adventofcode.com/$year/day/$iday/input"; cookies=Dict("session" => session)).body |> String)
end
end