Skip to content

Instantly share code, notes, and snippets.

@mururu mururu/soup.rb
Created Jan 25, 2012

Embed
What would you like to do?
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
You can’t perform that action at this time.