aoc2020/scripts/aoc.rkt

47 lines
1.5 KiB
Racket

#lang curly-fn racket
;; utilities for every challenge script
(require "aoc-lib.rkt" threading syntax/parse/define (for-syntax racket/syntax) graph)
(provide answer dbg memoize define/memoized
band bor bxor bnot bshl bshr bset? bfield
(all-from-out threading syntax/parse/define graph)
(for-syntax (all-from-out racket/syntax)))
;; in-expression debug print, uwu
(define (dbg x)
(pretty-write x)
x)
(define band bitwise-and)
(define bor bitwise-ior)
(define bxor bitwise-xor)
(define bnot bitwise-not)
(define bshl arithmetic-shift)
(define bshr #{arithmetic-shift %1 (- %2)})
(define bset? bitwise-bit-set?)
(define bfield bitwise-bit-field)
;; makes a memoization wrapper around a function
(define (memoize func)
(define memo (make-hash))
(lambda (arg)
(hash-ref! memo arg (λ () (func arg)))))
(define-simple-macro (define/memoized (name:id arg:id) body:expr ...+)
(define name (memoize (λ (arg) body ...))))
;; submit a solution to the server if not already submitted
(define (answer day part answer)
(printf "answer ~a.~a: ~s\n" day part answer)
(unless (aoc-complete? day part)
(printf "submit? [Y/n]: ")
(match (string-downcase (string-trim (read-line)))
[(or "" "y" "yes")
(printf "submitting...\n")
(define resp
(aoc-submit-answer (getenv "AOC_YEAR") (~a day) (getenv "AOC_SESSION") (~a part)
(~a answer)))
(printf "server responded: ~a\n" resp)]
[_ (printf "not submitting\n")])))