diff options
author | Oscar Najera <hi@oscarnajera.com> | 2022-12-04 22:50:54 +0100 |
---|---|---|
committer | Oscar Najera <hi@oscarnajera.com> | 2022-12-04 22:51:27 +0100 |
commit | afe994a44396c3a8e5ad29ccc3d7f21e85fecd14 (patch) | |
tree | 8abf88dcc5d87c36af46c096d1320db6adbe9108 /AoC2022 | |
parent | c91c280211b7706561d97c8131600398063c7527 (diff) | |
download | scratch-afe994a44396c3a8e5ad29ccc3d7f21e85fecd14.tar.gz scratch-afe994a44396c3a8e5ad29ccc3d7f21e85fecd14.tar.bz2 scratch-afe994a44396c3a8e5ad29ccc3d7f21e85fecd14.zip |
[AoC2022] Common Lisp 03
Diffstat (limited to 'AoC2022')
-rw-r--r-- | AoC2022/03/makefile | 2 | ||||
-rw-r--r-- | AoC2022/03/solver.lisp | 55 |
2 files changed, 56 insertions, 1 deletions
diff --git a/AoC2022/03/makefile b/AoC2022/03/makefile index f781686..630e630 100644 --- a/AoC2022/03/makefile +++ b/AoC2022/03/makefile @@ -10,5 +10,5 @@ run: emacs -batch -l ert -l solver.el -f ert-run-tests-batch-and-exit - # sbcl --load ~/.sbclrc --script solver.lisp + sbcl --load ~/.sbclrc --script solver.lisp rustc solver.rs && ./solver diff --git a/AoC2022/03/solver.lisp b/AoC2022/03/solver.lisp new file mode 100644 index 0000000..a221495 --- /dev/null +++ b/AoC2022/03/solver.lisp @@ -0,0 +1,55 @@ +(ql:quickload :fiveam) + +(defun recover-item (bitfield-int) + (loop for i from 0 + when (logbitp i bitfield-int) return i)) + +(defun priority (value) + (if (< value 31) ;; It is a capital letter in range [1;26] + (+ value 26) + (- value 32))) + +(defun line-priority (str) + (let ((mid (/ (length str) 2)) + (left-pack 0) (right-pack 0)) + (loop with cross = 0 + for l across (subseq str 0 mid) + for r across (subseq str mid) + do (progn + (setf left-pack (logior left-pack (ash 1 (- (char-code l) 64)))) + (setf right-pack (logior right-pack (ash 1 (- (char-code r) 64)))) + (setf cross (logand left-pack right-pack))) + when (< 0 cross) return (priority (recover-item cross))))) + + +(defun prio () + (with-open-file (in "input") + (loop :for l = (read-line in nil nil) + :while l + :sum (line-priority l)))) + +(defun pick-badge (str) + (loop with stack = 0 + for i across str + do (setf stack (logior stack (ash 1 (- (char-code i) 64)))) + finally (return stack))) + +(defun badge () + (with-open-file (in "input") + (loop :for b = (loop repeat 3 for l = (read-line in nil nil) while l collect (pick-badge l)) + :while b + :sum (priority (recover-item (apply #'logand b)))))) + +;; (pick-badge "LdHVLDLDdHdtLMhcqCqGWcWg" ) + +(fiveam:test intermediate + (fiveam:is (= 12 (recover-item 4096))) + (fiveam:is (= 20 (line-priority + "gzCjffWZCtCfZZVdqVSqJdvJndSt")))) + +(fiveam:test results + (fiveam:is (= 8072 (prio))) + (fiveam:is (= 2567 (badge)))) + + +(fiveam:run-all-tests) |