Avatar of McGittyHub

McGittyHub's solution

to Rectangles in the Rust Track

Published at Jul 13 2018 · 3 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code 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 wich 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 specfic 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.

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_equare() {
    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(map: &Vec<&str>) -> usize {
    (0..map.len()).fold(0usize, |acc, i| {
        acc +
        (0..map[i].len()).fold(0usize, |acc, j| if map[i].chars().nth(j).unwrap() == '+' {
            acc +
            map[i]
                .chars()
                .enumerate()
                .skip(j + 1)
                .take_while(|&(x, c)| c == '-' || c == '+')
                .filter(|&(x, c)| c == '+')
                .map(|(x, c)| x)
                .flat_map(|ii| {
                    map.iter()
                        .enumerate()
                        .skip(i + 1)
                        .map(|(x, s)| (x, s.chars().nth(ii).unwrap()))
                        .take_while(|&(x, c)| c == '|' || c == '+')
                        .filter(|&(x, c)| c == '+')
                        .map(|(x, c)| (ii, x))
                        .collect::<Vec<(usize, usize)>>()
                })
                .filter(|&(x, y)| {
                    map[y]
                        .chars()
                        .enumerate()
                        .take(x)
                        .collect::<Vec<_>>()
                        .into_iter()
                        .rev()
                        .take_while(|&(x, c)| c == '-' || c == '+')
                        .filter(|&(x, c)| c == '+')
                        .any(|(x, c)| x == j)
                })
                .filter(|&(x, y)| {
                    map.iter()
                        .enumerate()
                        .take(y)
                        .map(|(z, s)| (z, s.chars().nth(j).unwrap()))
                        .collect::<Vec<_>>()
                        .into_iter()
                        .rev()
                        .take_while(|&(x, c)| c == '|' || c == '+')
                        .filter(|&(x, c)| c == '+')
                        .any(|(x, c)| x == i)
                })
                .count()
        } else {
            acc
        })
    })
}

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of McGittyHub

got it passing tests l_0_l

Avatar of seshonaar

Impossible to understand, but absolutely beautiful in an artistic sense :) . I'm all smiles and I'm still looking at the code as if it's a good painting :)

Avatar of ajkavanagh

I agree; impossible to understand. This is the very definition of "write-only-code" :)

What can you learn from this solution?

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?