Discussion:
(3-digit combination) Lock.lsp (in Gauche Scheme)
(too old to reply)
HenHanna
2024-02-26 11:47:28 UTC
Permalink
(pls suggest improvements. Thanks!)


PS C:\Lisp\LockPuz> gosh -I .
gosh> (load "Lock.lsp")
1000 ( Now applying Constraint: ) ((6 8 2) (1 1))
192 ( Now applying Constraint: ) ((6 1 4) (1 0))
38 ( Now applying Constraint: ) ((2 0 6) (2 0))
1
((0 4 2))


(define (Score X Y)
(list (apply + (map (lambda (y) (if (member y X) 1 0)) Y))
(count zero? (map - X Y))))

(define (MapCan f Lis) (apply append (map f Lis)))

(define (run)
(let* ((Const '(((6 8 2) (1 1))
((6 1 4) (1 0))
((2 0 6) (2 0)) ))
(dig (iota 10))
(Cand (MapCan (lambda (x)
(map (lambda (i) (cons x i))
(MapCan (lambda (y) (map (lambda (z) (list y z))
dig)) dig))) dig)))
(dolist (req Const)
(format #t "~T ~S ~T ( Now applying Constraint: ) ~T ~S ~%"
(length Cand) req)
(set! Cand
(filter (lambda (c) (equal? (Score c (car req)) (cadr req)))
Cand)))
(format #t "~T ~S ~% ~T ~S ~%" (length Cand) Cand)))
(run)
HenHanna
2024-02-26 12:23:14 UTC
Permalink
                 (pls suggest improvements.  Thanks!)
PS C:\Lisp\LockPuz>        gosh    -I   .
gosh> (load "Lock.lsp")
         1000    ( Now applying Constraint: )        ((6 8 2) (1 1))
         192     ( Now applying Constraint: )        ((6 1 4) (1 0))
         38      ( Now applying Constraint: )        ((2 0 6) (2 0))
         1
         ((0 4 2))
(define (Score X Y)
  (list (apply + (map (lambda (y) (if (member y X) 1 0))    Y))
        (count zero?  (map - X Y))))
(define (MapCan f Lis)    (apply append (map f Lis)))
(define (run)
  (let* ((Const '(((6 8 2)  (1 1))
                  ((6 1 4)  (1 0))
                  ((2 0 6)  (2 0)) ))
         (dig (iota 10))
         (Cand (MapCan (lambda (x)
                   (map (lambda (i) (cons x i))
                        (MapCan (lambda (y) (map (lambda (z) (list y z))
                                                 dig)) dig))) dig)))
more readable as:

(define (conv3 x)
(list (floor/ x 100) (mod (floor/ x 10) 10) (mod x 10)))

(define Thousand (map conv3 (iota 1000)))
HenHanna
2024-03-01 02:11:33 UTC
Permalink
i enjoyed working on the problem of coming up with a 1-line Python code.
We dont really have LINES in Lisp-Scheme... What would be a similar challenge in Lisp-Scheme?



(define (Score X Y)
(list (count list? (map (lambda (y) (member y X)) Y))
(count zero? (map - X Y))))
; <--- Is there a better way? (using equal? instead of - ) ?
Paul Rubin
2024-03-01 02:28:44 UTC
Permalink
Post by HenHanna
(define (Score X Y)
(list (count list? (map (lambda (y) (member y X)) Y))
(count zero? (map - X Y))))
; <--- Is there a better way? (using equal? instead of - ) ?
This was my version:

(define (score2 candidate answer)
(let* ((well-placed (length (filter identity (map = candidate answer))))
(wrongly-placed (- (length (lset-intersection = candidate answer))
well-placed)))
(values well-placed wrongly-placed)))

Maybe there is something better.
HenHanna
2024-03-01 02:56:20 UTC
Permalink
(define (Score X Y)
(list (count (lambda (y) (member y X)) Y)
(count equal? X Y)))



Where can i find doc for COUNT ?

The section in the Gauche manual is so short!

but now i see that it explains what i was looking for
the count of times pred returned true is returned.
https://practical-scheme.net/gauche/man/gauche-refe/Pairs-and-lists.html


Function: count pred clist1 clist2 …

[R7RS list] A procedure pred is applied to the n-th element of given lists, from n is zero to the length of the the shortest finite list in the given lists, and the count of times pred returned true is returned.

(count even? '(3 1 4 1 5 9 2 5 6)) ⇒ 3
(count < '(1 2 4 8) '(2 4 6 8 10 12 14 16)) ⇒ 3

At least one of the argument lists must be finite:

(count < '(3 1 4 1) (circular-list 1 10)) ⇒ 2
Paul Rubin
2024-03-01 03:39:06 UTC
Permalink
FYI, here is an interesting paper about playing Mastermind using a SAT
solver, beating other approaches. Mastermind turns out to be well-known
to be NP-complete (a quick web search found that).

https://www.seas.upenn.edu/~ncollina/Mastermind.pdf
HenHanna
2024-03-02 09:40:04 UTC
Permalink
Post by Paul Rubin
FYI, here is an interesting paper about playing Mastermind using a SAT
solver, beating other approaches. Mastermind turns out to be well-known
to be NP-complete (a quick web search found that).
https://www.seas.upenn.edu/~ncollina/Mastermind.pdf
thank you... i'll take a look.



What are some relevant sections in Knuth's book [Satisfiability] ?

Sudoku is mentioned several times, but not Mastermind.
Paul Rubin
2024-03-02 18:44:28 UTC
Permalink
Post by HenHanna
What are some relevant sections in Knuth's book [Satisfiability] ?
That book discusses the algorithms used in SAT solvers, but from what
little I know, they are all tweaks and improvements on the DPLL
algorithm from the 1960s:

https://en.wikipedia.org/wiki/DPLL_algorithm

Thus, if you want to study the workings of SAT solvers, you could start
by looking at MiniSAT which is one of the simplest. If you want to
learn how to use them, this is good:

https://yurichev.com/writings/SAT_SMT_by_example.pdf

Loading...