aoc-cl/src/main.lisp

91 lines
2.5 KiB
Common Lisp

(defpackage aoc
(:use :cl)
(:export
#:split-lines
#:test-input
#:submit-part1
#:submit-part2
#:clear-data-cache))
(in-package :aoc)
(defun load-ql-dependencies ()
())
(defparameter *aoc-url* "https://adventofcode.com")
(defparameter *cookie-jar*
(cl-cookie:make-cookie-jar
:cookies (list
(cl-cookie:make-cookie
:name "session"
:value (uiop:getenv "AOC_SESSION")
:origin-host "adventofcode.com"
:path "/"
:domain ".adventofcode.com"
:secure-p t))))
(defun split-lines (s)
(cl-ppcre:split "\\n" s))
(defun fetch-input-data (year day)
(let ((url (format nil "~A/~D/day/~D/input" *aoc-url* year day)))
(multiple-value-bind
(body)
(dex:get url :cookie-jar *cookie-jar*)
(split-lines body))))
(defun print-articles (body)
(let* ((dom (plump:parse body))
(articles (plump:get-elements-by-tag-name dom "article")))
(loop for article in articles
do (format t "~A~%~%" (plump:render-text article)))))
(defun submit-answer (year day level answer)
(let ((url (format nil "~A/~D/day/~D/answer" *aoc-url* year day)))
(multiple-value-bind
(body)
(dex:post url :cookie-jar *cookie-jar*
:content `(("level" . ,level)
("answer" . ,answer)
("submit" . "[Submit]")))
(print-articles body))))
(defun submit-part1 (year day answer)
(submit-answer year day "1" answer))
(defun submit-part2 (year day answer)
(submit-answer year day "2" answer))
(defun show-articles (path)
(let ((url (format nil "~A/~A" *aoc-url* path)))
(multiple-value-bind
(body)
(dex:get url :cookie-jar *cookie-jar*)
(print-articles body))))
(defun private-leaderboard (year id)
(let ((path (format nil "~D/leaderboard/private/view/~D" year id)))
(show-articles path)))
(defvar *input-data-cache* (make-hash-table))
(defun clear-data-cache ()
(setf *input-data-cache* (make-hash-table)))
(defun test-input (year day)
"Return input data for the given challenge. Use a cached value if already fetched"
(let* ((key (+ (* year 100) day))
(val (gethash key *input-data-cache*)))
(if val
val
(setf (gethash key *input-data-cache*) (fetch-input-data year day)))))
(defun print-thread-info ()
(let* ((curr-thread sb-thread:*current-thread*)
(curr-thread-name (sb-thread:thread-name curr-thread))
(all-threads (sb-thread:list-all-threads)))
(format t "Current thread: ~a~%~%" curr-thread)
(format t "Current thread name: ~a~%~%" curr-thread-name)
(format t "All threads:~% ~{~a~%~}~%" all-threads))
nil)