Last active
August 31, 2020 04:58
-
-
Save rabidaudio/6a91bbb0e36c47bac4fa567ef52caf87 to your computer and use it in GitHub Desktop.
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
require 'open-uri' | |
# I happened to watch [this video](https://www.youtube.com/watch?v=le5uGqHKll8) and it got | |
# me thinking about if the executioner cheats by changing the word during the course of the | |
# game, can the guesser still win? | |
# | |
# Results: | |
# 1: y, 24 | |
# 2: ky, 20 | |
# 3: qqv, 16 | |
# 4: vugg, 15 | |
# 5: huzzy, 15 | |
# 6: clucky, 8 | |
# 7: cupfuls, 6 | |
# 8: buckjump, 5 | |
# 9: boughpots, 5 | |
# 10: backtracks, 3 | |
# 11: clancularly, 3 | |
# 12: structurally, 3 | |
# 13: subculturally, 3 | |
# 14: pharyngobranch, 2 | |
# 15: arthropathology, 2 | |
# 16: astrophotography, 2 | |
# 17: photochromography, 2 | |
# 18: phenylacetaldehyde, 2 | |
# 19: lymphogranulomatous, 2 | |
# 20: cholecystenterostomy, 1 | |
# 21: antimaterialistically, 1 | |
# 22: electroencephalography, 1 | |
# 23: phenolsulphonephthalein, 0 | |
# 24: diphenylaminechlorarsine, 0 | |
# 25: microspectrophotometrical, 0 | |
# 27: electroencephalographically, 0 | |
# 28: antidisestablishmentarianism, 0 | |
# 29: cyclotrimethylenetrinitramine, 0 | |
# 31: dichlorodiphenyltrichloroethane, 0 | |
class Game | |
def initialize(word_length, possibilities) | |
@word_length = word_length | |
@possibilities = possibilities | |
@guessed = [] | |
@board = ("_" * word_length).chars | |
end | |
def play! | |
until complete? | |
select_word(make_guess) | |
end | |
[word, wrong_guesses] | |
end | |
def word | |
@board.join | |
end | |
def wrong_guesses | |
(@guessed - @board).count | |
end | |
def complete? | |
!@board.include?("_") | |
end | |
def outstanding_letters | |
('a'..'z').to_a - @guessed | |
end | |
def make_guess | |
# for each outstanding letter, select the letter which minimizes | |
# the number of remaining words if the word does not contain | |
# that letter. | |
outstanding_letters.min_by do |letter| | |
@possibilities.count do |word| | |
!word.include?(letter) | |
end | |
end | |
end | |
def update_board(guess) | |
@guessed << guess | |
# select the letter positions which maximize | |
# the number of remaining possibilities | |
word, @possibilities = @possibilities | |
.group_by { |word| word.chars.map { |c| c == guess ? guess : "_" } } | |
.max_by { |_, possibilities| possibilities.size } | |
@board = @board.zip(word).map(&:max) | |
end | |
end | |
URI.open("https://raw.githubusercontent.com/dwyl/english-words/master/words_alpha.txt", &:read) | |
.each_line.map(&:chomp) | |
.group_by(&:size) | |
.sort | |
.each do |word_length, possibilities| | |
board, score = Game.new(word_length, possibilities).play! | |
puts "#{word_length}: #{board}, #{score}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment