src/2018/days/day-08.lisp @ 77fa6a483e0e
2021/04
author |
Steve Losh <steve@stevelosh.com> |
date |
Sat, 04 Dec 2021 13:11:25 -0500 |
parents |
182bdd87fd9e |
children |
(none) |
(advent:defpackage* :advent/2018/08)
(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) (37905 33891)
(let ((root (read-node data)))
(values
(recursively ((node root))
(+ (summation (metadata node))
(summation (children node) :key #'recur)))
(node-value root))))