Skip to content

Instantly share code, notes, and snippets.

@bellbind
Last active March 17, 2022 10:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bellbind/ecf34020ebd8306494d7974eeb91ff3a to your computer and use it in GitHub Desktop.
Save bellbind/ecf34020ebd8306494d7974eeb91ff3a to your computer and use it in GitHub Desktop.
[deno] wordle on console (use offcial data & refresh 24-hour)
{"fmt": {"options": {"lineWidth": 160, "indentWidth": 2, "singleQuote": false}}}
// deno run --allow-net wordle.js
import {JSDOM} from "https://jspm.dev/npm:jsdom-deno";
const url = "https://www.nytimes.com/games/wordle/index.html";
const {document} = new JSDOM(await fetch(url).then(r => r.text()), {url}).window;
const script = document.querySelector("script[src^=main]").src;
const code = await fetch(script).then(r => r.text());
const [solutions, accepts] = [...code.matchAll(/(\[("[a-z]{5}",)*"[a-z]{5}"\])/g)].map(m => JSON.parse(m[1]));
const index = (new Date().setHours(0, 0, 0, 0) - new Date("2021-6-19").getTime()) / (24 * 60 * 60 * 1000);
const answer = solutions[index % solutions.length];
const success = ["", "Phew", "Great", "Splendid", "Impressive", "Magnificent", "Genuis"];
const colors = {none: "background-color: #818384;", absent: "background-color: #3a3a3c;", present: "background-color: #b59f3b;", correct: "background-color: #538d4e;"};
const qwerty = ["qwertyuiop", "asdfghjkl", "zxcvbnm"];
const challenge = (answer, rest = 6, history = [], keymap = new Map()) => {
if (rest === 0) {
const correct = [...answer].map(c => `%c ${c.toUpperCase()} `).join("");
const css = [...answer].map(c => keymap.get(c) ?? colors.none);
console.log(`\n${" ".repeat(6)}${correct}`, ...css);
return;
}
const guess = prompt(`Guess 5 letter word (last ${rest}):`)?.trim()?.toLowerCase() ?? "";
if (!solutions.includes(guess) && !accepts.includes(guess)) return challenge(answer, rest, history, keymap);
const result = [...guess].map(c => `%c ${c.toUpperCase()} `).join("");
const resultCss = [...guess].map((c, i) => c === answer[i] ? colors.correct : answer.includes(c) ? colors.present : colors.absent);
resultCss.forEach((color, i) => {
if (color === colors.present && keymap.get(guess[i]) === colors.correct) return;
keymap.set(guess[i], color);
});
history.forEach(([r, css]) => console.log(r, ...css));
console.log(`${result}%c\n`, ...resultCss, "");
qwerty.forEach((line, i) => {
const keys = [...line].map(c => `%c ${c.toUpperCase()} `);
const keysCss = [...line].map(c => keymap.get(c) ?? colors.none);
console.log(`${" ".repeat(i * 2)}${keys.join("")}`, ...keysCss);
});
if (guess !== answer) return challenge(answer, rest - 1, [...history, [result, resultCss]], keymap);
console.log(`\n${" ".repeat(9)}%c ${success[rest]} ` , colors.none);
};
challenge(answer, 6);
globalThis?.Deno?.exit(0);
// deno run --allow-net wordle.ts
import {JSDOM} from "https://jspm.dev/npm:jsdom-deno";
const url = "https://www.nytimes.com/games/wordle/index.html";
const {document} = new JSDOM(await fetch(url).then(r => r.text()), {url}).window;
const script = document.querySelector("script[src^=main]").src;
const code = await fetch(script).then(r => r.text());
const [solutions, accepts] = [...code.matchAll(/(\[("[a-z]{5}",)*"[a-z]{5}"\])/g)].map(m => JSON.parse(m[1]));
const index = (new Date().setHours(0, 0, 0, 0) - new Date("2021-6-19").getTime()) / (24 * 60 * 60 * 1000);
const answer = solutions[index % solutions.length];
const success = ["", "Phew", "Great", "Splendid", "Impressive", "Magnificent", "Genuis"];
const colors = {none: "background-color: #818384;", absent: "background-color: #3a3a3c;", present: "background-color: #b59f3b;", correct: "background-color: #538d4e;"};
const qwerty = ["qwertyuiop", "asdfghjkl", "zxcvbnm"];
const challenge = (answer: string, rest = 6, history = [] as [string, string[]][], keymap = new Map()): void => {
if (rest === 0) {
const correct = [...answer].map(c => `%c ${c.toUpperCase()} `).join("");
const css = [...answer].map(c => keymap.get(c) ?? colors.none);
console.log(`\n${" ".repeat(6)}${correct}`, ...css);
return;
}
const guess = prompt(`Guess 5 letter word (last ${rest}):`)?.trim()?.toLowerCase() ?? "";
if (!solutions.includes(guess) && !accepts.includes(guess)) return challenge(answer, rest, history, keymap);
const result = [...guess].map(c => `%c ${c.toUpperCase()} `).join("");
const resultCss = [...guess].map((c, i) => c === answer[i] ? colors.correct : answer.includes(c) ? colors.present : colors.absent);
resultCss.forEach((color, i) => {
if (color === colors.present && keymap.get(guess[i]) === colors.correct) return;
keymap.set(guess[i], color);
});
history.forEach(([r, css]) => console.log(r, ...css));
console.log(`${result}%c\n`, ...resultCss, "");
qwerty.forEach((line, i) => {
const keys = [...line].map(c => `%c ${c.toUpperCase()} `);
const keysCss = [...line].map(c => keymap.get(c) ?? colors.none);
console.log(`${" ".repeat(i * 2)}${keys.join("")}`, ...keysCss);
});
if (guess !== answer) return challenge(answer, rest - 1, [...history, [result, resultCss]], keymap);
console.log(`\n${" ".repeat(9)}%c ${success[rest]} ` , colors.none);
};
challenge(answer, 6);
globalThis?.Deno?.exit(0);
@bellbind
Copy link
Author

To play as:

deno run --allow-net https://gist.githubusercontent.com/bellbind/ecf34020ebd8306494d7974eeb91ff3a/raw/wordle.js

or :

deno run --allow-net https://gist.githubusercontent.com/bellbind/ecf34020ebd8306494d7974eeb91ff3a/raw/wordle.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment