-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdayFive.cpp
More file actions
163 lines (140 loc) · 4.17 KB
/
dayFive.cpp
File metadata and controls
163 lines (140 loc) · 4.17 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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <unordered_map>
#include <set>
namespace DayFive
{
namespace {
void coutMapsContents();
void coutVectorContents(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 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(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++)
{
int diff = j - i;
if (diff < 0) {
if (rulesPrecedingPages[numToCheck].count(update.at(j)) == 1) { // set, count is 1 or 0
if (toLog) std::cout << "i: " << update.at(i) << ", j: " << update.at(j) << '\n';
return 0;
}
}
else if (diff > 0) {
if (rulesEnsuingPages[numToCheck].count(update.at(j)) == 1) {
if (toLog) std::cout << "i: " << update.at(i) << ", j: " << update.at(j) << '\n';
return 0;
}
}
else {
// do nothing
}
}
}
int parsedMiddlePageNum = std::stoi(update.at(vectorLen / 2));
if (toLog) std::cout << parsedMiddlePageNum << '\n';
return parsedMiddlePageNum;
}
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";
}
if (toLog) 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::vector<std::string>& update)
{
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 dayFive() {
std::cout << "Running program Day Five" << '\n'; // Safe inputs, two-digit numbers only
const bool isFullFile = false; // true false
std::string line;
std::ifstream inputFile;
(isFullFile) ? inputFile.open("dayFiveFull.txt") : inputFile.open("dayFiveTest.txt");
handleFile(inputFile);
}
}