Skip to content

Instantly share code, notes, and snippets.

@samdphillips
Last active January 16, 2022 19:13
Show Gist options
  • Save samdphillips/f7306ffd199259b6533c19d1db785f2b to your computer and use it in GitHub Desktop.
Save samdphillips/f7306ffd199259b6533c19d1db785f2b to your computer and use it in GitHub Desktop.
Wordle
#lang racket/base
(require racket/set)
(define (make-check-word never-set need-set fixed avoid)
(lambda (word)
(let/ec ret
(define-syntax-rule (fail) (ret #f))
(define-syntax-rule (check expr)
(when expr (fail)))
(unless (= 5 (string-length word)) (fail))
(for/fold ([need need-set] #:result (set-empty? need))
([ch (in-string (string-downcase word))]
[req (in-list fixed)]
[neq (in-list avoid)])
(check (and req (not (equal? ch req))))
(check (set-member? never-set ch))
(check (and neq (set-member? neq ch)))
(set-remove need ch)))))
(define (check-words pred?)
(call-with-input-file "words"
(lambda (inp)
(for/list ([a-word (in-lines inp)] #:when (pred? a-word))
a-word))))
(define (string->set s)
(for/set ([c (in-string s)]) c))
(define (string->fixed s)
(for/list ([c (in-string s)])
(cond
[(char=? #\. c) #f]
[else c])))
(define (list->avoid a-list)
(for/list ([e (in-list a-list)])
(and e (string->set e))))
(check-words
(make-check-word (string->set "qupoisdf") ;; never
(string->set "ht") ;; need
(string->fixed "....t") ;; fixed position
(list->avoid '(#f #f "h" #f #f)))) ;; avoid in position
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment