Skip to content

Commit

Permalink
Create 2013-detect-squares.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
nirajvenkat committed Jan 17, 2023
1 parent d9b73e8 commit a764cd5
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions rust/2013-detect-squares.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use std::collections::HashMap;

struct DetectSquares {
points: Vec<(i32, i32)>,
counts: HashMap<(i32, i32), i32>
}

impl DetectSquares {

fn new() -> Self {
Self {
points: vec![],
counts: HashMap::new()
}
}

fn add(&mut self, point: Vec<i32>) {
let p = (point[0], point[1]);
self.points.push(p);
*self.counts.entry(p).or_default() += 1;
}

fn count(&self, point: Vec<i32>) -> i32 {
let mut res = 0;
let (px, py) = (point[0], point[1]);
for (x, y) in self.points.iter() {
if (py - y).abs() != (px - x).abs() || *x == px || *y == py {
continue;
}
res += self.counts.get(&(*x, py)).unwrap_or(&0) * self.counts.get(&(px,*y)).unwrap_or(&0);
}

res
}
}

0 comments on commit a764cd5

Please sign in to comment.