squash! Day 7

master
4903000 4 years ago
parent 1b3ba7cbcb
commit edf043f825

@ -267,48 +267,47 @@
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))))
(copy-seq data)))
(defun recursive-search (node symbol)
(cond ((eq node nil)
nil)
((member symbol (map 'list #'car node))
t)
(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
(defun solve-7-1 (data target)
(length (remove-if #'null
(map 'list (lambda (sym)
(recursive-search (eval sym) '|shiny gold|))
(get-bag-symbols data)))))
(recursive-search (eval sym) target))
(map 'list (lambda (entry) (intern (first entry))) 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)
((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)))
(t (+ (* bag-multiplier-up (count-bags bags-up (if (null (eval bags-up)) t)))
(count-bags bags-right))))))
(defun solve-7-2 (node)
(1- (count-bags (list (cons node 1)))))
(defun solve-7 (file target-bag)
(let ((data (read-7 file))
(symbol (intern target-bag)))
(time (progn (build-graph data)
(print (solve-7-1 data symbol))
(print (solve-7-2 symbol))))))

Loading…
Cancel
Save