-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClue.cs
More file actions
32 lines (24 loc) · 1.21 KB
/
Copy pathClue.cs
File metadata and controls
32 lines (24 loc) · 1.21 KB
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
namespace CodeAdvent;
public class Clue
{
private readonly string _pattern;
private readonly int _inPositionElementCount;
private readonly int _outOfPositionElementCount;
public Clue(string pattern, int inPositionElementCount, int outOfPositionElementCount)
{
_pattern = pattern;
_inPositionElementCount = inPositionElementCount;
_outOfPositionElementCount = outOfPositionElementCount;
}
public bool Matches(string input)
=> input.Length != _pattern.Length
? throw new ArgumentOutOfRangeException(nameof(input), "The length of input and clue pattern must match.")
: GetInPositionMatchCount(input) == _inPositionElementCount
&& GetOutOfPositionMatchCount(input) == _outOfPositionElementCount;
private int GetInPositionMatchCount(string input)
=> _pattern.Select((c, idx) => c == input[idx] ? 1 : 0).Sum();
private int GetOutOfPositionMatchCount(string input)
=> _pattern.Select((c, idx) => OccursOutOfPosition(input, c, idx) ? 1 : 0).Sum();
private static bool OccursOutOfPosition(string input, char charToCheck, int position)
=> input[position] != charToCheck && input.Contains(charToCheck);
};