-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLand_perimeter.cs
78 lines (57 loc) · 2.54 KB
/
Land_perimeter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using System.Collections.Generic;
using System.Linq;
namespace CodingChallenges;
[TestClass]
public class Land_perimeter {
/*
Land perimeter
Given an array arr of strings, complete the function by calculating the total perimeter of all the islands. Each piece of land will be marked with 'X' while the water fields are represented as 'O'. Consider each tile being a perfect 1 x 1 piece of land. Some examples for better visualization:
['XOOXO',
'XOOXO',
'OOOXO',
'XXOXO',
'OXOOO']
should return: "Total land perimeter: 24".
Following input:
['XOOO',
'XOXO',
'XOXO',
'OOXX',
'OOOO']
should return: "Total land perimeter: 18"
*/
[DataTestMethod]
[DataRow("", "Total land perimeter: 0")]
[DataRow("O", "Total land perimeter: 0")]
[DataRow("X", "Total land perimeter: 4")]
[DataRow("OXOOOX OXOXOO XXOOOX OXXXOO OOXOOX OXOOOO OOXOOX OOXOOO OXOOOO OXOOXX", "Total land perimeter: 60")]
[DataRow("OXOOO OOXXX OXXOO XOOOO XOOOO XXXOO XOXOO OOOXO OXOOX XOOOO OOOXO", "Total land perimeter: 52")]
[DataRow("XXXXXOOO OOXOOOOO OOOOOOXO XXXOOOXO OXOXXOOX", "Total land perimeter: 40")]
[DataRow("XOOOXOO OXOOOOO XOXOXOO OXOXXOO OOOOOXX OOOXOXX XXXXOXO", "Total land perimeter: 54")]
[DataRow("OOOOXO XOXOOX XXOXOX XOXOOO OOOOOO OOOXOO OOXXOO", "Total land perimeter: 40")]
public void Test(string input, string expected) =>
Assert.AreEqual(expected, Calculate(input.Split()));
public static string Calculate(string[] map) {
int peri = 0;
if (map is null || map.Length == 0 || map.FirstOrDefault("").Length == 0) goto goOut;
string mapAsString = string.Join(' ', map);
int countX = mapAsString.Count(c => c == 'X');
int hDelims = CountContChar(mapAsString, 'X');
List<string> rotatedArr = new();
foreach (int ind in map.FirstOrDefault("").Select((_, i) => i))
rotatedArr.Add( string.Concat(map.Select(r => r[ind])) );
string rotatedMapAsString = string.Join(' ', rotatedArr);
int vDelims = CountContChar(rotatedMapAsString, 'X');
peri = countX * 4 - (hDelims + vDelims) * 2;
goOut: return $"Total land perimeter: {peri}";
}
private static int CountContChar(string str, char ch) {
int masterCounter = 0;
int regionalCounter = 0;
foreach (char c in str) {
if (c == ch) regionalCounter++; else regionalCounter = 0;
if (regionalCounter > 1) masterCounter++;
}
return masterCounter;
}
}