fc+cdj works
parent
2e3a5d3fa0
commit
2eca36e674
@ -0,0 +1,58 @@
|
||||
#lang debug racket
|
||||
(require "hacs.rkt")
|
||||
(module+ test (require rackunit))
|
||||
|
||||
(define (map-coloring-csp colors neighbors)
|
||||
(define variables (remove-duplicates (flatten neighbors) eq?))
|
||||
(define vds (for/list ([var (in-list variables)])
|
||||
($var var colors null)))
|
||||
(define cs (for*/list ([neighbor neighbors]
|
||||
[target (cdr neighbor)])
|
||||
($constraint (list (car neighbor) target) neq?)))
|
||||
($csp vds cs))
|
||||
|
||||
(define (parse-colors str) (map string->symbol (map string-downcase (regexp-match* "." str))))
|
||||
(define(parse-neighbors str)
|
||||
(define recs (map string-trim (string-split str ";")))
|
||||
(for/list ([rec recs])
|
||||
(match-define (cons head tail) (string-split rec ":"))
|
||||
(map string->symbol (map string-downcase (map string-trim (cons head (string-split (if (pair? tail)
|
||||
(car tail)
|
||||
""))))))))
|
||||
|
||||
|
||||
(current-inference forward-check)
|
||||
(current-select-variable minimum-remaining-values)
|
||||
(define aus (map-coloring-csp (parse-colors "RGB")
|
||||
(parse-neighbors "SA: WA NT Q NSW V; NT: WA Q; NSW: Q V; T: ")))
|
||||
|
||||
(module+ test
|
||||
(check-equal? (length (solve* aus)) 18))
|
||||
|
||||
(define usa (map-coloring-csp (parse-colors "RGBY")
|
||||
(parse-neighbors "WA: OR ID; OR: ID NV CA; CA: NV AZ; NV: ID UT AZ; ID: MT WY UT;
|
||||
UT: WY CO AZ; MT: ND SD WY; WY: SD NE CO; CO: NE KA OK NM; NM: OK TX;
|
||||
ND: MN SD; SD: MN IA NE; NE: IA MO KA; KA: MO OK; OK: MO AR TX;
|
||||
TX: AR LA; MN: WI IA; IA: WI IL MO; MO: IL KY TN AR; AR: MS TN LA;
|
||||
LA: MS; WI: MI IL; IL: IN KY; IN: OH KY; MS: TN AL; AL: TN GA FL;
|
||||
MI: OH IN; OH: PA WV KY; KY: WV VA TN; TN: VA NC GA; GA: NC SC FL;
|
||||
PA: NY NJ DE MD WV; WV: MD VA; VA: MD DC NC; NC: SC; NY: VT MA CT NJ;
|
||||
NJ: DE; DE: MD; MD: DC; VT: NH MA; MA: NH RI CT; CT: RI; ME: NH;
|
||||
HI: ; AK:")))
|
||||
|
||||
(module+ test
|
||||
(check-true (pair? (solve usa))))
|
||||
|
||||
(define fr (map-coloring-csp (parse-colors "RGBY")
|
||||
(parse-neighbors "AL: LO FC; AQ: MP LI PC; AU: LI CE BO RA LR MP; BO: CE IF CA FC RA
|
||||
AU; BR: NB PL; CA: IF PI LO FC BO; CE: PL NB NH IF BO AU LI PC; FC: BO
|
||||
CA LO AL RA; IF: NH PI CA BO CE; LI: PC CE AU MP AQ; LO: CA AL FC; LR:
|
||||
MP AU RA PA; MP: AQ LI AU LR; NB: NH CE PL BR; NH: PI IF CE NB; NO:
|
||||
PI; PA: LR RA; PC: PL CE LI AQ; PI: NH NO CA IF; PL: BR NB CE PC; RA:
|
||||
AU BO FC PA LR")))
|
||||
|
||||
(module+ test
|
||||
(check-true (pair? (solve fr))))
|
||||
|
||||
(module+ main
|
||||
(solve aus))
|
@ -0,0 +1,47 @@
|
||||
#lang br
|
||||
(require "hacs.rkt")
|
||||
|
||||
; SEND
|
||||
;+ MORE
|
||||
;------
|
||||
; MONEY
|
||||
|
||||
(define $vd +var)
|
||||
|
||||
(define (word-value . xs)
|
||||
(for/sum ([(x idx) (in-indexed (reverse xs))])
|
||||
(* x (expt 10 idx))))
|
||||
|
||||
(define vs '(s e n d m o r y))
|
||||
(define vds (for/list ([k vs])
|
||||
($vd k (range 10))))
|
||||
|
||||
(define (not= x y) (not (= x y)))
|
||||
|
||||
(define alldiffs
|
||||
(for/list ([pr (in-combinations vs 2)])
|
||||
($constraint pr not=)))
|
||||
|
||||
(define (smm-func s e n d m o r y)
|
||||
(= (+ (word-value s e n d) (word-value m o r e)) (word-value m o n e y)))
|
||||
|
||||
(define csp ($csp vds (append
|
||||
|
||||
alldiffs
|
||||
(list
|
||||
($constraint vs smm-func)
|
||||
($constraint '(s) positive?)
|
||||
($constraint '(m) (λ (x) (= 1 x)))
|
||||
($constraint '(d e y) (λ (d e y) (= (modulo (+ d e) 10) y)))
|
||||
($constraint '(n d r e y) (λ (n d r e y)
|
||||
(= (modulo (+ (word-value n d) (word-value r e)) 100)
|
||||
(word-value e y))))
|
||||
($constraint '(e n d o r y) (λ (e n d o r y)
|
||||
(= (modulo (+ (word-value e n d) (word-value o r e)) 1000) (word-value n e y))))))))
|
||||
(parameterize ([current-select-variable mrv]
|
||||
[current-order-values lcv]
|
||||
[current-inference mac])
|
||||
(time (solve csp)))
|
||||
(nassigns csp)
|
||||
(nchecks csp)
|
||||
(reset! csp)
|
@ -0,0 +1,71 @@
|
||||
#lang debug racket
|
||||
(require "hacs.rkt" rackunit)
|
||||
|
||||
(check-equal? (first-unassigned-variable ($csp (list (+var 'a (range 3)) (+var 'b (range 3))) null))
|
||||
(+var 'a (range 3)))
|
||||
(check-equal? (first-unassigned-variable ($csp (list (+avar 'a (range 3)) (+var 'b (range 3))) null))
|
||||
(+var 'b (range 3)))
|
||||
(check-false (first-unassigned-variable ($csp (list (+avar 'a (range 3)) (+avar 'b (range 3))) null)))
|
||||
|
||||
(check-equal?
|
||||
;; no forward checking when no constraints
|
||||
($csp-vars (forward-check ($csp (list (+avar 'a '(1)) (+var 'b (range 2))) null) 'a))
|
||||
(list (+avar 'a '(1)) (+var 'b '(0 1))))
|
||||
|
||||
(check-equal?
|
||||
($csp-vars (forward-check (forward-check ($csp (list (+avar 'a '(1)) (+avar 'b '(0)) (+var 'c '(0 1 2)))
|
||||
(list ($constraint '(a c) (negate =))
|
||||
($constraint '(b c) (negate =)))) 'a) 'b))
|
||||
(list (+avar 'a '(1)) (+avar 'b '(0) '()) (+var 'c '(2) '(b a))))
|
||||
|
||||
(check-equal?
|
||||
;; no inconsistency: b≠c not checked when fc is relative to a
|
||||
($csp-vars (forward-check ($csp (list (+avar 'a '(1)) (+var 'b (range 2)) (+var 'c '(0)))
|
||||
(list ($constraint '(a b) (negate =))
|
||||
($constraint '(b c) (negate =)))) 'a))
|
||||
(list (+avar 'a '(1)) (+var 'b '(0) '(a)) (+var 'c '(0))))
|
||||
|
||||
(check-equal?
|
||||
;; no inconsistency: a≠b not checked when fc ignores a, which is already assigned
|
||||
($csp-vars (forward-check ($csp (list (+avar 'a '(1)) (+avar 'b '(1)) (+var 'c (range 2)))
|
||||
(list ($constraint '(a b) (negate =))
|
||||
($constraint '(b c) (negate =)))) 'b))
|
||||
(list (+avar 'a '(1)) (+avar 'b '(1)) (+var 'c '(0) '(b))))
|
||||
|
||||
(check-exn $backtrack?
|
||||
(λ () ($csp-vars (forward-check ($csp (list (+avar 'a '(1))
|
||||
(+var 'b '(1)))
|
||||
(list ($constraint '(a b) (negate =)))) 'a))))
|
||||
|
||||
|
||||
(check-equal? ($csp-vars (forward-check ($csp (list (+avar 'a (range 3))
|
||||
(+var 'b (range 3)))
|
||||
(list ($constraint '(a b) <)
|
||||
($constraint '(a b) <)
|
||||
($constraint '(a b) <))) 'a))
|
||||
(list (+avar 'a '(0 1 2)) (+var 'b '(1 2) '(a))))
|
||||
|
||||
(check-equal?
|
||||
(parameterize ([current-inference forward-check])
|
||||
(length (solve* ($csp (list (+var 'x (range 3))
|
||||
(+var 'y (range 3))
|
||||
(+var 'z (range 3)))
|
||||
(list ($constraint '(x y) <>)
|
||||
($constraint '(x z) <>)
|
||||
($constraint '(y z) <>)))))) 6)
|
||||
|
||||
(parameterize ([current-inference forward-check])
|
||||
(define vds (for/list ([k '(wa nt nsw q t v sa)])
|
||||
(+var k '(red green blue))))
|
||||
(define cs (list
|
||||
($constraint '(wa nt) neq?)
|
||||
($constraint '(wa sa) neq?)
|
||||
($constraint '(nt sa) neq?)
|
||||
($constraint '(nt q) neq?)
|
||||
($constraint '(q sa) neq?)
|
||||
($constraint '(q nsw) neq?)
|
||||
($constraint '(nsw sa) neq?)
|
||||
($constraint '(nsw v) neq?)
|
||||
($constraint '(v sa) neq?)))
|
||||
(define csp ($csp vds cs))
|
||||
(check-equal? (length (solve* csp)) 18))
|
Loading…
Reference in New Issue