Skip to content

Instantly share code, notes, and snippets.

@leanucci
Created February 20, 2024 14:38
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 leanucci/c9a2547bdcfaddbdbd70129306455d43 to your computer and use it in GitHub Desktop.
Save leanucci/c9a2547bdcfaddbdbd70129306455d43 to your computer and use it in GitHub Desktop.
Rendezvous with Cassidoo challenge 19-02-2024
# frozen_string_literal: true
class HeterogramProductMaximizer
attr_accessor :words
def initialize(words)
@words = words
@product = 0
end
def find!
words.each do |word|
le = longest_exclusive(word)
@product = [@product, word.length * le.length].max
end
puts [@product, '#', words.join(', ')].join(' ')
end
def longest_exclusive(word)
@winner = ''
words.each do |candidate|
@winner = candidate if heterogram?(word, candidate)
end
@winner
end
def heterogram?(word, candidate)
candidate.length > @winner.length &&
word.chars.intersection(candidate.chars).empty?
end
end
lists = [
%w[fish fear boo egg cake abcdef],
%w[cat dog fish bird elephant],
%w[moon sun star planet galaxy],
%w[apple banana orange grape kiwi]
].each { |list| HeterogramProductMaximizer.new(list).find! }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment