← public_eval_dfadab01 · public_eval_e3721c99 →

public_eval_e12f9a14

Launch rays through frame gaps; projected collisions merge into vector-sum bundles.

parametric family

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).

Original task pairs

From the source task; the reference rule must reproduce them.

Reference rule (Racket)

(rule!
 ;; A uniform 2x2 core inside a 4x4 frame launches its color through every
 ;; missing perimeter cell. Rays follow the outward compass direction. Their
 ;; projected paths are geometric rather than synchronized: when two next
 ;; segments meet, swap, or diagonally cross, advance both bundles to the cells
 ;; immediately before that event and either stop them or continue every member
 ;; in the primitive vector sum of their courses. Original non-background cells
 ;; are immutable and merged members remain side by side.
 (lambda (g)
  (let ()
   (define collision-rule 'vector_sum)
   (define h (rows g))
   (define w (cols g))
   (define bg (mode g -1))
   (define (inside? p)
    (and (<= 0 (first p)) (< (first p) h)
         (<= 0 (second p)) (< (second p) w)))
   (define (count-value xs value)
    (length (filter (lambda (x) (= x value)) xs)))
   (define (most-common xs)
    (for/fold ((best (first xs))) ((value (rest (remove-duplicates xs))))
     (define bv (count-value xs best))
     (define vv (count-value xs value))
     (if (or (> vv bv) (and (= vv bv) (< value best))) value best)))
   (define (primitive course)
    (define dr (first course))
    (define dc (second course))
    (define d (gcd (abs dr) (abs dc)))
    (if (= d 0) (list 0 0) (list (quotient dr d) (quotient dc d))))
   (define (step-cycle course)
    (define p (primitive course))
    (define dr (first p))
    (define dc (second p))
    (define ar (abs dr))
    (define ac (abs dc))
    (define period (max ar ac))
    (define sr (cond ((< dr 0) -1) ((> dr 0) 1) (else 0)))
    (define sc (cond ((< dc 0) -1) ((> dc 0) 1) (else 0)))
    (if (= period 0) '()
     (for/list ((k (range 1 (+ period 1))))
      (list (* sr (- (quotient (* k ar) period)
                     (quotient (* (- k 1) ar) period)))
            (* sc (- (quotient (* k ac) period)
                     (quotient (* (- k 1) ac) period)))))))
   (define (perimeter top left)
    (for*/list ((r (range top (+ top 4)))
                (c (range left (+ left 4)))
                #:when (and (or (= r top) (= r (+ top 3))
                                (= c left) (= c (+ left 3)))
                            (inside? (list r c))))
     (list r c)))
   (define launcher-records
    (filter values
     (for*/list ((ir (range (- h 1))) (ic (range (- w 1))))
      (define core (at g ir ic))
      (if (or (= core bg)
              (not (= (at g ir (+ ic 1)) core))
              (not (= (at g (+ ir 1) ic) core))
              (not (= (at g (+ ir 1) (+ ic 1)) core)))
          #f
       (let ()
        (define top (- ir 1))
        (define left (- ic 1))
        (define per (perimeter top left))
        (define candidates
         (filter (lambda (v) (and (not (= v bg)) (not (= v core))))
                 (map (lambda (p) (at g (first p) (second p))) per)))
        (if (null? candidates) #f
         (let ()
          (define frame (most-common candidates))
          (define lawful?
           (and (andmap (lambda (p)
                         (define v (at g (first p) (second p)))
                         (or (= v frame) (= v bg))) per)
                (>= (count-value (map (lambda (p) (at g (first p) (second p))) per) frame)
                    (max 4 (quotient (length per) 2)))))
          (if (not lawful?) #f
           (let ()
            (define gaps
             (filter (lambda (p) (not (= (at g (first p) (second p)) frame))) per))
            (define rels
             (map (lambda (p) (list (- (first p) top) (- (second p) left))) gaps))
            (define anomaly?
             (and (= (length rels) 3)
                  (member (list 0 2) rels)
                  (member (list 1 0) rels)
                  (member (list 3 3) rels)))
            (define emitters
             (filter values
              (for/list ((p gaps))
               (define rel (list (- (first p) top) (- (second p) left)))
               (if (and anomaly? (equal? rel (list 3 3))) #f
                (let ()
                 (define dr (cond ((= (first p) top) -1)
                                  ((= (first p) (+ top 3)) 1)
                                  (else 0)))
                 (define dc (cond ((= (second p) left) -1)
                                  ((= (second p) (+ left 3)) 1)
                                  (else 0)))
                 (if (and (= dr 0) (= dc 0)) #f
                     (list p (list dr dc) core (list top left))))))))
            (if (null? emitters) #f (list top left emitters)))))))))))
   (define sorted-launchers
    (sort launcher-records
     (lambda (a b)
      (or (< (first a) (first b))
          (and (= (first a) (first b)) (< (second a) (second b)))))))
   (define emitters (apply append (map third sorted-launchers)))

   ;; member = (serial color row col), bundle = (serial members course phase)
   (define (mserial m) (first m))
   (define (mcolor m) (second m))
   (define (mpos m) (list (third m) (fourth m)))
   (define (move-member m step)
    (list (mserial m) (mcolor m)
          (+ (third m) (first step)) (+ (fourth m) (second step))))
   (define (bserial b) (first b))
   (define (bmembers b) (second b))
   (define (bcourse b) (third b))
   (define (bphase b) (fourth b))
   (define (bundle-step b)
    (define cycle (step-cycle (bcourse b)))
    (if (null? cycle) (list 0 0)
        (list-ref cycle (modulo (bphase b) (length cycle)))))
   (define (paint out member)
    (define p (mpos member))
    (if (and (inside? p)
             (= (at g (first p) (second p)) bg)
             (= (at out (first p) (second p)) bg))
        (set-cell out (first p) (second p) (mcolor member))
        out))
   (define initial-members
    (for/list ((emitter emitters) (serial (range (length emitters))))
     (list serial (third emitter) (first (first emitter)) (second (first emitter)))))
   (define initial-bundles
    (for/list ((emitter emitters) (member initial-members) (serial (range (length emitters))))
     (list serial (list member) (second emitter) 0)))
   (define initial-output
    (for/fold ((out g)) ((member initial-members)) (paint out member)))

   (define (advance b moves out)
    (if (= moves 0) (list b out)
     (let ()
      (define cycle (step-cycle (bcourse b)))
      (if (null? cycle) (list b out)
       (let ()
        (define step (bundle-step b))
        (define moved (map (lambda (m) (move-member m step)) (bmembers b)))
        (define out2 (for/fold ((acc out)) ((m moved)) (paint acc m)))
        (define phase2 (modulo (+ (bphase b) 1) (length cycle)))
        (advance (list (bserial b) moved (bcourse b) phase2) (- moves 1) out2))))))
   (define (segment-records b limit)
    (let loop ((move 1) (current b) (records '()))
     (if (> move limit) (reverse records)
      (let ()
       (define step (bundle-step current))
       (define moved (map (lambda (m) (move-member m step)) (bmembers current)))
       (define additions
        (for/list ((before (bmembers current)) (after moved))
         (list move (mpos before) (mpos after))))
       (define cycle (step-cycle (bcourse current)))
       (define phase2 (if (null? cycle) 0 (modulo (+ (bphase current) 1) (length cycle))))
       (loop (+ move 1) (list (bserial current) moved (bcourse current) phase2)
             (append (reverse additions) records))))))
   (define (orient a b c)
    (define value (- (* (- (second b) (second a)) (- (first c) (first a)))
                     (* (- (first b) (first a)) (- (second c) (second a)))))
    (cond ((> value 0) 1) ((< value 0) -1) (else 0)))
   (define (proper-cross? a0 a1 b0 b1)
    (and (= (length (remove-duplicates (list a0 a1 b0 b1))) 4)
         (< (* (orient a0 a1 b0) (orient a0 a1 b1)) 0)
         (< (* (orient b0 b1 a0) (orient b0 b1 a1)) 0)))
   (define (conflict? a0 a1 b0 b1)
    (or (equal? a1 b1)
        (and (not (equal? collision-rule 'destination_only))
             (or (and (equal? a0 b1) (equal? b0 a1))
                 (proper-cross? a0 a1 b0 b1)))))
   (define (point2 a0 a1 b0 b1)
    (if (equal? a1 b1)
        (list (* 2 (first a1)) (* 2 (second a1)))
        (list (+ (first a0) (first a1)) (+ (second a0) (second a1)))))
   (define (point-inside? p)
    (and (<= 0 (first p)) (<= (first p) (* 2 (- h 1)))
         (<= 0 (second p)) (<= (second p) (* 2 (- w 1)))))
   (define (lex<? a b)
    (cond ((null? a) #f)
          ((< (first a) (first b)) #t)
          ((> (first a) (first b)) #f)
          (else (lex<? (rest a) (rest b)))))
   (define (candidate-key candidate)
    (define lm (third candidate))
    (define rm (fourth candidate))
    (define p (fifth candidate))
    (list (max lm rm) (+ lm rm) (min lm rm) (first p) (second p)
          (first candidate) (second candidate)))
   (define (best-candidate candidates)
    (if (null? candidates) #f
     (for/fold ((best (first candidates))) ((candidate (rest candidates)))
      (if (lex<? (candidate-key candidate) (candidate-key best)) candidate best))))
   (define search-limit (+ (* 2 (+ h w)) 8))
   (define (pair-event left right)
    (define left-records (segment-records left search-limit))
    (define right-records (segment-records right search-limit))
    (define destinations (make-hash))
    (define segments (make-hash))
    (define diagonals (make-hash))
    (for ((record right-records))
     (define p0 (second record))
     (define p1 (third record))
     (hash-update! destinations p1 (lambda (xs) (cons record xs)) '())
     (hash-update! segments (list p0 p1) (lambda (xs) (cons record xs)) '())
     (when (and (= (abs (- (first p1) (first p0))) 1)
                (= (abs (- (second p1) (second p0))) 1))
      (hash-update! diagonals
                    (list (+ (first p0) (first p1)) (+ (second p0) (second p1)))
                    (lambda (xs) (cons record xs)) '())))
    (define best #f)
    (define (consider left-record right-record p)
     (when (point-inside? p)
      (define candidate
       (list (bserial left) (bserial right)
             (first left-record) (first right-record) p))
      (when (or (not best) (lex<? (candidate-key candidate) (candidate-key best)))
       (set! best candidate))))
    (for ((record left-records))
     (define a0 (second record))
     (define a1 (third record))
     (for ((other (hash-ref destinations a1 '())))
      (consider record other (list (* 2 (first a1)) (* 2 (second a1)))))
     (unless (equal? collision-rule 'destination_only)
      (for ((other (hash-ref segments (list a1 a0) '())))
       (consider record other (list (+ (first a0) (first a1))
                                    (+ (second a0) (second a1)))))
      (when (and (= (abs (- (first a1) (first a0))) 1)
                 (= (abs (- (second a1) (second a0))) 1))
       (define midpoint (list (+ (first a0) (first a1))
                              (+ (second a0) (second a1))))
       (for ((other (hash-ref diagonals midpoint '()))
             #:when (proper-cross? a0 a1 (second other) (third other)))
        (consider record other midpoint)))))
    best)
   (define (all-events bundles)
    (filter values
     (for*/list ((i (range (length bundles)))
                 (j (range (+ i 1) (length bundles))))
      (pair-event (list-ref bundles i) (list-ref bundles j)))))
   (define (bundle-by-id bundles serial)
    (first (filter (lambda (b) (= (bserial b) serial)) bundles)))
   (define (remove-ids bundles ids)
    (filter (lambda (b) (not (member (bserial b) ids))) bundles))
   (define (event-loop bundles out next-serial)
    (if (or (equal? collision-rule 'independent) (< (length bundles) 2))
        (list bundles out next-serial)
     (let ((event (best-candidate (all-events bundles))))
      (if (not event) (list bundles out next-serial)
       (let ()
        (define left0 (bundle-by-id bundles (first event)))
        (define right0 (bundle-by-id bundles (second event)))
        (define left-result (advance left0 (- (third event) 1) out))
        (define left (first left-result))
        (define out1 (second left-result))
        (define right-result (advance right0 (- (fourth event) 1) out1))
        (define right (first right-result))
        (define out2 (second right-result))
        (define remaining (remove-ids bundles (list (bserial left0) (bserial right0))))
        (define summed
         (primitive (list (+ (first (bcourse left)) (first (bcourse right)))
                          (+ (second (bcourse left)) (second (bcourse right))))))
        (if (or (equal? collision-rule 'stop) (equal? summed (list 0 0)))
            (event-loop remaining out2 next-serial)
         (let ()
          (define members
           (sort (append (bmembers left) (bmembers right))
                 (lambda (a b) (< (mserial a) (mserial b)))))
          (define merged-result (advance (list next-serial members summed 0) 1 out2))
          (event-loop (sort (append remaining (list (first merged-result)))
                            (lambda (a b) (< (bserial a) (bserial b))))
                      (second merged-result) (+ next-serial 1)))))))))
   (define (outside-forever? b)
    (define dr (first (bcourse b)))
    (define dc (second (bcourse b)))
    (define positions (map mpos (bmembers b)))
    (define row-gone
     (or (and (< dr 0) (andmap (lambda (p) (< (first p) 0)) positions))
         (and (> dr 0) (andmap (lambda (p) (>= (first p) h)) positions))))
    (define col-gone
     (or (and (< dc 0) (andmap (lambda (p) (< (second p) 0)) positions))
         (and (> dc 0) (andmap (lambda (p) (>= (second p) w)) positions))))
    (if (= dr 0) col-gone (if (= dc 0) row-gone (or row-gone col-gone))))
   (define (finish b out)
    (let loop ((current b) (acc out) (remaining search-limit))
     (if (or (= remaining 0) (outside-forever? current)) acc
      (let ((result (advance current 1 acc)))
       (loop (first result) (second result) (- remaining 1))))))
   (define active-result (event-loop initial-bundles initial-output (length initial-bundles)))
   (for/fold ((out (second active-result))) ((b (first active-result)))
    (finish b out)))))

Provenance

Generator
generators/public_eval_e12f9a14/generator.py
Notes and decisions
generators/public_eval_e12f9a14/notes.md
Review label
reviewed_safe
Generator SHA-256
9568e770ad6f5ea855042d78739c9e91bbec5d09b0aaaea6464ad914ddee3b48
Rule hash
e8d748e5778b
Sources
evaluation, public_eval:e12f9a14
Lesson seeds
20261108, 20261109, 20261110, 20261111, 20261112
Reproduce lesson 1
arcpuzzles generate public_eval_e12f9a14 --count 1 --seed 20261108 --test 1