add more script goodness

This commit is contained in:
xenia 2020-12-01 18:14:54 -05:00
parent 8925617afa
commit ac90336f64
4 changed files with 73 additions and 8 deletions

59
scripts/aoc-lib.rkt Normal file
View File

@ -0,0 +1,59 @@
#lang racket
(require net/uri-codec net/http-client)
(provide aoc-fetch-input aoc-submit-answer)
(define *host* "adventofcode.com")
(define/contract (puzzle-path year day endpoint)
(-> string? string? (or/c "input" "answer") path?)
(build-path "/" year "day" day endpoint))
(define (make-headers session)
(list (string-append "Cookie: session=" session)))
(define (aoc-request year day endpoint session [method 'GET] [data #f])
(define (parse-headers hlist)
(for/list ([h (in-list hlist)])
(match h
[(pregexp #px"^([^:]+): (.*?)$" (list _ k v))
(cons (string->symbol (string-downcase (bytes->string/utf-8 k)))
(bytes->string/utf-8 v))]
[x (cons 'unknown x)])))
(define (do-request path headers)
(define-values [status headers-out content]
(http-sendrecv *host* path #:ssl? #t #:headers headers))
(define headers-out/parsed (parse-headers headers-out))
(match status
[(pregexp #px"^HTTP/1\\.[10] 200") content]
[(pregexp #px"^HTTP/1\\.[10] 302")
(define location (cdr (or (assoc 'location headers-out/parsed)
(error "got 302 with no location"))))
(printf "got redirect to ~a\n" location)
(close-input-port content)
(do-request location headers)]
[(pregexp #px"^HTTP/1\\.[10] 404")
(error "endpoint returned 404\n response: " (port->bytes content))]
[stat
(error "endpoint returned unexpected data\n status: " stat "\n response: "
(port->bytes content))]))
(do-request (path->string (puzzle-path year day endpoint)) (make-headers session)))
(define/contract (aoc-fetch-input year day session)
(-> string? string? string? input-port?)
(aoc-request year day "input" session))
(define/contract (aoc-submit-answer year day session part answer)
(-> string? string? string? (or/c 1 2 "1" "2") string? (or/c symbol? bytes?))
(define data `((level . ,(~a part))
(answer . ,answer)))
(define resp
(port->bytes (aoc-request year day "answer" session 'POST (alist->form-urlencoded data))))
(match resp
[(pregexp #px"Both parts of this puzzle are complete") 'day-complete]
[(pregexp #px"That's the right answer") 'answer-correct]
[(pregexp #px"That's not the right answer") 'answer-incorrect]
[x x]))

View File

@ -1,14 +1,10 @@
#!/usr/bin/env racket
#lang racket
(require net/http-client)
(define *year* "2020")
(require "aoc-lib.rkt")
(command-line
#:program "get-input"
#:args (day)
(define-values [status headers content]
(http-sendrecv "adventofcode.com" (format "/~a/day/~a/input" *year* day)
#:ssl? #t
#:headers (list (format "Cookie: session=~a" (getenv "AOC_SESSION")))))
(call-with-output-file (build-path "inputs" day) (lambda (out) (copy-port content out))))
(define in (aoc-fetch-input (getenv "AOC_YEAR") day (getenv "AOC_SESSION")))
(call-with-output-file (build-path "inputs" day) (lambda (out) (copy-port in out))))

View File

@ -19,7 +19,7 @@
(command-line
#:program "get-input"
#:program "make-day"
#:args (day)
(call-with-output-file
(format "~a.rkt" day)

10
scripts/submit-answer Executable file
View File

@ -0,0 +1,10 @@
#!/usr/bin/env racket
#lang racket
(require "aoc-lib.rkt")
(command-line
#:program "submit-answer"
#:args (day part answer)
(define resp (aoc-submit-answer (getenv "AOC_YEAR") day (getenv "AOC_SESSION") part answer))
(printf "server returned: ~a\n" resp))