-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanswer.rkt
43 lines (39 loc) · 1.44 KB
/
answer.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
34
35
36
37
38
39
40
41
42
43
#lang racket/base
(require racket/contract/base
net/uri-codec
racket/port
racket/format
advent-of-code/request
(only-in advent-of-code/input advent-day? advent-year?))
(provide (contract-out
[aoc-submit (-> aoc-session? advent-year? advent-day?
(or/c 1 2) any/c
string?)]
[aoc-submit* (-> aoc-session? advent-year? advent-day?
(or/c 1 2) any/c
input-port?)])
advent-day?
advent-year?)
(define (aoc-submit* session year day part answer)
(aoc-request session year "day" day "answer"
#:post
(lambda (hs)
(values
;; form-data would yield a "; charset=utf-8" at the end,
;; which the server doesn't accept
(hash-set hs 'content-type #"application/x-www-form-urlencoded")
(alist->form-urlencoded
`((level . ,(~a part))
(answer . ,(~a answer))))))))
(define (aoc-submit session year day part answer)
(define response (port->string (aoc-submit* session year day part answer)))
(define matches (regexp-match #px"<article><p>(.*)</p></article>" response))
(if matches
(regexp-replace*
#px"</?.+?>"
(regexp-replace*
#px" |</?br>"
(cadr matches)
"\n")
"")
#f))