Published at Apr 06 2019
·
0 comments

Instructions

Test suite

Solution

Count the rectangles in an ASCII diagram like the one below.

```
+--+
++ |
+-++--+
| | |
+--+--+
```

The above diagram contains 6 rectangles:

```
+-----+
| |
+-----+
```

```
+--+
| |
| |
| |
+--+
```

```
+--+
| |
+--+
```

```
+--+
| |
+--+
```

```
+--+
| |
+--+
```

```
++
++
```

You may assume that the input is always a proper rectangle (i.e. the length of every line equals the length of the first line).

Refer to the exercism help page for Rust installation and learning resources.

Execute the tests with:

```
$ cargo test
```

All but the first test have been ignored. After you get the first test to
pass, open the tests source file which is located in the `tests`

directory
and remove the `#[ignore]`

flag from the next test and get the tests to pass
again. Each separate test is a function with `#[test]`

flag above it.
Continue, until you pass every test.

If you wish to run all tests without editing the tests source file, use:

```
$ cargo test -- --ignored
```

To run a specific test, for example `some_test`

, you can use:

```
$ cargo test some_test
```

If the specific test is ignored use:

```
$ cargo test some_test -- --ignored
```

To learn more about Rust tests refer to the online test documentation

Make sure to read the Modules chapter if you haven't already, it will help you with organizing your files.

After you have solved the exercise, please consider using the additional utilities, described in the installation guide, to further refine your final solution.

To format your solution, inside the solution directory use

```
cargo fmt
```

To see, if your solution contains some common ineffective use cases, inside the solution directory use

```
cargo clippy --all-targets
```

Generally you should submit all files in which you implemented your solution (`src/lib.rs`

in most cases). If you are using any external crates, please consider submitting the `Cargo.toml`

file. This will make the review process faster and clearer.

The exercism/rust repository on GitHub is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help!

If you want to know more about Exercism, take a look at the contribution guide.

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

```
use rectangles::count;
#[test]
fn test_zero_area_1() {
let lines = &[];
assert_eq!(0, count(lines))
}
#[test]
#[ignore]
fn test_zero_area_2() {
let lines = &[""];
assert_eq!(0, count(lines))
}
#[test]
#[ignore]
fn test_empty_area() {
let lines = &[" "];
assert_eq!(0, count(lines))
}
#[test]
#[ignore]
fn test_one_rectangle() {
let lines = &[
"+-+",
"| |",
"+-+",
];
assert_eq!(1, count(lines))
}
#[test]
#[ignore]
fn test_two_rectangles_no_shared_parts() {
let lines = &[
" +-+",
" | |",
"+-+-+",
"| | ",
"+-+ "
];
assert_eq!(2, count(lines))
}
#[test]
#[ignore]
fn test_five_rectangles_three_regions() {
let lines = &[
" +-+",
" | |",
"+-+-+",
"| | |",
"+-+-+"
];
assert_eq!(5, count(lines))
}
#[test]
#[ignore]
fn rectangle_of_height_1() {
let lines = &[
"+--+",
"+--+"
];
assert_eq!(1, count(lines))
}
#[test]
#[ignore]
fn rectangle_of_width_1() {
let lines = &[
"++",
"||",
"++"
];
assert_eq!(1, count(lines))
}
#[test]
#[ignore]
fn unit_square() {
let lines = &[
"++",
"++"
];
assert_eq!(1, count(lines))
}
#[test]
#[ignore]
fn test_incomplete_rectangles() {
let lines = &[
" +-+",
" |",
"+-+-+",
"| | -",
"+-+-+"
];
assert_eq!(1, count(lines))
}
#[test]
#[ignore]
fn test_complicated() {
let lines = &[
"+------+----+",
"| | |",
"+---+--+ |",
"| | |",
"+---+-------+"
];
assert_eq!(3, count(lines))
}
#[test]
#[ignore]
fn test_not_so_complicated() {
let lines = &[
"+------+----+",
"| | |",
"+------+ |",
"| | |",
"+---+-------+"
];
assert_eq!(2, count(lines))
}
#[test]
#[ignore]
fn test_large_input_with_many_rectangles() {
let lines = &[
"+---+--+----+",
"| +--+----+",
"+---+--+ |",
"| +--+----+",
"+---+--+--+-+",
"+---+--+--+-+",
"+------+ | |",
" +-+"
];
assert_eq!(60, count(lines))
}
```

```
use itertools::Itertools;
#[derive(Debug, Clone, PartialEq)]
struct Point {
x: usize,
y: usize,
}
pub fn count(lines: &[&str]) -> u32 {
let char_grid: Vec<Vec<char>> = lines.iter().map(|line| line.chars().collect()).collect();
if char_grid.is_empty() || char_grid[0].is_empty() {
return 0;
}
// All the potential corners of squares
let points: Vec<(usize, usize)> = (0..char_grid.len())
.cartesian_product(0..char_grid[0].len())
.filter(|(y, x)| char_grid[*y][*x] == '+')
.collect();
// Every set of corners that is connected by lines
// (only check ones that either share x or y values)
let (vertical_connections, horizontal_connections): (Vec<(Point, Point)>, Vec<(Point, Point)>) =
points
.iter()
.cloned()
.enumerate()
.flat_map(|(i, start_point)| {
points
.iter()
.skip(i + 1)
.cloned()
.map(|end_point| (start_point, end_point))
.collect::<Vec<((usize, usize), (usize, usize))>>()
})
.filter(|((y1, x1), (y2, x2))| x1 == x2 || y1 == y2)
.map(|((y1, x1), (y2, x2))| (Point { x: x1, y: y1 }, Point { x: x2, y: y2 }))
.partition(|(p1, p2)| p1.x == p2.x);
// Filter out the vertical connections, so we only have the ones that are connected
let vertical_connections: Vec<(Point, Point)> = vertical_connections
.iter()
.cloned()
.filter(|(start, end)| {
(start.y + 1..end.y).all(|y| match char_grid[y][start.x] {
'|' | '+' => true,
_ => false,
})
})
.collect();
// Filter out the horizontal connections, so we only have the ones that are connected
let horizontal_connections: Vec<(Point, Point)> = horizontal_connections
.iter()
.cloned()
.filter(|(start, end)| {
(start.x + 1..end.x).all(|x| match char_grid[start.y][x] {
'-' | '+' => true,
_ => false,
})
})
.collect();
// Check connections to see which ones make a square
vertical_connections
.iter()
.tuple_combinations()
.filter(|((a, b), (c, d))| a.y == c.y && b.y == d.y)
.filter(|((a, b), (c, d))| {
horizontal_connections.iter().any(|(e, f)| a == e && c == f)
&& horizontal_connections.iter().any(|(e, f)| b == e && d == f)
})
.count() as u32
}
```

```
[package]
edition = "2018"
name = "rectangles"
version = "1.1.0"
[dependencies]
itertools = "0.8.0"
```

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,048 exercises across 51 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments