Published at Jun 18 2019
·
1 comment

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

```
const HORIZONTAL: char = '-';
const VERTEX: char = '+';
const VERTICAL: char = '|';
type Vertex = (usize, usize);
type Field<'a> = &'a [&'a str];
fn opposite_corners_of_rectangle(field: Field, v0: &Vertex, v1: &Vertex) -> bool {
v0.0 < v1.0 && v0.1 < v1.1 && connected(field, v0, v1)
}
fn connected(field: Field, &(y0, x0): &Vertex, &(y1, x1): &Vertex) -> bool {
field[y0][x0..x1]
.chars()
.zip(field[y1][x0..x1].chars())
.all(|(c0, c1)| is_horizontal(c0) && is_horizontal(c1))
&& field[y0..y1]
.iter()
.map(|row| (row.chars().nth(x0).unwrap(), row.chars().nth(x1).unwrap()))
.all(|(c0, c1)| is_vertical(c0) && is_vertical(c1))
&& field[y0].chars().nth(x1) == Some(VERTEX)
&& field[y1].chars().nth(x0) == Some(VERTEX)
}
fn is_horizontal(c: char) -> bool {
c == VERTEX || c == HORIZONTAL
}
fn is_vertical(c: char) -> bool {
c == VERTEX || c == VERTICAL
}
pub fn count(field: Field) -> usize {
println!("{:#?}", field);
// Collect all vertices
let vertices = field
.iter()
.enumerate()
.flat_map(|(row_id, line)| {
line.chars()
.enumerate()
.filter(|(_, c)| *c == VERTEX)
.map(move |(column_id, _)| (row_id, column_id))
})
.collect::<Vec<_>>();
vertices
.iter()
.flat_map(|v0| {
vertices
.iter()
.filter(move |v1| opposite_corners_of_rectangle(field, v0, v1))
})
.count()
}
```

Do not judge.

Unreadable solution, but it works.

1) Find the next vertex to the left that is connected;

2) Find the next pair of vertices below that are connected to the other two.

3) If these two are also connected to eachother, then we've found a rectangle. Repeat step

4) Repeat step two if there's another pair. Else, continue.

5) Repeat step 1, until there are no other vertices left.

Now you've found how many rectangles there are.

In other words, if "go to" means "follow the line"

1) Take a vertex

2) Go left until the next one

3) Go down from both until the next pair of vertices

4) Go from the bottom-left one to the bottom-right one

5) Now you've a square.

6) Go even lower (go on with step 3, starting from where you left during the last iteration)

7) Go even more left (go on with step 2)

8) Take another vertex (go on with step 1)

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

Solution number 2 is much more readable. Heavily inspired by kstep's solution.

Shorter than the first one, functional, passes all tests.

Could've been shorter if it wasn't

`cargo fmt`

'd.