master
Matthew Butterick 4 years ago
parent 50e2a39a2a
commit d8551d7c06

@ -0,0 +1,7 @@
Step C must be finished before step A can begin.
Step C must be finished before step F can begin.
Step A must be finished before step B can begin.
Step A must be finished before step D can begin.
Step B must be finished before step E can begin.
Step D must be finished before step E can begin.
Step F must be finished before step E can begin.

@ -0,0 +1,47 @@
#lang debug br
(require graph)
#|
Track the progress through the steps with a dag,
but track the state of the prerequisites with another dag (in the reverse direction)
|#
(define dag (directed-graph null))
(define prereqs (weighted-graph/directed null))
(for ([ln (in-lines (open-input-file "07.txt"))])
(match-define (list left right) (regexp-match* #rx"(?<=[Ss]tep )." ln))
(add-directed-edge! dag left right)
(add-directed-edge! prereqs right left +inf.0))
(define (activate-prereq! v1 v2)
;; a prereq is "met" if the edge weight is zero
(add-directed-edge! prereqs v2 v1 0))
(define (prereqs-met? v)
;; check if all v's edges in the prereq graph are zero
(andmap zero? (map (λ (n) (edge-weight prereqs v n)) (get-neighbors prereqs v))))
(define (find-available g)
(filter prereqs-met? (get-vertices g)))
(define ()
(let loop ([vs-available (find-available prereqs)] [visited null])
(cond
[(= (length visited) (length (get-vertices dag)))
(apply string-append (reverse visited))]
[else
(match (sort vs-available string<?)
[(list this-v others ...)
(for-each (λ (n) (activate-prereq! this-v n)) (get-neighbors dag this-v))
(define this-visited (cons this-v visited))
(loop (filter-not (λ (v) (member v this-visited)) (find-available prereqs))
this-visited)])])))
#;(define (★★)
)
#;(★★)
(module+ test
(require rackunit)
(check-equal? (time ()) "GNJOCHKSWTFMXLYDZABIREPVUQ")
#;(check-equal? (time (★★)) 566))

@ -0,0 +1,101 @@
Step O must be finished before step C can begin.
Step Y must be finished before step D can begin.
Step N must be finished before step D can begin.
Step G must be finished before step F can begin.
Step C must be finished before step Z can begin.
Step H must be finished before step K can begin.
Step W must be finished before step T can begin.
Step T must be finished before step F can begin.
Step S must be finished before step I can begin.
Step X must be finished before step B can begin.
Step J must be finished before step A can begin.
Step K must be finished before step D can begin.
Step Z must be finished before step A can begin.
Step A must be finished before step B can begin.
Step L must be finished before step V can begin.
Step F must be finished before step M can begin.
Step B must be finished before step V can begin.
Step M must be finished before step Q can begin.
Step D must be finished before step E can begin.
Step I must be finished before step U can begin.
Step R must be finished before step V can begin.
Step E must be finished before step U can begin.
Step P must be finished before step V can begin.
Step V must be finished before step Q can begin.
Step U must be finished before step Q can begin.
Step P must be finished before step U can begin.
Step O must be finished before step F can begin.
Step T must be finished before step M can begin.
Step I must be finished before step Q can begin.
Step M must be finished before step U can begin.
Step R must be finished before step E can begin.
Step T must be finished before step R can begin.
Step H must be finished before step S can begin.
Step L must be finished before step B can begin.
Step S must be finished before step Q can begin.
Step E must be finished before step Q can begin.
Step B must be finished before step Q can begin.
Step S must be finished before step M can begin.
Step C must be finished before step D can begin.
Step S must be finished before step R can begin.
Step G must be finished before step D can begin.
Step T must be finished before step E can begin.
Step T must be finished before step Q can begin.
Step N must be finished before step I can begin.
Step S must be finished before step P can begin.
Step N must be finished before step J can begin.
Step X must be finished before step L can begin.
Step G must be finished before step K can begin.
Step N must be finished before step E can begin.
Step H must be finished before step D can begin.
Step H must be finished before step P can begin.
Step O must be finished before step A can begin.
Step V must be finished before step U can begin.
Step F must be finished before step D can begin.
Step B must be finished before step P can begin.
Step T must be finished before step L can begin.
Step I must be finished before step P can begin.
Step K must be finished before step Z can begin.
Step G must be finished before step M can begin.
Step F must be finished before step Q can begin.
Step J must be finished before step L can begin.
Step H must be finished before step Q can begin.
Step W must be finished before step R can begin.
Step R must be finished before step U can begin.
Step P must be finished before step Q can begin.
Step D must be finished before step V can begin.
Step G must be finished before step C can begin.
Step Z must be finished before step B can begin.
Step O must be finished before step H can begin.
Step S must be finished before step A can begin.
Step J must be finished before step Q can begin.
Step N must be finished before step F can begin.
Step L must be finished before step R can begin.
Step O must be finished before step R can begin.
Step W must be finished before step M can begin.
Step J must be finished before step F can begin.
Step G must be finished before step W can begin.
Step K must be finished before step U can begin.
Step D must be finished before step U can begin.
Step W must be finished before step I can begin.
Step E must be finished before step V can begin.
Step Y must be finished before step Q can begin.
Step L must be finished before step E can begin.
Step S must be finished before step B can begin.
Step T must be finished before step V can begin.
Step C must be finished before step U can begin.
Step M must be finished before step P can begin.
Step G must be finished before step S can begin.
Step B must be finished before step R can begin.
Step K must be finished before step M can begin.
Step X must be finished before step A can begin.
Step R must be finished before step P can begin.
Step B must be finished before step I can begin.
Step C must be finished before step X can begin.
Step O must be finished before step P can begin.
Step D must be finished before step Q can begin.
Step F must be finished before step B can begin.
Step I must be finished before step R can begin.
Step Y must be finished before step I can begin.
Step M must be finished before step D can begin.
Step F must be finished before step U can begin.