Re: Mastermind Puzzle (3-digit Combination Lock) -- Elegant (readable) code Sought
On 2024-02-26, HenHanna <HenHanna@gmail.com> wrote:
>
> (i just wrote (non-elegant) Python code.)
>
>
> Could you share a short, VERY Readable Pythonic (or Common Lisp, Scheme)
> code that solves this?
TXR Lisp, using scoring method:
(defun score (pat can)
(vec (sum-each ((p pat) (c can))
(if (eql p c) 1 0))
(len (isec pat can))))
(defun filt-score (pat ngoodpl nbadpl list)
(keep-if (op equal (score pat @1) (vec ngoodpl nbadpl)) list))
(flow "000".."999"
list-seq
(filt-score "682" 1 1)
(filt-score "614" 0 1)
(filt-score "206" 0 2)
;; surprises: these two not required
(filt-score "738" 0 0)
(filt-score "780" 0 1)
prinl)
$ txr lock2.tl
("042")
--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
Fnews-brouse 1.9(20180406) -- by Mizuno, MWE <mwe@ccsf.jp>
GnuPG Key ID = ECC8A735
GnuPG Key fingerprint = 9BE6 B9E9 55A5 A499 CD51 946E 9BDC 7870 ECC8 A735