src/2017/days/day-12.lisp @ 2078ac8647c6
2017 16, 17, 18p1
author |
Steve Losh <steve@stevelosh.com> |
date |
Fri, 06 Dec 2019 20:58:57 -0500 |
parents |
cd781337a694 |
children |
182bdd87fd9e |
(defpackage :advent/2017/12 #.cl-user::*advent-use*)
(in-package :advent/2017/12)
(defun parse-line (line)
(destructuring-bind (id others) (str:split "<->" line)
(list (parse-integer id)
(mapcar #'parse-integer (str:split #\, others)))))
(defun build-graph (records)
(iterate
(with graph = (digraph:make-digraph :initial-vertices (mapcar #'car records)))
(for (id others) :in records)
(dolist (id2 others)
(digraph:insert-edge graph id id2))
(finally (return graph))))
(defun connected-to (graph start)
(gathering
(digraph:mapc-depth-first #'gather graph start)))
(defun count-subgraph (graph start)
(length (connected-to graph start)))
(defun remove-subgraph (graph start)
(map nil (alexandria:curry #'digraph:remove-vertex graph)
(connected-to graph start)))
(define-problem (2017 12) (data read-lines) (141 171)
(let ((graph (build-graph (mapcar #'parse-line data))))
(values
(count-subgraph graph 0)
(iterate
(for vertex = (digraph:arbitrary-vertex graph))
(while vertex)
(remove-subgraph graph vertex)
(counting t)))))