From edf043f825a55702e07c123c4e7f721c805c93a8 Mon Sep 17 00:00:00 2001 From: 4903000 <4903000@chiru.no> Date: Tue, 8 Dec 2020 00:09:12 +0000 Subject: [PATCH] squash! Day 7 --- aoc20.lisp | 55 +++++++++++++++++++++++++++--------------------------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/aoc20.lisp b/aoc20.lisp index bf3cc9d..5c9494f 100644 --- a/aoc20.lisp +++ b/aoc20.lisp @@ -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)))) + (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) + (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 solve-7-1 (data target) + (length (remove-if #'null + (map 'list (lambda (sym) + (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) + (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))) + (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))))))