Skip to content

Instantly share code, notes, and snippets.

@LdBeth
Created December 15, 2022 08:11
Show Gist options
  • Save LdBeth/332395b13287bb1acb6728411e686e9b to your computer and use it in GitHub Desktop.
Save LdBeth/332395b13287bb1acb6728411e686e9b to your computer and use it in GitHub Desktop.
aoc2022d15
(ql:quickload :fset)
(ql:quickload :cl-interval)
(load "data.lisp")
(defparameter *map*
(reduce #'fset:with `(,(fset:empty-set) ,@*ff*)))
(defparameter *test1*
(fset:set '(2 18)
'(-2 15)
'(9 16)
'(10 16)
'(13 2)
'(15 3)
'(12 14)
'(10 16)
'(10 20)
'(10 16)
'(14 17)
'(10 16)
'(8 7)
'(2 10)
'(2 0)
'(2 10)
'(0 11)
'(2 10)
'(20 14)
'(25 17)
'(17 20)
'(21 22)
'(16 7)
'(15 3)
'(14 3)
'(15 3)
'(20 1)
'(15 3)))
(defparameter *test2* '((2 18 7)
(9 16 1)
(13 2 3)
(12 14 4)
(10 20 4)
(14 17 5)
(8 7 9)
(2 0 10)
(0 11 3)
(20 14 8)
(17 20 6)
(16 7 5)
(14 3 1)
(20 1 7)))
(defparameter *line* 10)
(defparameter *line* 2000000)
(defun zip (a b m)
(format t "zip!~%")
(if (and (<= (- b m) *line*) (<= *line* (+ b m)))
(let ((s (fset:empty-set))
(j (- *line* b)))
(loop for i from (- (- m (abs j))) to (- m (abs j))
do (fset:adjoinf s (list (+ a i) *line*))
)
s)
(fset:empty-set)))
(defun zip-count (a b m)
(let ((s 0))
(loop for i from (- m) to m
do (loop for j from (- (- m (abs i))) to (- m (abs i))
do (incf s)
))
s))
(defun part1 (data set)
(let ((sets (reduce #'fset:union (mapcar (lambda (l) (apply #' zip l)) data))))
(fset:set-difference sets set)))
(defvar *min*)
(defvar *max*)
(defvar *sum*)
(defun zip-mode (a b m)
(if (and (<= (- b m) *line*) (<= *line* (+ b m)))
(let ((j (- *line* b)))
(cons (+ a (max (- a) (- (- m (abs j))))) (+ a (min (- m (abs j)) (- 4000000 a)))))
nil))
(defun set-abnormal (set)
(format t "good:~A~%" *line*)
(let ((min 5000000)
(max -5000000)
(sum 0))
(fset:do-set (e set)
(if (> e max)
(setf max e))
(if (< e min)
(setf min e))
(incf sum e))
(let ((b (<= (fset:size set) (- max min))))
(values b
(if b (- (/ (* (1+ (fset:size set)) (+ max min)) 2) sum))))))
(defun bad-set (lst)
(let ((s (remove nil lst))
(max 0)
(tree (interval:make-tree)))
(loop for p in s
do (progn (interval:insert tree p)
(setf max (max max (cdr p)))))
(loop for p in s
do (if (and (< (1+ (cdr p)) max) (not (interval:find-all tree (1+ (cdr p)))))
(return-from bad-set (1+ (cdr p)))))))
(defun part2 (data)
(catch 'foo
(loop for *line* from 0 to 4000000
do (let ((l (mapcar (lambda (l) (apply #' zip-mode l)) data)))
(let ((r (bad-set l)))
(if r
(throw 'foo (+ (* 4000000 r) *line*))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment