-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added solution for day 8.
- Loading branch information
Showing
2 changed files
with
34 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,4 +15,5 @@ | |
(:file "day4") | ||
(:file "day5") | ||
(:file "day6") | ||
(:file "day7"))) | ||
(:file "day7") | ||
(:file "day8"))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)))) |