Skip to content

Instantly share code, notes, and snippets.

@tokland
Created March 15, 2011 20:34
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 tokland/871417 to your computer and use it in GitHub Desktop.
Save tokland/871417 to your computer and use it in GitHub Desktop.
Solution for codejam 2010_1b A. File Fix-it (Ruby)
#!/usr/bin/ruby
# http://code.google.com/codejam/contest/dashboard?c=635101#s=p0
#
# Author: tokland@gmail.com
class Problem
# Create a directory (as array of slugs) in an existing filesystem and
# return the final filesytem and the total creation cost.
def self.create_directory(filesystem, slugs)
if (head = slugs.first)
insertion = create_directory(filesystem[head] || {}, slugs.drop(1))
new_cost = insertion[:cost] + (filesystem.has_key?(head) ? 0 : 1)
new_filesystem = filesystem.merge(head => insertion[:filesystem])
{:filesystem => new_filesystem, :cost => new_cost}
else
{:filesystem => filesystem, :cost => 0}
end
end
# Take an array of pre-existing directories and return the cost
# (number of new sub-directories) in order to create some other directories.
def self.create_directories(existing, to_create)
initial = {:filesystem => {}, :cost => 0}
(existing + to_create).each_with_index.inject(initial) do |input, (directory, idx)|
result = create_directory(input[:filesystem], directory[1..-1].split("/"))
new_cost = input[:cost] + (idx >= existing.size ? result[:cost] : 0)
{:filesystem => result[:filesystem], :cost => new_cost}
end
end
def self.solve(problem_lines)
1.upto(problem_lines[0].to_i).inject(problem_lines.drop(1)) do |lines, idx|
n, m = lines[0].split.map(&:to_i)
directories = lines.drop(1).map(&:strip).take(n + m)
cost = create_directories(directories[0...n], directories[n..-1])[:cost]
puts "Case ##{idx}: #{cost}"
lines.drop(n + m + 1)
end
end
end
Problem.solve(File.readlines(ARGV[0] || "A-small-practice.in"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment