Skip to content

Instantly share code, notes, and snippets.

@veer66
Created December 28, 2014 15:09
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 veer66/8c5db123c0d8e10a332c to your computer and use it in GitHub Desktop.
Save veer66/8c5db123c0d8e10a332c to your computer and use it in GitHub Desktop.
pw = nil
pc = 0
$stdin.each do |line|
line.chomp!
w, year, c1, c2 = line.split(/\t/)
if not pw.nil? and pw != w
puts "#{pw}\t#{pc}"
pw = nil
end
c1 = c1.to_i
if pw.nil?
pw = w
pc = c1
else
pc += c1
end
end
if not pw.nil?
puts "#{pw}\t#{pc}"
end
use std::io;
fn main() {
let mut pw: String = String::new();
let mut pc: int = 0;
let mut started = false;
for line in io::stdin().lock().lines() {
let line = line.unwrap();
let mut split = line.split_str("\t");
let _w = split.next().unwrap();
let w = String::from_str(_w);
split.next();
let _c:Option<int> = split.next().unwrap().parse();
let c = _c.unwrap();
if started && pw != w {
println!("{}\t{}", pw, pc);
started = false;
}
if !started {
started = true;
pw = w;
pc = c;
} else {
pc += c;
}
}
if started {
println!("{}\t{}", pw, pc);
}
}
@veer66
Copy link
Author

veer66 commented Dec 29, 2014

Thank @awesomeintheory and @bstrie. Now my program written in #Rust very fast just by adding "--release" to cargo. ^^

@veer66
Copy link
Author

veer66 commented Dec 29, 2014

Ruby

real 90m47.088s
user 94m15.470s
sys 1m20.872s

Rust

real 11m9.995s
user 13m12.904s
sys 0m59.891s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment