-
Notifications
You must be signed in to change notification settings - Fork 0
/
UniqueSequencesCounter.cpp
43 lines (31 loc) · 1.39 KB
/
UniqueSequencesCounter.cpp
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
#include <numeric>
#include "UniqueSequencesCounter.hpp"
UniqueSequencesCounter::UniqueSequencesCounter(const MovesMap &movesMap) : movesMap_{movesMap} {
}
uint64_t UniqueSequencesCounter::calculate(char startChar, int steps) {
auto startPosition = movesMap_.getPosition(startChar);
uint64_t result{0};
if (startPosition) {
result = calculateUniqueSequencesCount(startPosition.value(), steps);
}
return result;
}
uint64_t UniqueSequencesCounter::calculateUniqueSequencesCount(Position startPosition, int steps, bool isShortMove) {
auto params = std::make_tuple(startPosition, isShortMove, steps);
if (calculatedResults_.find(params) != calculatedResults_.end()) {
return calculatedResults_.at(params);
}
uint64_t result;
auto validMoves{isShortMove ? movesMap_.getShortMoves(startPosition) : movesMap_.getLongMoves(startPosition)};
if (steps == 1) {
result = validMoves.size();
calculatedResults_.insert({params, result});
return result;
}
result = std::accumulate(std::begin(validMoves), std::end(validMoves), (uint64_t) 0,
[steps, isShortMove, this](uint64_t acc, auto pos) {
return acc + calculateUniqueSequencesCount(pos, steps - 1, !isShortMove);
});
calculatedResults_.insert({params, result});
return result;
}