Skip to content

[New practice exercise] Rectangles - #1133

Merged
colinleach merged 2 commits into
exercism:mainfrom
colinleach:rectangles
Aug 3, 2026
Merged

[New practice exercise] Rectangles#1133
colinleach merged 2 commits into
exercism:mainfrom
colinleach:rectangles

Conversation

@colinleach

Copy link
Copy Markdown
Contributor

I've gone with difficulty 4, but could be tempted to raise it to 5. Except Python (an outlier at 3), other tracks range between 4 and 7.

I think you may enjoy this one. Simple to state, but no single, obvious approach to solving it. My solution here borrows from Isaac's solution on Python, but changed so much that he would struggle to recognize it.

I hope my example is somewhat like Julia. I've been using other languages so much in the last few months, my early drafts were just an ugly mixture of R and Python.

@colinleach
colinleach requested a review from depial August 2, 2026 23:56
@depial

depial commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

This does look like an interesting one!

I'll have to give it a proper go tomorrow to get a better feeling for where I think the difficulty should be set.

@depial depial left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the end, I could see this being a difficulty 5. Your comment about approaches being open is on point. I initially wanted to solve it with a graph traversal method, but I ended up going a different way:

My solution
function validcorners(strmatrix, invalid = " |")
    corners = []
    for column in 1:size(strmatrix, 2)
        valid = Set()
        for i in 1:size(strmatrix, 1)-1
            if strmatrix[i, column] == '+'
                for j in i+1:size(strmatrix, 1)
                    occursin(strmatrix[j, column], invalid) && break
                    strmatrix[j, column] == '+' && push!(valid, (i, j))
                end
            end
        end
        push!(corners, valid)
    end
    corners
end

function rectangles(strings)
    isempty(strings) && return 0
    strmatrix = stack(strings, dims=1)
    vcorners = validcorners(strmatrix, " -")
    hcorners = validcorners(permutedims(strmatrix))

    count = 0
    for i in 1:length(hcorners)-1
        for j in i+1:length(hcorners)
            sides = intersect(hcorners[i], hcorners[j])
            if !isempty(sides)
                count += sum((i, j)  intersect(vcorners[side[1]], vcorners[side[2]]) for side in sides)
            end
        end
    end
    count
end

The overall strategy is to find the validly connected corners on the horizontal, find the validly connected corners on the vertical, and then compare (i.e. intersect) those two sets.

Your exemplar.jl looks Julian to my eyes if that's worth anything :D
I also get the feeling that your implementation is less opaque than mine. If I could offer one minor suggestion, it would be to make the function names for sides and top_bottom a bit more descriptive. Maybe something like valid_sides or connected_vert? But this is not important, it could just make it a bit easier on first reading.

Comment thread exercises/practice/rectangles/.meta/example.jl
@colinleach

Copy link
Copy Markdown
Contributor Author

I increased the difficulty to 5, but after some tinkering decided to leave the example unchanged. Your suggestions were good, it's just that I'm in a grumpy mood after getting back from the supermarket: headache not helped by 105F temperatures and a thunderstorm approaching (plus age, decrepitude and all the usual stuff...)

Meanwhile, I've been playing about with Crypto Square in Python and R. It looks like a useful one to add to Julia. Tomorrow...

Incidentally, I've no idea why GH thinks I started a review. I didn't, as far as I know.

@colinleach
colinleach merged commit 8575644 into exercism:main Aug 3, 2026
5 checks passed
@depial

depial commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Hope you feel better soon!

@colinleach
colinleach deleted the rectangles branch August 5, 2026 14:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants