# Mountain Viewing

# Problem description

Chalkolate Resort is commissioning a mural of the mountain range behind the resort as a 2D projection.
The mountain range has `N`  mountains that span up to `R` units from left to right and up to `R` units from top to bottom.
Each mountain has slope 1 (so a mountain of height 1 spans 1 unit at its base, a mountain of height 2 spans 3 units at its base, and so forth) and is described by the height of the mountain (`H`), the horizontal position of the peak (`X`), and the distance of the mountain (`D`) from the resort.
You may assume that mountains at different distances do not physically overlap and that every mountain fits within the mural.

Since this is an expensive venture, you have been tasked with producing a proof of concept before the resort commits to the project.

You will be provided as input:
```
N R
H X D
H X D
...
```
where **1 ≤ `N` ≤ 10⁶, 1 ≤ `R` ≤ 10⁶ - 1, 1 ≤ `H` ≤ 10⁶, 1 ≤ `X` ≤ `R`, and 1 ≤ `D` ≤ 10⁶**.

For example:
```
3 6
3 3 3
2 5 4
1 4 2
```
produces the mountain range
```
3 |     1
2 |   1 1 1 2
1 | 1 1 1 3 1 2
--+------------
  | 1 2 3 4 5 6
```
Notably, since Mountain 3 is in front of Mountains 1 and 2, it is not obscured by the much larger Mountain 1.
Mountain 2, meanwhile, is partially obscured by Mountain 1.

# Part 1: Viewing

In the example above, all three mountains are visible.
But if Mountain 2's peak were at position `4` instead of `5`, it would be completed obscured:

```
3 6
3 3 3
2 4 4
1 4 2
```
the mountain range would look like
```
3 |     1
2 |   1 1 1
1 | 1 1 1 3 1
--+------------
  | 1 2 3 4 5 6
```

Given a description of a mountain range as above, output how many distinct mountains are visible on the mural.

Test your solution against this input:

3 mountains, R=20

Input:
```
3 20
3 10 3
4 11 1
5 7 2
```

Output:
```
2
```

M1, the farthest back, is hidden in the shadow of M2.
M3 is the closest and always visible.
If M2's `X` were 12 instead of 11, M1's peak would be visible.

# Part 2: Slopes

Due to a recent earthquake, the Chalkfornia Mountains are no longer right triangles, but have varying, craggy mountain slopes.
The base of each mountain spans a number of units (1 ≤ `B` ≤ `R`) that is always an odd number (how unusual!).
The input is now provided as
```
N R
H X D B
H X D B
...
```
For example, the mountain range in the problem description would be:
```
3 6
3 3 3 5
2 5 4 3
1 4 2 1
```

If it helps, you may assume that the height of the mountainside is the floored integer value of the mountain at that index.
So, a mountain with
```
3 3 3 3
```
has staircase-like sides and looks like
```
3 |     X
2 |     X
1 |   X X X
--+--------
  | 1 2 3 4
```

Given a description of a mountain range as above, output how many distinct mountains are visible on the mural.

Test your solution against this input:

3 mountains, R=21

Input:
```
3 21
9 11 13 3
3 11 17 11
4 11 20 5
```

Output:
```
2
```

M1 is tall and skinny.
M2 is short and wide.
M3 is somewhere in-between.
If M3 were distance 10 instead of 20, then it would be visible.

# Part 3a: Parallelizing (extension)

To aid in your research, CR has built for your exclusive use the Superchalkputer.
Boasting ∞ cores, caches, and RAM, this machine is a wonder of the world on its own!
Update your solution to take advantage of this phenomenal opportunity.

# Part 3b: Optimizing (extension)

The resort also wants to sell prints of the mountain view from the resort, and they want you to help them choose the best angle that captures the most mountains in the shot!
The camera will be placed at ground level at (1 ≤ `L` ≤ `R`).

For example,
```
3 6
3 3 3 5
2 4 4 3
1 4 2 1
```
has Mountain 2 obscured by Mountain 1
```
3 |     1
2 |   1 1 1
1 | 1 1 1 3 1
--+------------
  | 1 2 3 4 5 6
```
but if `L = 6`, then onlookers would be able to see just the slightest edge of Mountain 2!

Given a description of a mountain range, find an `L` that captures the most mountains in the photo.
