0a6b6dba8b90

2017-7/1
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Sat, 01 Dec 2018 17:00:00 -0500
parents d3799236a70d
children 66e86b59fc60
branches/tags (none)
files advent.asd src/2017/main.lisp src/utils.lisp

Changes

--- a/advent.asd	Sat Dec 01 16:59:43 2018 -0500
+++ b/advent.asd	Sat Dec 01 17:00:00 2018 -0500
@@ -5,7 +5,17 @@
 
   :license "MIT"
 
-  :depends-on (:iterate :losh :split-sequence :str)
+  :depends-on (
+
+               :cl-digraph
+               :cl-digraph.dot
+               :cl-ppcre
+               :iterate
+               :losh
+               :split-sequence
+               :str
+
+               )
 
   :serial t
   :components ((:module "vendor" :serial t
--- a/src/2017/main.lisp	Sat Dec 01 16:59:43 2018 -0500
+++ b/src/2017/main.lisp	Sat Dec 01 17:00:00 2018 -0500
@@ -1,4 +1,5 @@
 (in-package :advent)
+(named-readtables:in-readtable :interpol-syntax)
 
 
 (define-problem (2017 1 1) (data read-file-of-digits)
@@ -126,3 +127,29 @@
         (for last-seen = (gethash banks seen))
         (until last-seen)
         (finally (return (values cycle (- cycle last-seen))))))))
+
+
+(define-problem (2017 7) (data read-lines-from-file)
+  (labels
+      ((parse-line (line)
+         (ppcre:register-groups-bind
+             (name (#'parse-integer weight) ((curry #'str:split ", ") holding))
+             (#?/(\w+) \((\d+)\)(?: -> (.+))?/
+              line)
+           (values name weight holding)))
+       (insert-edge (digraph pred succ)
+         (digraph:insert-vertex digraph pred)
+         (digraph:insert-vertex digraph succ)
+         (digraph:insert-edge digraph pred succ))
+       (build-tower (lines)
+         (iterate
+           (with tower = (digraph:make-digraph :test #'equal))
+           (for line :in lines)
+           (for (values name weight holding) = (parse-line line))
+           (collect-hash (name weight) :into weights :test #'equal)
+           (digraph:insert-vertex tower name)
+           (map nil (curry #'insert-edge tower name) holding)
+           (finally (return tower)))))
+    (let ((tower (build-tower data)))
+      ;; (digraph.dot:draw tower)
+      (first (digraph:roots tower)))))
--- a/src/utils.lisp	Sat Dec 01 16:59:43 2018 -0500
+++ b/src/utils.lisp	Sat Dec 01 17:00:00 2018 -0500
@@ -18,6 +18,11 @@
   (with-open-file (s path)
     (read s)))
 
+(defun read-lines-from-file (path)
+  "Read the lines in `path` into a list of strings."
+  (iterate (for line :in-file path :using #'read-line)
+           (collect line)))
+
 
 (defun read-file-of-digits (path)
   "Read all the ASCII digits in `path` into a list of integers.