Published at Feb 18 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.

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))
}
```

```
pub fn count(lines: &[&str]) -> u32 {
let mut result = 0;
let numlines = lines.len();
for row1 in 0 .. numlines - 1 {
for row2 in row1 + 1 .. numlines {
// Find the columns with a '+' in both row1 and row2.
// If there are more than two then we potentially have a
// triangle.
let col_matches = lines[row1].chars()
.zip(lines[row2].chars())
.enumerate()
.filter(|(_, (c1, c2))| *c1 == '+' && *c2 == '+')
.map(|(idx, (_, _))| idx)
.collect::<Vec<usize>>();
let matches = col_matches.len();
if matches < 2 {
continue;
}
// Check each pair of matches to see if it forms a rectangle
for idx1 in 0 .. matches - 1 {
for idx2 in idx1 + 1 .. matches {
let col1 = col_matches[idx1];
let col2 = col_matches[idx2];
if is_good_rect(&lines[row1..=row2], col1, col2) {
result += 1;
}
}
}
}
}
result
}
// Check for allowed characters along the sides of a rectangle
fn is_good_rect(lines: &[&str], col1: usize, col2: usize) -> bool {
// check top and bottom sides
for col in col1 + 1 ..= col2 - 1 {
let ch_top = lines[0].chars().nth(col).unwrap();
if ch_top != '+' && ch_top != '-' {
return false;
}
let ch_bottom = lines[lines.len() - 1].chars().nth(col).unwrap();
if ch_bottom != '+' && ch_bottom != '-' {
return false;
}
}
// check left and right sides
for row in 1 .. lines.len() - 1 {
let ch_left = lines[row].chars().nth(col1).unwrap();
if ch_left != '+' && ch_left != '|' {
return false;
}
let ch_right = lines[row].chars().nth(col2).unwrap();
if ch_right != '+' && ch_right != '|' {
return false;
}
}
true
}
```

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