-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdayTen.cpp
More file actions
143 lines (118 loc) · 4.73 KB
/
dayTen.cpp
File metadata and controls
143 lines (118 loc) · 4.73 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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <set>
namespace DayTen
{
namespace {
using square_vect_t = std::vector<std::vector<int>>;
using coord_t = std::pair<int, int>;
void handleFile(std::ifstream& inputFile, square_vect_t& intSquare, const int kSquareLen);
void coutVectorContents(const std::string prefix, const std::vector<coord_t>& vector);
void fillLineInVector(const std::string line, square_vect_t& intSquare, const int kSquareLen, const int currentRow);
bool isValidSlope(const square_vect_t& intSquare, const int prevHeight, const coord_t& coordToCheck, const int kSquareLen);
bool isCoordWithinBounds(const coord_t& nextCoord, const int kSquareLen);
const bool toLog = false; // true false
const int kTestFileLineLen = 8;
const int kFullFileLineLen = 43; // files validated and pre checked for length and valid chars only.
const int kPeakVal = 9;
const std::vector<coord_t> movements = { {-1, 0}, {0, 1}, {1, 0}, {0, -1} };
std::vector<coord_t> startingCoords;
void findPeaks(const square_vect_t& intSquare, const coord_t& startCoord, std::set<coord_t>& peakCoordsForThisStart, const int kSquareLen)
{
std::vector<coord_t> stack;
stack.push_back(startCoord);
while (!stack.empty()) {
coord_t currentCoord = stack.back();
stack.pop_back();
for (const auto& movement : movements) {
coord_t nextCoord = { currentCoord.first + movement.first, currentCoord.second + movement.second };
if (isValidSlope(intSquare, intSquare[currentCoord.first][currentCoord.second], nextCoord, kSquareLen)) {
if (intSquare[nextCoord.first][nextCoord.second] == kPeakVal) {
peakCoordsForThisStart.insert(nextCoord);
}
else {
stack.push_back(nextCoord);
}
}
}
}
}
size_t calculatePeaksForCoord(const square_vect_t& intSquare, const coord_t startCoord, const int kSquareLen)
{
std::set<coord_t> peakCoordsForThisStart;
findPeaks(intSquare, startCoord, peakCoordsForThisStart, kSquareLen);
return peakCoordsForThisStart.size();
}
size_t calculateTotalPeaks(const square_vect_t& intSquare, const int kSquareLen)
{
size_t total = 0;
for (const auto& startCoord : startingCoords)
{
total += calculatePeaksForCoord(intSquare, startCoord, kSquareLen);
}
return total;
}
void handleFile(std::ifstream& inputFile, square_vect_t& intSquare, const int kSquareLen)
{
if (inputFile.is_open()) {
std::string line;
int currentRow = 0;
while (getline(inputFile, line)) {
if (currentRow >= kSquareLen) throw std::runtime_error("Line read out of bounds");
fillLineInVector(line, intSquare, kSquareLen, currentRow);
currentRow++;
}
if (toLog) coutVectorContents("Starting coords: ", startingCoords);
std::cout << "Finished reading file\n";
inputFile.close(); // not required, handled by scope, about explicit purpose
size_t totalScore = calculateTotalPeaks(intSquare, kSquareLen);
std::cout << "Total: " << totalScore << '\n';
std::cout << "Finished running program\n";
}
else {
std::cout << "Unable to open file\n";
}
}
bool isValidSlope(const square_vect_t& intSquare, const int prevHeight, const coord_t& coordToCheck, const int kSquareLen) {
if (!isCoordWithinBounds(coordToCheck, kSquareLen)) return false;
return (intSquare[coordToCheck.first][coordToCheck.second] - prevHeight) == 1;
}
bool isCoordWithinBounds(const coord_t& nextCoord, const int kSquareLen)
{
return nextCoord.first >= 0 && nextCoord.first < kSquareLen && nextCoord.second >= 0 && nextCoord.second < kSquareLen;
}
void fillLineInVector(const std::string line, square_vect_t& intSquare, const int kSquareLen, const int currentRow) {
for (int col = 0; col < kSquareLen; ++col) {
intSquare[currentRow][col] = line[col] - '0'; //ASCII num parsing
if (intSquare[currentRow][col] == 0) startingCoords.emplace_back(currentRow, col);
}
}
void coutVectorContents(const std::string prefix, const std::vector<coord_t>& vector)
{
std::cout << prefix;
for (const auto& coord : vector) {
std::cout << "[" << coord.first << "," << coord.second << "] ";
}
std::cout << '\n';
}
}
void dayTen() {
std::system("cls");
std::cout << "Running program DayTen" << '\n';
const bool isFullFile = true; // true false
const int kSquareLen = isFullFile ? kFullFileLineLen : kTestFileLineLen;
square_vect_t intSquare(kSquareLen, std::vector<int>(kSquareLen));
std::ifstream inputFile;
if (isFullFile) {
std::cout << "Using full file\n\n";
}
else {
std::cout << "Using test file\n\n";
}
(isFullFile) ? inputFile.open("dayTenFull.txt") : inputFile.open("dayTenTest.txt");
handleFile(inputFile, intSquare, kSquareLen);
}
}