Finish the stupid goddamn pots
author |
Steve Losh <steve@stevelosh.com> |
date |
Sun, 16 Dec 2018 19:28:52 -0500 |
parents |
a19c9e1fd077 |
children |
5b5c61ad8d2b |
(defpackage :advent/2018/08 #.cl-user::*advent-use*)
(in-package :advent/2018/08)
(named-readtables:in-readtable :interpol-syntax)
(defstruct (node (:conc-name nil))
children metadata)
(defun read-node (stream)
(let ((children-count (read stream))
(metadata-count (read stream)))
(make-node :children (iterate
(repeat children-count)
(collect (read-node stream) :result-type vector))
:metadata (iterate
(repeat metadata-count)
(collect (read stream))))))
(defun node-value (node &aux (children (children node)))
(if (emptyp children)
(summation (metadata node))
(iterate
(for meta :in (metadata node))
(for index = (1- meta))
(when (array-in-bounds-p children index)
(summing (node-value (aref children index)))))))
(define-problem (2018 8) (data)
(let ((root (read-node data)))
(values
(recursively ((node root))
(+ (summation (metadata node))
(summation (children node) :key #'recur)))
(node-value root))))
(1am:test test-2018/08
(multiple-value-bind (part1 part2) (run)
(1am:is (= 37905 part1))
(1am:is (= 33891 part2))))