Skip to content

Instantly share code, notes, and snippets.

@emanuele6
Last active September 27, 2023 22:51
Show Gist options
  • Save emanuele6/761cf2dc2c4786dbc5cff0d321fea1c2 to your computer and use it in GitHub Desktop.
Save emanuele6/761cf2dc2c4786dbc5cff0d321fea1c2 to your computer and use it in GitHub Desktop.
sharewordle
#!/bin/python3 --
import argparse
import datetime
import itertools
import sys
ap = argparse.ArgumentParser()
ap.add_argument("--number", "-N", type=int,
help="The number to use in the header instead of the "
"number of today's Wordle")
ap.add_argument("--hard", "-H", action="store_true",
help="Print an asterisk at the end of the header")
ap.add_argument("--light", "-l", action="store_true",
help="Use white instead of black for incorrect letters")
ap.add_argument("--words", "-w", action="store_true",
help="Show guesses to the output")
ap.add_argument("--discord", "-d", action="store_true",
help="Use discord :emojis: instead of raw emoji "
"characters, and print words with monospace and "
"spoiler formatting")
for i in range(1, 7):
ap.add_argument("guesses", nargs="?", action="append",
metavar=f"guess{i}", help="Incorrect guess")
ap.add_argument("solution", help="The correct word")
args = ap.parse_args()
if args.number is None:
number = (datetime.date.today() - datetime.date(2021, 6, 19)).days
else:
number = args.number
def word5(word):
if len(word) != 5:
sys.exit(f"{word!r} is not a five letter word!")
return word
solution = word5(args.solution).upper()
guesses = tuple(word5(g).upper() for g in args.guesses if g)
result = "X" if args.guesses[5] else str(len(guesses) + 1)
print(f"Wordle {number} {result}/6{'*' if args.hard else ''}\n")
def count_char(string, c):
return sum(1 for x in string if x == c)
for word in itertools.islice((*guesses, solution), 6):
colors = ""
for i, c in enumerate(word):
if c == solution[i]:
colors += ":green_square:" if args.discord else "\U0001f7e9"
elif count_char(word[:i], c) < count_char(solution, c):
colors += ":yellow_square:" if args.discord else \
"\U0001f7e8"
elif args.light:
colors += ":white_large_square:" if args.discord else \
"\u25a1"
else:
colors += ":black_large_square:" if args.discord else \
"\u2b1b"
print(f"{colors}" if not args.words else
f"{colors} {word}" if not args.discord else
f"{colors} ||`{word}`||")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment