Created
February 20, 2024 14:38
-
-
Save leanucci/c9a2547bdcfaddbdbd70129306455d43 to your computer and use it in GitHub Desktop.
Rendezvous with Cassidoo challenge 19-02-2024
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
# 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