# SRM 739 Div II - C++ vs Haskell

SRM 739 was held on October 10th. The original editorial can be found here. Since I was participating and I am also currently learning Haskell, I decided to solve all the problems in Haskell as an exercise and then I found it interesting to compare it with C++ solutions.

## HungryCowsEasy - Div 2 Easy

Short problem statement: On a straight line cows and barns are located, and each of them is assigned a position on that line (x coordinate). For each cow determine the barn that is closest to it. Among two equally close barns choose the one with the lower position.

Due to modest input size limits, here it is more than enough to employ a straightforward brute-force solution. For each cow we can examine all the barns and see which one is the closest. If two barns are equally close, one with the lower position is preferred.

### Comparison

Language Lines 100 runs
C++ 30 TBD

Both solutions are short, but I really like how Haskell solution is more readable while being shorter when compared to C++ solution. In Haskell we could very easily create pairs `(idx, value)` and did not have to explicitly handle them as we did in C++.

## ForumPostMedium - Div 2 Medium

Short problem statement: We are given two timestamps in `hh:mm:ss` format, time when the forum post was published and current time. We have to determine how long ago was a post published and express it through the appropriate string (e.g. “a few seconds ago”, “X minutes ago”, …).

This problem can actually be solved almost entirely analytically - we just transform both timestamps to seconds, get the difference and then transform it from seconds back to the required format (hour,min and sec diff). There is on case to be aware of - if current time is before the publishing time, it means the post was published the day before.

### Comparison

Language Lines 100 runs
C++ 43 TBD

Not such a huge difference here! What is interesting is that both solutions are actually very similar, since this problem can be solved in a completely analytical way, we don’t need even one for loop in the C++ solution (except in the helper method to get time in seconds from the original format).
It was actually quite straightforward to write a Haskell solution from the C++ solution.

## CheckPolygon - Div 2 Hard

Short problem statement: We are given points respectively describing polygon in a 2D space. The task is to determine whether a polygon is a regular polygon, which means it satisfies certain conditions, such as that two adjacent segments are not collinear and none two segments are intersecting.
If the polygon is regular we should also calculate and return its area.

Input size limits are also not a problem here, so we just need to check if polygon satisfies all the requirements. This task was mostly about geometry - determining whether two segments interesect with each other, checking if point lines on a segment, calculating area of the polygon etc. It was also important to keep all the calculations in integers so we don’t get into precision problems.

Here are some links I found useful in understanding the formulas employed in the solution:

• Orientation of 3 ordered points - link
• Area of a polygon (shoelace formula) - link
• Check if two segments intersect - link
• Check if three points are collinear - link