AtCoder in Rust: ABC121 B
I have been trying competitive programming in both Rust and OCaml. As a small comparison, I solved ABC121 B — Can you solve this? in each language.
This post covers Rust. The second post covers OCaml.
The problem
For every row A_i, calculate its dot product with B, add C, and count the
rows whose result is positive:
A_i1 * B_1 + A_i2 * B_2 + ... + A_iM * B_M + C > 0Rust with cargo-compete and proconio
cargo-compete handles the contest
project and tests. I couldn't get the submission working though. proconio makes
parsing AtCoder input very pleasant.
use proconio::input;
fn can_solve(c: i32, b: &[i32], row: &[i32]) -> bool {
row.iter()
.zip(b)
.map(|(a_i, b_i)| a_i * b_i)
.sum::<i32>()
+ c
> 0
}
fn main() {
input! {
n: usize,
m: usize,
c: i32,
b: [i32; m],
rows: [[i32; m]; n],
}
let answer = rows
.iter()
.filter(|row| can_solve(c, &b, row))
.count();
println!("{answer}");
}
With rust I have to consider which arguments need &, .iter() and .sum::<i32>. I'm sure later I will have more problem related to memory and type. How do people get this right the first time?
With cargo-compete, the feedback loop is short:
cargo compete add abc121
cd abc121
cargo compete test b
Rust needs very little contest-specific boilerplate here. Next, I wanted to see whether I could get the same feeling in OCaml.