Skip to content

Instantly share code, notes, and snippets.

@yyaammaa
Created January 25, 2022 08:57
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 yyaammaa/cd63e163162fd9de4f06e27d6fc93b4a to your computer and use it in GitHub Desktop.
Save yyaammaa/cd63e163162fd9de4f06e27d6fc93b4a to your computer and use it in GitHub Desktop.
// 0: 黒, 1: 黄色, 2: 緑
const THE_ANSWER = ['12220', '02222', '20222', '22202', '22210', '22022']
const WORDLE_WORDS = ['cigar', 'rebut', 'sissy', 'humph'] // 1万のリスト。省略
const SHORTEN_WORDS = ['cigar', 'rebut', 'sissy', 'humph'] // 2000のリスト。省略
const extractGreen = (answers) => {
const positions = []
for (let i = 0; i < answers.length; i++) {
const answer = answers[i]
const p = []
for (let j = 0; j < answer.length; j++) {
if (answer[j] === '2') {
p.push(j)
}
}
positions.push(p)
}
return positions.filter((p) => p.length > 0)
}
const extractYellow = (answers) => {
const positions = []
for (let i = 0; i < answers.length; i++) {
const answer = answers[i]
const p = []
let hasOne = false
for (let j = 0; j < answer.length; j++) {
if (answer[j] === '1') {
hasOne = true
}
p.push({
index: j,
type: answer[j],
})
}
if (hasOne) {
positions.push(p)
}
}
return positions
}
const hasSameWordInPosition = (word1, word2, position) => {
if (position.length === 0) {
return false
}
let ok = true
for (const p of position) {
if (word1[p] !== word2[p]) {
ok = false
}
}
return ok
}
const getMatchedWordsOfYellow = (word, words, position) => {
const wArr = word.split('')
let count = 0
for (const v of position) {
if (v.type === '1') {
count++
}
}
if (count === 0) {
throw new Error('count is zero')
}
const perm = getPermutations(wArr, count)
const permValues = perm.map((p) => {
const arr = []
const pp = [...p]
for (const v of position) {
if (v.type === '1') {
arr.push({
index: v.index,
letter: pp[0],
})
pp.shift()
}
}
return arr
})
const filtered = permValues.filter((p) => {
let ok = true
for (let i = 0; i < p.length; i++) {
const ind = p[i].index
if (word[ind] === p[i].letter) {
ok = false
break
}
}
return ok
})
const matchedWords = []
for (const w of words) {
if (w === word) {
continue
}
let matched = false
for (const pv of filtered) {
const count = pv.filter((v) => {
return w[v.index] === v.letter
}).length
if (count === pv.length) {
matched = true
break
}
}
if (matched) {
matchedWords.push(w)
}
}
return matchedWords
}
const getMatchedWordsOfGreen = (word, words, position) => {
const matchedWords = []
for (const w of words) {
if (w === word) {
continue
}
if (hasSameWordInPosition(word, w, position)) {
matchedWords.push(w)
}
}
return matchedWords
}
const getPermutations = (letters, size) => {
const arrSize = size ? letters.length : size
const result = []
const doSome = (arg) => {
if (arg.arr.length === 0 || arg.s.length === arrSize) {
result.push(arg.s)
return
}
for (const a of arg.arr) {
const copy = [...arg.arr]
copy.splice(copy.indexOf(a), 1)
doSome({
s: arg.s.concat(a),
arr: copy,
})
}
}
doSome({ s: [], arr: letters })
return result
}
// --
const filteredAnswers = THE_ANSWER.filter((a) => {
return a !== '00000' && a !== '22222'
})
const green = extractGreen(filteredAnswers)
const yellow = extractYellow(filteredAnswers)
const possibleAnswers = []
for (const word of WORDLE_WORDS) {
const matchedWordsArray = []
for (const p2 of green) {
const matchedWords = getMatchedWordsOfGreen(word, WORDLE_WORDS, p2)
if (matchedWords.length > 0) {
matchedWordsArray.push(matchedWords)
}
}
if (matchedWordsArray.length !== green.length) {
continue
}
const matchedWordsArrayForYellow = []
for (const pos of yellow) {
const matchedWordsForOne = getMatchedWordsOfYellow(word, WORDLE_WORDS, pos)
if (matchedWordsForOne.length > 0) {
matchedWordsArrayForYellow.push(matchedWordsForOne)
}
}
if (matchedWordsArrayForYellow.length === yellow.length) {
possibleAnswers.push(`${word}`)
}
}
console.log(JSON.stringify(possibleAnswers))
console.log(`${WORDLE_WORDS.length} to ${possibleAnswers.length}`)
const cheat = possibleAnswers.filter((p) => {
return SHORTEN_WORDS.includes(p)
})
console.log(JSON.stringify(cheat))
console.log(cheat.length)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment