diff --git a/21.rkt b/21.rkt index 3808d54..28f1070 100644 --- a/21.rkt +++ b/21.rkt @@ -16,10 +16,10 @@ (define (part2 input) (match-define (data lines all-is all-as tainted) input) - (define G (undirected-graph - (for*/list ([(k v*) (in-hash tainted)] [v (in-set v*)]) - (list k v)))) - (define matching (maximum-bipartite-matching G)) + (define matching + (maximum-bipartite-matching/stable + (for*/list ([(k v*) (in-hash tainted)] [v (in-set v*)]) + (list k v)))) (string-join (map second (sort matching stringset (map first edges))) + (define G (undirected-graph edges)) + (define matching (maximum-bipartite-matching G)) + (define num-first-lefts + (for/sum ([m (in-list matching)] #:when (set-member? left (first m))) + 1)) + (if (> num-first-lefts (/ (set-count left) 2)) + matching + (map #{list (second %) (first %)} matching))) + ;; submit a solution to the server if not already submitted (define (answer day part answer) (printf "answer ~a.~a: ~s\n" day part answer)