Created
April 5, 2012 14:45
-
-
Save koko1000ban/2311588 to your computer and use it in GitHub Desktop.
template for google codejam with Rust
This file contains hidden or 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
use std; | |
import core::io; | |
import io::reader; | |
import io::reader_util; | |
import std::sort; | |
impl read_util for reader{ | |
fn read_valid_char() -> char { | |
let mut c; | |
do { | |
c = self.read_char(); | |
} while c == '\n' || c == ' '; | |
ret c; | |
} | |
fn read_valid_line() -> str{ | |
let mut line; | |
do { | |
line = self.read_line(); | |
} while line == ""; | |
ret line; | |
} | |
fn read_uint() -> uint{ | |
let raw_uint = self.read_valid_line(); | |
ret option::get(uint::from_str(raw_uint)); | |
} | |
fn read_int() -> int{ | |
ret option::get(int::from_str(self.read_line())); | |
} | |
fn read_uints() -> [uint]{ | |
fn to_uint(&&s: str) -> uint{ | |
ret option::get(uint::from_str(s)); | |
} | |
let line = self.read_line(); | |
ret vec::map(str::split_char(line, ' '), to_uint); | |
} | |
fn read_ints() -> [int]{ | |
fn to_int(&&s: str) -> int{ | |
ret option::get(int::from_str(s)); | |
} | |
let line = self.read_valid_line(); | |
ret vec::map(str::split_char(line, ' '), to_int); | |
} | |
} | |
#[doc="input data type"] | |
type input = { | |
mut index: uint, | |
w: uint, | |
h: uint, | |
board: [str] | |
}; | |
#[doc="build input data from stdin"] | |
fn fetch(stdin: io::reader) -> input { | |
let s = stdin.read_uint(); | |
let v1 = stdin.read_ints(); | |
let v2 = stdin.read_ints(); | |
ret { | |
mut index: 0u, | |
s : s, | |
v1: v1, | |
v2: v2 | |
}; | |
} | |
#[doc="output status"] | |
enum out_status { | |
ready, | |
start, | |
solved((uint, str)) | |
} | |
fn solve(in: input) -> str { | |
log(error, #fmt("idx.%u start solved", in.index)); | |
let mut v1 = in.v1; | |
let mut v2 = in.v2; | |
fn le(&&a: int, &&b: int) -> bool { int::le(a, b) } | |
fn ge(&&a: int, &&b: int) -> bool { int::ge(a, b) } | |
v1 = sort::merge_sort(le, v1); | |
v2 = sort::merge_sort(ge, v2); | |
fn times(&&x: int, &&y: int) -> int { ret x * y; } | |
fn sum(&&accum: int, &&a: int) -> int { ret accum+a; } | |
ret int::to_str(vec::foldl(0, vec::map2(v1, v2, times), sum), 10u); | |
} | |
fn main() { | |
let stdin = io::stdin(); | |
let n = stdin.read_uint(); | |
let mut inputs : [input] = []; | |
let mut outputs = vec::to_mut(vec::from_elem(n, "")); | |
let port = comm::port(); | |
let chan = comm::chan(port); | |
log(error, #fmt("n: %u", n)); | |
uint::range(0u, n) {|i| | |
let in = fetch(stdin); | |
in.index = i; | |
inputs += [in]; | |
task::spawn{|| | |
build_actor(in, chan); | |
} | |
} | |
iter::repeat(n) {|| | |
alt check comm::recv(port){ | |
solved((idx, res)) { | |
outputs[idx] = res; | |
} | |
_ { | |
log(error, "what?"); | |
} | |
} | |
} | |
vec::iteri(outputs){|i, res| | |
io::println(#fmt("Case #%u: %s", i+1u, res)); | |
} | |
} | |
fn build_actor(in: input, chan: comm::chan<out_status>) { | |
let res = solve(in); | |
comm::send(chan, solved((in.index, res))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment