本帖最后由 TSCAN 于 2017-7-20 10:16 编辑
- #lang racket
- (require net/http-client json)
- (define (reg username)
- (define-values
- (a b c)
- (http-sendrecv "bbaass.tk"
- "http://bbaass.tk/math/"
- #:method "POST"
- #:headers (list "Content-Type: application/x-www-form-urlencoded")
- #:data (string-append "send=reg&username=" username)))
- (port->string c))
-
- (define ((send-answer username) ans)
- (define-values
- (a b c)
- (http-sendrecv "bbaass.tk"
- "http://bbaass.tk/math/"
- #:method "POST"
- #:headers (list "Content-Type: application/x-www-form-urlencoded")
- #:data
- (string-append "send=Answer&username=" username "&"
- "math=" (number->string ans) "&")
- ))
- c
- )
-
-
-
- (define (try-once min max)
- (define (mid x y) (floor (/ (+ x y) 2)))
- (define first-time
- (read-json ((send-answer "racket")
- (mid min max)))
- )
- (define token (hash-ref first-time 'tokens))
- (define first-re (hash-ref first-time 're))
- (define (token-changed? t)
- (not (string=? token t)))
- (define (loop min max exit)
- (printf "min :~a,max :~a\n" min max)
- (define this-time (read-json ((send-answer "racket") (mid min max))))
- (define this-token (hash-ref this-time 'tokens))
- (define this-re (hash-ref this-time 're))
- (when (string=? this-re "=") (exit (printf "succeed.\n")))
- (when (token-changed? this-token) (exit (void)))
- (match this-re
- ["<" (loop (mid min max) max exit)]
- [">" (loop min (mid min max) exit)]))
- (call/ec
- (lambda (exit) (loop min max exit)))
- )
- (define (repl) (try-once 0 10000) (repl))
- (repl)
复制代码 racket代码,有点慢,注意先用reg函数注册。
更新的代码,速度更快:- #lang racket
- (require net/http-client json racket/unsafe/ops)
- (define (reg username)
- (define-values
- (a b c)
- (http-sendrecv "bbaass.tk"
- "http://bbaass.tk/math/"
- #:method "POST"
- #:headers (list "Content-Type: application/x-www-form-urlencoded")
- #:data (string-append "send=reg&username=" username)))
- (port->string c))
-
- (define-syntax-rule (send-answer connection ans)
- (let-values
- [[(a b c)
- (http-conn-sendrecv! connection
- "http://bbaass.tk/math/"
- #:method "POST"
- #:headers (list "Content-Type: application/x-www-form-urlencoded")
- #:data
- (string-append "send=Answer&username=racket&"
- "math=" (number->string ans) "&")
- )]]
- c
- ))
- ;;Update : No function call here
- (define-syntax-rule (mid x y) (if (= 1 (- y x))
- y (unsafe-fxquotient (unsafe-fx+ x y) 2)))
-
- (define (try-once)
- (define min 0)
- (define max 10000)
- (define next (mid min max))
- ;;Update : A bug has been fixed.
- (define http-connector (http-conn-open "bbaass.tk" #:auto-reconnect? #t))
- (define first-time
- (read-json (send-answer http-connector
- (mid min max)))
- )
- (define token (hash-ref first-time 'tokens))
- (define first-re (hash-ref first-time 're))
- (define (token-changed? t)
- (not (string=? token t)))
-
- (define (loop min max)
- (printf "min :~a,max :~a\n" min max)
- (define next (mid min max))
- (define this-time (read-json (send-answer http-connector next)))
- (define this-token (hash-ref this-time 'tokens))
- (define this-re (hash-ref this-time 're))
- (cond
- [(string=? this-re "=") (begin (printf "succeed.\n")
- (http-conn-close! http-connector)
- )]
- [(token-changed? this-token) (http-conn-close! http-connector)]
- [(string=? this-re "<") (loop next max)]
- [(string=? this-re ">") (loop min next)]
-
- ))
- ;;Update: Because of tail call optimization, we don't need call/cc or call/ec
-
- (match first-re
- ["<" (loop next max)]
- [">" (loop min next)]
- [_ (begin (printf "succeed.\n")
- (http-conn-close! http-connector)
- )]
- )
- )
-
- (define (repl) (try-once) (repl))
- (repl)
复制代码
|