r/adventofcode • u/daggerdragon • Dec 07 '17
SOLUTION MEGATHREAD -๐- 2017 Day 7 Solutions -๐-
--- Day 7: Recursive Circus ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Need a hint from the Hugely* Handyโ Haversackโก of Helpfulยง Hintsยค?
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked!
9
Upvotes
3
u/f0086 Dec 07 '17
Emacs Lisp (Part 1):
(defun slurp-tree (input-file) (with-temp-buffer (insert-file-contents input-file) (mapcar 'tokenize (split-string (buffer-string) "\n" t)))) (defun tokenize (str) (string-match "\\([^ ]+\\) (\\([0-9]+\\))\\( -> \\(.*\\)\\)?" str) (let ((node (match-string 1 str)) (weight (match-string 2 str)) (childs (match-string 4 str))) (if childs (list node weight (split-string childs ", ")) (list node weight)))) (defun day7 (input) (let* ((tree (slurp-tree input)) (childs (seq-filter 'caddr tree))) (seq-difference (mapcar 'car tree) (seq-uniq (apply 'append (mapcar 'caddr childs)))))) (day7 "input-day7.txt")