squash! Day 7

master
4903000 4 years ago
parent 1b3ba7cbcb
commit edf043f825

@ -267,48 +267,47 @@
while line while line
collect (read-bag line)))) collect (read-bag line))))
(defun get-bag-symbols (data)
(map 'list (lambda (entry) (intern (first entry))) data))
(defun build-graph (data) (defun build-graph (data)
(let ((symbols (get-bag-symbols data)))
(map 'list (lambda (entry) (map 'list (lambda (entry)
(set (intern (first entry)) (set (intern (first entry))
(if (null (first (second entry))) (if (null (first (second entry)))
nil nil
(map 'list (lambda (r) (cons (intern (first r)) (second r))) (map 'list (lambda (r) (cons (intern (first r)) (second r)))
(second entry))))) (second entry)))))
(copy-seq data)))) (copy-seq data)))
(defun recursive-search (node symbol) (defun recursive-search (node symbol)
(cond ((eq node nil) (cond ((eq node nil) nil)
nil) ((member symbol (map 'list #'car node)) t)
((member symbol (map 'list #'car node))
t)
(t (or (recursive-search (eval (caar node)) symbol) (t (or (recursive-search (eval (caar node)) symbol)
(recursive-search (cdr node) symbol))))) (recursive-search (cdr node) symbol)))))
(defun solve-7-1 (data) (defun solve-7-1 (data target)
(length (length (remove-if #'null
(remove-if
#'null
(map 'list (lambda (sym) (map 'list (lambda (sym)
(recursive-search (eval sym) '|shiny gold|)) (recursive-search (eval sym) target))
(get-bag-symbols data))))) (map 'list (lambda (entry) (intern (first entry))) data)))))
(defun count-bags (node &optional cheat) (defun count-bags (node &optional cheat)
(let* ((node (if (symbolp node) (eval node) node)) (let* ((node (if (symbolp node) (eval node) node))
(bag-multiplier-up (or (cdar node) 0)) (bag-multiplier-up (or (cdar node) 0))
(bag-multiplier-right (or (cdadr node) 0))
(bags-up (caar node)) (bags-up (caar node))
(bags-right (cdr 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) (cond (cheat 1)
((null node) ((null node) 0)
0)
((not (null (eval (caar node)))) ((not (null (eval (caar node))))
(+ (* bag-multiplier-up (count-bags bags-up)) (+ (* bag-multiplier-up (count-bags bags-up))
(count-bags bags-right) (count-bags bags-right)
bag-multiplier-up)) 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)))))) (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