-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdayFivePartTwo.cpp
More file actions
200 lines (173 loc) · 5.39 KB
/
dayFivePartTwo.cpp
File metadata and controls
200 lines (173 loc) · 5.39 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <unordered_map>
#include <set>
namespace DayFivePartTwo
{
namespace {
void coutMapsContents();
void coutVectorContents(std::string prefix, std::vector<std::string>& update);
using rulemap_t = std::unordered_map<std::string, std::set<std::string>>; // int parsing not needed for part one, no logic for the numbers
const bool toLog = false; // true false
const char ruleDelim = '|';
const char updateDelim = ',';
const int numLen = 2;
rulemap_t rulesPrecedingPages; // Key must appear after value in set
rulemap_t rulesEnsuingPages; // Key must appear before value in set
int getMiddlePageForIncorrectUpdate(std::vector<std::string> updateCopy)
{
size_t vectorLen = updateCopy.size();
for (size_t i = 1; i < vectorLen; ++i) { // insertion sort
std::string key = updateCopy[i];
size_t j = i;
while (j > 0 && rulesPrecedingPages[key].count(updateCopy[j - 1]) == 1) {
updateCopy[j] = updateCopy[j - 1];
--j;
}
updateCopy[j] = key;
}
for (size_t i = 0; i < vectorLen; ++i) {
std::string numToCheck = updateCopy[i];
for (size_t j = 0; j < vectorLen; ++j) {
long long diff = static_cast<long long>(j) - static_cast<long long>(i); // unsigned underflow
if (diff < 0) {
if (rulesPrecedingPages[numToCheck].count(updateCopy.at(j)) == 1) { // set, count is 1 or 0
coutVectorContents("Invalid sort: ", updateCopy);
throw std::runtime_error::runtime_error("Sorting not succesful");
}
}
else if (diff > 0) {
if (rulesEnsuingPages[numToCheck].count(updateCopy.at(j)) == 1) {
coutVectorContents("Invalid sort: ", updateCopy);
throw std::runtime_error::runtime_error("Sorting not succesful");
}
}
else {
// do nothing
}
}
if (toLog) coutVectorContents("Current State: ", updateCopy);
}
if (toLog) coutVectorContents("Sorted: ", updateCopy);
return std::stoi(updateCopy[updateCopy.size() / 2]);
}
int getMiddlePageValue(std::string line)
{
const int numDist = numLen + 1;
std::vector<std::string> update;
size_t lineLen = line.length();
for (size_t i = 0; i < lineLen; i += numDist) {
if (i + 1 < lineLen) {
update.push_back(line.substr(i, numLen));
}
}
if (toLog) coutVectorContents("Testing: ", update);
size_t vectorLen = update.size();
if (vectorLen % 2 == 0) throw std::runtime_error::runtime_error("Even amount of update entries in a line, no middle");
for (size_t i = 0; i < vectorLen; i++)
{
std::string numToCheck = update.at(i);
for (size_t j = 0; j < vectorLen; j++)
{
long long diff = static_cast<long long>(j) - static_cast<long long>(i); // unsigned underflow
if (diff < 0) {
if (rulesPrecedingPages[numToCheck].count(update.at(j)) == 1) { // set, count is 1 or 0
return getMiddlePageForIncorrectUpdate(update);
}
}
else if (diff > 0) {
if (rulesEnsuingPages[numToCheck].count(update.at(j)) == 1) {
return getMiddlePageForIncorrectUpdate(update);
}
}
else {
// do nothing
}
}
}
return 0;
}
void handleRuleLine(std::string line)
{
size_t delimPos = line.find(ruleDelim);
std::string firstPage = line.substr(0, numLen);
std::string secondPage = line.substr(delimPos + 1, numLen);
// if (toLog) std::cout << firstPage << '-' << secondPage << '\n';
rulesEnsuingPages[secondPage].insert(firstPage);
rulesPrecedingPages[firstPage].insert(secondPage);
}
void handleAnyLine(std::string& line, int& middlePageTotal)
{
if (line.empty() || line[0] == '\n') {
if (toLog) {
coutMapsContents();
std::cout << "---\nRules are defined.\n---\n";
}
}
else {
switch (line[2]) {
case ruleDelim:
handleRuleLine(line);
break;
case updateDelim:
middlePageTotal += getMiddlePageValue(line);
break;
default:
throw std::runtime_error::runtime_error("Invalid line parsing");
}
}
}
void handleFile(std::ifstream& inputFile)
{
int middlePageTotal = 0;
if (inputFile.is_open()) {
std::string line;
while (getline(inputFile, line)) {
handleAnyLine(line, middlePageTotal);
}
std::cout << "Total: " << middlePageTotal << '\n';
std::cout << "Finished running program\n";
}
else {
std::cout << "Unable to open file\n";
}
}
void coutVectorContents(std::string prefix, std::vector<std::string>& update)
{
std::cout << prefix;
for (const auto& num : update) {
std::cout << num << ' ';
}
std::cout << '\n';
}
void coutMapsContents()
{
std::cout << "Preceding:\n";
for (const auto& pair : rulesPrecedingPages) {
std::cout << pair.first << ": ";
for (const auto& value : pair.second) {
std::cout << value << ' ';
}
std::cout << '\n';
}
std::cout << "Ensuing:\n";
for (const auto& pair : rulesEnsuingPages) {
std::cout << pair.first << ": ";
for (const auto& value : pair.second) {
std::cout << value << ' ';
}
std::cout << '\n';
}
}
}
void dayFivePartTwo() {
std::cout << "Running program Day Five Part Two" << '\n'; // Safe inputs, two-digit numbers only
const bool isFullFile = true; // true false
std::string line;
std::ifstream inputFile;
(isFullFile) ? inputFile.open("dayFiveFull.txt") : inputFile.open("dayFiveTest.txt");
handleFile(inputFile);
}
}