Last active
December 26, 2015 15:09
-
-
Save lacrosse/7170672 to your computer and use it in GitHub Desktop.
Codingame 2013-10-19
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
def char_weight(char) | |
case char | |
when "e", "a", "i", "o", "n", "r", "t", "l", "s", "u" | |
1 | |
when "d", "g" | |
2 | |
when "b", "c", "m", "p" | |
3 | |
when "f", "h", "v", "w", "y" | |
4 | |
when "k" | |
5 | |
when "j", "x" | |
8 | |
when "q", "z" | |
10 | |
end | |
end | |
def word_weight(word) | |
@weights[word] ||= word.chars.reduce(0){ |s,v| s + char_weight(v) } | |
end | |
def is_subset?(word1, word2) | |
word1 = word1.chars.to_a | |
word2.chars.each do |ch| | |
(first_index = word1.index(ch)) or next | |
word1.delete_at(first_index) | |
end | |
word1.size == 0 | |
end | |
@weights = {} | |
dictionary = Array.new(STDIN.gets.to_i){ STDIN.gets.strip } | |
letters = STDIN.gets.strip | |
puts dictionary.select{ |word| is_subset? word.dup, letters }.max{ |x,y| word_weight(x) <=> word_weight(y) } |
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
def shortest start, visited | |
unless start >= @board.size || visited.include?(start) | |
visited += [start] | |
case @board[start] | |
when "E" | |
visited.size - 1 | |
when "R", "S" | |
Array.new(6){ |n| shortest(start + n + 1, visited) }.reject{ |path| path == -1 }.min or -1 | |
else | |
shortest(start + @board[start].to_i, visited) | |
end | |
else | |
-1 | |
end | |
end | |
@board = Array.new(STDIN.gets.to_i){ STDIN.gets.strip } | |
if (shortest_for_board = shortest(@board.index("S"), [])) > 0 | |
puts shortest_for_board | |
else | |
puts "impossible" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment