Skip to content

Commit fd9bcf3

Browse files
JakubDotPygithub-actions[bot]
authored andcommitted
Automated update
1 parent 8fb2030 commit fd9bcf3

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""Kata - Simple Fun #19: Count Black Cells
2+
3+
completed at: 2024-05-07 13:58:31
4+
by: Jakub Červinka
5+
6+
# Task
7+
Imagine a white rectangular grid of `n` rows and `m` columns divided into two parts by a diagonal line running from the upper left to the lower right corner. Now let's paint the grid in two colors according to the following rules:
8+
```
9+
A cell is painted black if it has at least one point in common with the diagonal;
10+
Otherwise, a cell is painted white.
11+
```
12+
Count the number of cells painted black.
13+
14+
# Example
15+
16+
For n = 3 and m = 4, the output should be `6`
17+
18+
There are 6 cells that have at least one common point with the diagonal and therefore are painted black.
19+
20+
![](https://codefightsuserpics.s3.amazonaws.com/tasks/countBlackCells/img/example1.jpg?_tm=1474285619565)
21+
22+
For n = 3 and m = 3, the output should be `7`
23+
24+
7 cells have at least one common point with the diagonal and are painted black.
25+
26+
![](https://codefightsuserpics.s3.amazonaws.com/tasks/countBlackCells/img/example2.jpg?_tm=1474285619707)
27+
28+
# Input/Output
29+
30+
- `[input]` integer `n`
31+
32+
The number of rows.
33+
34+
Constraints: 1 ≤ n ≤ 10000.
35+
36+
- `[input]` integer `m`
37+
38+
The number of columns.
39+
40+
Constraints: 1 ≤ m ≤ 10000.
41+
42+
- `[output]` an integer
43+
44+
The number of black cells.
45+
"""
46+
47+
from math import gcd
48+
49+
def count_black_cells(h, w):
50+
g = gcd(h, w)
51+
return h + w - g + (g - 1) * 2

0 commit comments

Comments
 (0)