-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.rkt
33 lines (28 loc) · 1.02 KB
/
input.rkt
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
#lang racket
(require net/url)
(provide input)
(define (input day)
(let ([filename (~a "inputs/" day)])
(if (file-exists? filename)
(file->string filename)
(let ([input (download day)])
(unless (directory-exists? (path-only filename))
(make-directory (path-only filename)))
(call-with-output-file filename (curry display input))
input))))
(define (session-cookie)
(let ([filename "session.cookie"])
(if (file-exists? filename)
(file->string filename)
(error "Missing `session.cookie`."))))
(define (download day)
(let*-values ([(port headers)
(get-pure-port/headers
(string->url(~a "https://adventofcode.com/2024/day/" day "/input"))
(list (~a "cookie: " (session-cookie)))
#:status? #t)]
[(str) (port->string port)])
(close-input-port port)
(unless (string-contains? headers "200 OK")
(error (~a "Error while downloading input " day ": " str)))
str))