Groups | Search | Server Info | Login | Register


Groups > fj.comp.lang.lisp > #13

Re: Mastermind Puzzle (3-digit Combination Lock) -- Elegant (readable) code Sought

From Kaz Kylheku <433-929-6894@kylheku.com>
Newsgroups comp.lang.lisp, fr.comp.lang.lisp, fj.comp.lang.lisp
Subject Re: Mastermind Puzzle (3-digit Combination Lock) -- Elegant (readable) code Sought
Date 2024-02-26 18:24 +0000
Organization A noiseless patient Spider
Message-ID <20240226102023.256@kylheku.com> (permalink)
References <urh45k$2br0h$2@dont-email.me>

Cross-posted to 3 groups.

Show all headers | View raw


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

Back to fj.comp.lang.lisp | Previous | NextPrevious in thread | Find similar


Thread

Mastermind Puzzle (3-digit Combination Lock) -- Elegant (readable) code Sought HenHanna <HenHanna@gmail.com> - 2024-02-25 20:30 -0800
  Re: Mastermind Puzzle (3-digit Combination Lock) -- Elegant (readable) code Sought Kaz Kylheku <433-929-6894@kylheku.com> - 2024-02-26 07:34 +0000
  Re: Mastermind Puzzle (3-digit Combination Lock) -- Elegant (readable) code Sought Paul Rubin <no.email@nospam.invalid> - 2024-02-26 00:12 -0800
  Re: Mastermind Puzzle (3-digit Combination Lock) -- Elegant (readable) code Sought Kaz Kylheku <433-929-6894@kylheku.com> - 2024-02-26 18:24 +0000

csiph-web