Skip to content

Instantly share code, notes, and snippets.

@mururu
Created January 25, 2012 06:22
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 mururu/1675060 to your computer and use it in GitHub Desktop.
Save mururu/1675060 to your computer and use it in GitHub Desktop.
Alphabet Soup - Facebook Hacker Cup 2012 Qualification Round
# coding: utf-8
class Soup
def initialize(input)
lines = []
open(input) do |f|
lines = f.readlines.map{|l|l.chomp!}
end
@num = lines.shift.to_i
@case = lines
end
def self.conc(line)
p tokens = line.split(//).reject{|v| !'HACKERUP'.include?(v)}
h = {:H=>0,:A=>0,:C=>0,:K=>0,:E=>0,:R=>0,:U=>0,:P=>0}
tokens.each do |v|
if v == 'H'
h[:H] += 1
elsif v == 'A'
h[:A] += 1
elsif v == 'C'
h[:C] += 1
elsif v == 'K'
h[:K] += 1
elsif v == 'E'
h[:E] += 1
elsif v == 'R'
h[:R] += 1
elsif v == 'U'
h[:U] += 1
elsif v == 'P'
h[:P] += 1
end
end
h[:C] /= 2
h.values.min
end
def set_ans
@ans = @case.map{|v| p v; Soup.conc v}
end
def put_ans(output)
self.set_ans
open(output, "w") do |f|
@ans.each_with_index do |ans, i|
p "Case #" + (i+1).to_s + ": " + ans.to_s
f.puts("Case #" + (i+1).to_s + ": " + ans.to_s)
end
end
end
end
soup = Soup.new(ARGV[0])
soup.put_ans(ARGV[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment