Skip to content

Commit

Permalink
Day 8
Browse files Browse the repository at this point in the history
Added solution for day 8.
  • Loading branch information
abraemer committed Dec 8, 2018
1 parent 725071d commit 3976cc4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
3 changes: 2 additions & 1 deletion advent-of-code-2018.asd
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
(:file "day4")
(:file "day5")
(:file "day6")
(:file "day7")))
(:file "day7")
(:file "day8")))
32 changes: 32 additions & 0 deletions day8.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
;;;; day8.lisp

(in-package :advent-of-code-2018)

(defun day8-build-tree ()
(with-open-file (in (puzzlepath "input8.txt"))
(labels ((build-node ()
(let ((num-children (read in))
(num-metadata (read in)))
(list (loop :repeat num-children
:collect (build-node))
(loop :repeat num-metadata
:collect (read in))))))
(build-node))))

(defun day8-sum-metadata (node)
(+ (reduce #'+ (mapcar #'day8-sum-metadata (first node)))
(reduce #'+ (second node))))

(defun day8-node-value (node)
(flet ((nth-child-value (n)
(if (<= 1 n (length (first node)))
(day8-node-value (nth (1- n) (first node)))
0)))
(if (null (first node))
(reduce #'+ (second node))
(reduce #'+ (mapcar #'nth-child-value (second node))))))

(defun day8 ()
(let ((root (day8-build-tree)))
(format t "The sum of all metadata is ~a and the score for the root node is ~a.~%"
(day8-sum-metadata root) (day8-node-value root))))

0 comments on commit 3976cc4

Please sign in to comment.