|
|
|
@ -248,3 +248,67 @@
|
|
|
|
|
(let ((data (read-6 file)))
|
|
|
|
|
(time (progn (print (solve-6-1 data))
|
|
|
|
|
(print (solve-6-2 data))))))
|
|
|
|
|
|
|
|
|
|
;;; day 7
|
|
|
|
|
|
|
|
|
|
(defun read-bag (line)
|
|
|
|
|
(let ((key-bound (search "bags" line)))
|
|
|
|
|
(list (subseq line 0 (1- key-bound))
|
|
|
|
|
(map 'list (lambda (r)
|
|
|
|
|
(multiple-value-bind (n index) (parse-integer r :junk-allowed t)
|
|
|
|
|
(unless (search "no other" r)
|
|
|
|
|
(list (subseq r (1+ index) (1- (search "bag" r))) n))))
|
|
|
|
|
(split-sequence:split-sequence
|
|
|
|
|
#\, (subseq line (+ 1 (length "bags contain") key-bound)))))))
|
|
|
|
|
|
|
|
|
|
(defun read-7 (file)
|
|
|
|
|
(with-open-file (stream file)
|
|
|
|
|
(loop for line = (read-line stream nil)
|
|
|
|
|
while line
|
|
|
|
|
collect (read-bag line))))
|
|
|
|
|
|
|
|
|
|
(defun get-bag-symbols (data)
|
|
|
|
|
(map 'list (lambda (entry) (intern (first entry))) data))
|
|
|
|
|
|
|
|
|
|
(defun build-graph (data)
|
|
|
|
|
(let ((symbols (get-bag-symbols data)))
|
|
|
|
|
(map 'list (lambda (entry)
|
|
|
|
|
(set (intern (first entry))
|
|
|
|
|
(if (null (first (second entry)))
|
|
|
|
|
nil
|
|
|
|
|
(map 'list (lambda (r) (cons (intern (first r)) (second r)))
|
|
|
|
|
(second entry)))))
|
|
|
|
|
(copy-seq data))))
|
|
|
|
|
|
|
|
|
|
(defun recursive-search (node symbol)
|
|
|
|
|
(cond ((eq node nil)
|
|
|
|
|
nil)
|
|
|
|
|
((member symbol (map 'list #'car node))
|
|
|
|
|
t)
|
|
|
|
|
(t (or (recursive-search (eval (caar node)) symbol)
|
|
|
|
|
(recursive-search (cdr node) symbol)))))
|
|
|
|
|
|
|
|
|
|
(defun solve-7-1 (data)
|
|
|
|
|
(length
|
|
|
|
|
(remove-if
|
|
|
|
|
#'null
|
|
|
|
|
(map 'list (lambda (sym)
|
|
|
|
|
(recursive-search (eval sym) '|shiny gold|))
|
|
|
|
|
(get-bag-symbols data)))))
|
|
|
|
|
|
|
|
|
|
(defun count-bags (node &optional cheat)
|
|
|
|
|
(let* ((node (if (symbolp node) (eval node) node))
|
|
|
|
|
(bag-multiplier-up (or (cdar node) 0))
|
|
|
|
|
(bag-multiplier-right (or (cdadr node) 0))
|
|
|
|
|
(bags-up (caar node))
|
|
|
|
|
(bags-right (cdr node)))
|
|
|
|
|
(format t "m-up: ~A m-right: ~A node: ~A~%" bag-multiplier-up bag-multiplier-right node)
|
|
|
|
|
(cond (cheat 1)
|
|
|
|
|
((null node)
|
|
|
|
|
0)
|
|
|
|
|
((not (null (eval (caar node))))
|
|
|
|
|
(+ (* bag-multiplier-up (count-bags bags-up))
|
|
|
|
|
(count-bags bags-right)
|
|
|
|
|
bag-multiplier-up))
|
|
|
|
|
(t (+ (* bag-multiplier-up (count-bags bags-up (if (null (eval (caar node))) t)))
|
|
|
|
|
(count-bags bags-right))))))
|
|
|
|
|