Last active
December 23, 2024 06:36
-
-
Save rjvdw/f3d21b8243ee215df8d0420425d7dfd6 to your computer and use it in GitHub Desktop.
Solutions for NYT games
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
#!/usr/bin/env java --source 23 --enable-preview | |
import java.util.Scanner; | |
/// Find all words in a word list that fit the pattern for the | |
/// [NYT Letter Boxed game](https://www.nytimes.com/puzzles/letter-boxed). | |
/// | |
/// Usage: | |
/// | |
/// cat word-list.txt | ./LetterBoxed abc def hij klm | |
void main(String[] groups) { | |
for (int i = 0; i < groups.length; i += 1) { | |
groups[i] = groups[i].toLowerCase(); | |
} | |
Scanner input = new Scanner(System.in); | |
lines: while (input.hasNext()) { | |
var line = input.nextLine(); | |
int prev = -1; | |
characters: for (char ch : line.toLowerCase().toCharArray()) { | |
for (int i = 0; i < groups.length; i += 1) { | |
if (i == prev) continue; | |
if (groups[i].indexOf(ch) != -1) { | |
prev = i; | |
continue characters; | |
} | |
} | |
continue lines; | |
} | |
System.out.println(line); | |
} | |
} |
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
#!/usr/bin/env java --source 23 --enable-preview | |
import java.util.Scanner; | |
/// Find all words in a word list that fit the pattern for the | |
/// [NYT Spelling Bee game](https://www.nytimes.com/puzzles/spelling-bee). | |
/// | |
/// Usage: | |
/// | |
/// cat word-list.txt | ./SpellingBee a bcdefg | |
void main(String[] groups) { | |
char mandatory = groups[0].toLowerCase().charAt(0); | |
char[] letters = groups[1].toLowerCase().toCharArray(); | |
Scanner input = new Scanner(System.in); | |
lines: while (input.hasNext()) { | |
var line = input.nextLine(); | |
if (line.length() < 4) { | |
continue; | |
} | |
boolean hasMandatory = false; | |
characters: for (char ch : line.toLowerCase().toCharArray()) { | |
if (ch == mandatory) { | |
hasMandatory = true; | |
} else if (!contains(letters, ch)) { | |
continue lines; | |
} | |
} | |
if (hasMandatory) { | |
System.out.println(line); | |
} | |
} | |
} | |
private boolean contains(char[] haystack, char needle) { | |
for (char ch : haystack) { | |
if (ch == needle) { | |
return true; | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment