Created
July 1, 2022 17:33
-
-
Save marchelzo/b42c06b83c3f6ed18e4855bfe17c2640 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import chalk | |
import os (sleep, write) | |
let words = slurp('/usr/share/dict/words').words().filter!(/^[a-z]{5}$/) | |
let alphabet = [chr(o) for o in ord('a')...ord('z')] | |
function putc(s) { | |
write(1, s) | |
} | |
while true { | |
let word = rand(words) | |
let used = %{} | |
while true { | |
let guess = readLine() | |
if guess not in words { | |
print('Cope.') | |
continue | |
} | |
let w = word.chars() | |
let pens = [id for _ in guess] | |
for c, i in guess { | |
if w[i] == c { | |
pens[i] = chalk.brightGreen | |
w[i] = nil | |
} | |
used << c | |
} | |
for c, i in guess { | |
if pens[i] == id and let $j = w.search(c) { | |
pens[i] = chalk.yellow | |
w[j] = nil | |
} | |
} | |
for c in pens.zip(guess.chars(), (p, c) -> p(c)) { | |
sleep(0.4) | |
putc(c) | |
} | |
sleep(0.4) | |
putc('\n') | |
print(alphabet.map(c -> match (c in word, c in used) { | |
(true, true) => chalk.brightGreen(c), | |
(_, used) => used ? c : chalk.blue(c), | |
}).groupsOf(7).map(&join(' ')).unlines()) | |
if guess == word { | |
break | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment