;; https://adventofcode.com/2022/day/13 ;; (defparameter *day13-file-name* "day13-test.txt") (defparameter *day13-file-name* "day13-input.txt") (defparameter *day13-all-list* nil) ;; 5739 is too low & incorrect ;; 6398 too high (defun nest-2-< (left right) (cond ((and (numberp left) (numberp right)) (- left right)) ((numberp left) (nest-2-< (list left) right)) ((numberp right) (nest-2-< left (list right))) ((and (listp left) (listp right)) (cond ((and (not left) (not right)) 0) ; both equal and empty ((not left) -1) ; only left empty, left is smaller ((not right) 1) ; only right is empty, left is bigger (t (if (equal (first left) (first right)) (nest-2-< (rest left) (rest right)) (let ((head-comparison (nest-2-< (first left) (first right)))) (if (= 0 head-comparison) (nest-2-< (rest left) (rest right)) head-comparison)))))) (t 'default))) (progn (defparameter *day13-all-list* nil) (setq *day13-all-list* (with-open-file (in *day13-file-name*) (read in))) (defparameter *day13-lefts* nil) (defparameter *day13-rights* nil) (loop for i from 0 below (length *day13-all-list*) when (= 0 (mod i 2)) do (push (nth i *day13-all-list*) *day13-lefts*) when (= 1 (mod i 2)) do (push (nth i *day13-all-list*) *day13-rights*)) (setq *day13-lefts* (reverse *day13-lefts*)) (setq *day13-rights* (reverse *day13-rights*)) (defparameter *day13-indices* nil) (setq *day13-indices* (loop for i from 0 below (length *day13-lefts*) when (< (nest-2-< (nth i *day13-lefts*) (nth i *day13-rights*)) 0) collect (1+ i))) (apply #'+ *day13-indices*)) ;;; PART 2 (let ((sorted (sort (copy-list *day13-all-list*) (lambda (left right) (< (nest-2-< left right) 0))))) (* (1+ (position '((2)) sorted :test #'equal)) (1+ (position '((6)) sorted :test #'equal)))) ;; 24477 yay