← 78645c33b002 · 78806e443729 →

786a759f780e

Find every 4-connected nonzero component, even when a component mixes colors. Crop each component, count enclosed black regions inside its bounding box, sort by increasing hole count, increasing painted area, and finally the first row-major component color, then pack the crops left-to-right with one black separator column.

parametric familycomponent extractiondynamic orderinghole countinglayout

Generated by the released generator before publication; every output was computed by its Racket reference rule. White outlines mark the cells the rule changed. Lesson check:

refuted: a demonstration contradicts the rival. survived_all: it fits every demonstration and also gives the intended answer, so it cannot mislead here. ambiguous: it fits every demonstration but answers the query differently. Names are as declared by the generator; a declared shortcut is not guaranteed to fit the original pairs (see the paper’s Limitations).

Reference rule (Racket)

(rule!
  (let ((dummy 0))
    (define (components-all)
      (define visited (make-hash))
      (define comps (list))
      (for* ([r (in-range (rows g))] [c (in-range (cols g))])
        (when (and (not (= (at g r c) 0))
                   (not (hash-has-key? visited (cons r c))))
          (define color (at g r c))
          (define queue (list (list r c)))
          (define cells (list))
          (hash-set! visited (cons r c) #t)
          (define (bfs)
            (unless (empty? queue)
              (define p (first queue))
              (set! queue (rest queue))
              (set! cells (cons p cells))
              (for ([d (in-list cardinal-deltas)])
                (define nr (+ (first p) (first d)))
                (define nc (+ (second p) (second d)))
                (when (and (in-bounds? nr nc (rows g) (cols g))
                           (not (= (at g nr nc) 0))
                           (not (hash-has-key? visited (cons nr nc))))
                  (hash-set! visited (cons nr nc) #t)
                  (set! queue (append queue (list (list nr nc))))))
              (bfs)))
          (bfs)
          (set! comps (cons (list color (reverse cells)) comps))))
      (reverse comps))
    (define (hole-count cells)
      (if (empty? cells)
          0
          (let* ((bb (bbox-of-cells cells))
                 (h (+ (- (third bb) (first bb)) 1))
                 (w (+ (- (fourth bb) (second bb)) 1))
                 (solid (make-hash))
                 (visited (make-hash))
                 (holes 0))
            (for ([p (in-list cells)])
              (hash-set! solid (cons (- (first p) (first bb)) (- (second p) (second bb))) #t))
            (for* ([r (in-range h)] [c (in-range w)])
              (when (and (not (hash-has-key? solid (cons r c)))
                         (not (hash-has-key? visited (cons r c))))
                (let ((queue (list (list r c)))
                      (touch (or (= r 0) (= c 0) (= r (- h 1)) (= c (- w 1)))))
                  (hash-set! visited (cons r c) #t)
                  (define (bfs0)
                    (unless (empty? queue)
                      (define p (first queue))
                      (set! queue (rest queue))
                      (for ([d (in-list cardinal-deltas)])
                        (define nr (+ (first p) (first d)))
                        (define nc (+ (second p) (second d)))
                        (when (and (in-bounds? nr nc h w)
                                   (not (hash-has-key? solid (cons nr nc)))
                                   (not (hash-has-key? visited (cons nr nc))))
                          (when (or (= nr 0) (= nc 0) (= nr (- h 1)) (= nc (- w 1)))
                            (set! touch #t))
                          (hash-set! visited (cons nr nc) #t)
                          (set! queue (append queue (list (list nr nc))))))
                      (bfs0)))
                  (bfs0)
                  (when (not touch) (set! holes (+ holes 1))))))
            holes)))
    (define items
      (sort (map (lambda (item)
                   (let* ((color (first item))
                          (cells (second item))
                          (bb (bbox-of-cells cells)))
                     (list (hole-count cells)
                           (length cells)
                           color
                           (subgrid g (first bb) (second bb) (third bb) (fourth bb)))))
                 (components-all))
            (lambda (a b)
              (cond [(not (= (first a) (first b))) (< (first a) (first b))]
                    [(not (= (second a) (second b))) (< (second a) (second b))]
                    [else (< (third a) (third b))]))))
    (define crops (map fourth items))
    (define out-h (apply max (map rows crops)))
    (define out-w (+ (apply + (map cols crops)) (max 0 (- (length crops) 1))))
    (let pack ([remaining crops] [left 0] [out (empty-grid out-h out-w 0)])
      (if (empty? remaining)
          out
          (let* ((crop (first remaining))
                 (next (for*/fold ([acc out])
                                   ([r (in-range (rows crop))]
                                    [c (in-range (cols crop))])
                         (let ((v (at crop r c)))
                           (if (not (= v 0)) (set-cell acc r (+ left c) v) acc)))))
            (pack (rest remaining) (+ left (cols crop) 1) next))))))

Provenance

Generator
generators/786a759f780e/generator.py
Notes and decisions
generators/786a759f780e/notes.md
Review label
reviewed_safe
Generator SHA-256
e5caaeb59dcbcfd5c9efc45b2d8291e96d7ce8600302237107dd80e8136ce7ba
Rule hash
71abd31a186f
Sources
bank:f3a2e93ae563
Lesson seeds
20261108, 20261109, 20261110, 20261111, 20261112
Reproduce lesson 1
arcpuzzles generate 786a759f780e --count 1 --seed 20261108 --test 1