# ivfranco's solution

## to Rectangles in the Rust Track

Published at Nov 27 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

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).

## Rust Installation

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

## Writing the Code

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

## Feedback, Issues, Pull Requests

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.

## Submitting Incomplete Solutions

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

### rectangles.rs

``````extern crate rectangles;

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 grid = Grid::new(lines);
let vertices: Vec<Pos> = grid.vertices().collect();
let mut count = 0u32;

for origin in vertices.iter() {
count += vertices
.iter()
.filter(|target| grid.is_rect(*origin, **target))
.count() as u32;
}

count
}

type Pos = (usize, usize);
type Tile = u8;

const V_PIPE: Tile = b'|';
const H_PIPE: Tile = b'-';
const CROSS: Tile = b'+';

fn at_bottom_right((x0, y0): Pos, (x1, y1): Pos) -> bool {
x0 < x1 && y0 < y1
}

struct Grid<'a> {
lines: &'a [&'a str],
}

impl<'a> Grid<'a> {
fn new(lines: &'a [&'a str]) -> Self {
Grid { lines }
}

fn get(&self, x: usize, y: usize) -> Tile {
self.lines[y].as_bytes()[x]
}

fn width(&self) -> usize {
self.lines.get(0).map_or(0, |row| row.len())
}

fn height(&self) -> usize {
self.lines.len()
}

fn tiles(&'a self) -> impl Iterator<Item = (Pos, Tile)> + 'a {
(0..self.height())
.flat_map(move |y| (0..self.width()).map(move |x| ((x, y), self.get(x, y))))
}

fn vertices(&'a self) -> impl Iterator<Item = Pos> + 'a {
self.tiles()
.filter(|(_, tile)| *tile == CROSS)
.map(|(pos, _)| pos)
}

fn is_rect(&self, (x0, y0): Pos, (x1, y1): Pos) -> bool {
let vertices = self.get(x0, y0) == CROSS
&& self.get(x1, y1) == CROSS
&& self.get(x0, y1) == CROSS
&& self.get(x1, y0) == CROSS;
let top = (x0 + 1..x1)
.map(|x| self.get(x, y0))
.all(|tile| tile == H_PIPE || tile == CROSS);
let bottom = (x0 + 1..x1)
.map(|x| self.get(x, y1))
.all(|tile| tile == H_PIPE || tile == CROSS);
let left = (y0 + 1..y1)
.map(|y| self.get(x0, y))
.all(|tile| tile == V_PIPE || tile == CROSS);
let right = (y0 + 1..y1)
.map(|y| self.get(x1, y))
.all(|tile| tile == V_PIPE || tile == CROSS);

at_bottom_right((x0, y0), (x1, y1)) && vertices && top && bottom && left && right
}
}``````