Last active
January 25, 2022 16:40
-
-
Save tompng/bd946aa7b7c9d9058d2011bc200078c2 to your computer and use it in GitHub Desktop.
This file contains 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
allwords = File.read('./words.txt').split($/) | |
def select_words(words, query, result) | |
r = result.chars.map(&:to_i) | |
words.select do |w| | |
5.times.all? do |i| | |
c = query[i] | |
r[i] == (w[i] == c ? 2 : w[c] ? 1 : 0) | |
end | |
end | |
end | |
def query_result(query, word) | |
query.chars.zip(word.chars).map {|q, w| | |
q == w ? 2 : word[q] ? 1 : 0 | |
}.join | |
end | |
def find_query(words, queries) | |
words, queries = [words, queries].map{|ws| | |
ws.map{|s|s.downcase.bytes.map{_1-'a'.ord}} | |
} | |
word_sets = words.map do |w| | |
m = [0]*26 | |
w.each{m[_1]=1} | |
[w, m] | |
end | |
minresults = [] | |
minvalue = Float::INFINITY | |
n = 3**5 | |
queries.each do |q| | |
result = [0]*n | |
word_sets.each do |w, s| | |
ans = 0 | |
i = 0 | |
while i < 5 | |
c = q[i] | |
ans = ans * 3 + (c == w[i] ? 2 : s[c]) | |
i += 1 | |
end | |
result[ans] += 1 | |
end | |
cnt = result.max | |
if cnt < minvalue | |
minvalue = cnt | |
minresults = [] | |
end | |
if cnt == minvalue | |
minresults << [q, result.index(cnt)] | |
end | |
end | |
query_patterns = minresults.map{|w, r| | |
[ | |
w.map{(_1+'a'.ord).chr}.join, | |
(3**5+r).to_s(3)[1..] | |
] | |
} | |
strong_quereis = query_patterns.map(&:first) & words | |
[ | |
minvalue, | |
strong_quereis, | |
query_patterns | |
] | |
end | |
answer = allwords.sample | |
candidates = allwords | |
valid_queries = allwords.select { _1.chars.uniq.size == 5 } | |
cnt=0 | |
while true | |
n, queries1, queries2 = candidates == allwords ? [298, ['aloes'], [['aloes', '10000']]] : find_query(candidates, valid_queries) | |
query = candidates.size <= 2 ? candidates.sample : queries1.sample || queries2.sample[0] | |
res = query_result(query, answer) | |
candidates = select_words(candidates, query, res) | |
p [query, res, candidates.size] | |
cnt+=1 | |
break if res == '22222' | |
end | |
binding.irb | |
__END__ | |
5757単語の辞書だと | |
# クエリ 最悪パターンの絞り込み結果 最悪パターンのクエリの結果(0:なし,1:あり,2:一致) | |
# aloes 5757 → 298 10000 (このクエリを探すのに16秒くらいかかる。でも毎回これを使うのなら計算する必要がない) | |
# carny 298 → 27 02000 | |
# might 27 → 2 00000 (papaw,kappa の2択に絞り込まれる) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment