-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdayNinePartTwo.cpp
More file actions
224 lines (224 loc) · 6.6 KB
/
dayNinePartTwo.cpp
File metadata and controls
224 lines (224 loc) · 6.6 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// COMMENTED OUT CODE DISCLAIMER
// To ensure compilation for the other files with the BigInt.hpp or any other external header files, this entire file is commented out.
// In the current structure that only uses one file at a time, header files are not perfectly organised.
// Having many files in a project you have no intention of using is not a normal use case, therefore organising this perfectly is not a priority.
// To test this file, get the BigInt hpp file from github and add it to the Header Files, in my case in a "libs" filter.
// Comment out any other cpp files using it. Only the .cpp files should need to be edited.
// /COMMENTED OUT CODE DISCLAIMER
//#include <cstdlib>
//#include <iostream>
//#include <fstream>
//#include <string>
//#include <sstream>
//#include <vector>
//#include <limits>
//#include <algorithm>
//#include "BigInt.hpp" // https://github.com/faheel/BigInt
//
//namespace DayNinePartTwo
//{
// namespace {
// const bool toLog = false; // true false
// const size_t kEmptySpaceValue = std::numeric_limits<size_t>::max();
// const size_t kFirstIdVal = 0;
// const size_t kMaxSpace = 9;
// const int kMaxIter = 1'000'000;
// void coutVectorContents(const std::string prefix, const std::vector<size_t>& vector);
// BigInt calculateChecksum(std::vector<size_t>& compressedDisk);
//
// size_t findAvailableSpaceStartIndex(const std::vector<size_t>& compressedDisk, const size_t leftMostEmptySpacePos, const size_t spaceRequested, const size_t right)
// {
// size_t currentPos = leftMostEmptySpacePos;
// int iter = 0;
//
// while (currentPos < right)
// {
// size_t spaceLen = 0;
//
// while (currentPos + spaceLen < right && compressedDisk[currentPos + spaceLen] == kEmptySpaceValue) spaceLen++;
//
// if (spaceLen >= spaceRequested) return currentPos;
// currentPos += spaceLen;
//
// while (currentPos < right && compressedDisk[currentPos] != kEmptySpaceValue) currentPos++; // find next non-empty pos
//
// iter++;
// if (iter > kMaxIter) throw std::runtime_error("Max iter breached");
// }
//
// return kEmptySpaceValue;
// }
//
// std::pair<size_t, size_t> getIdSpaces(const std::vector<size_t>& compressedDisk, size_t rightCopy)
// {
// size_t id = compressedDisk[rightCopy];
// size_t count = 0;
// int iter = 0;
//
// while (rightCopy >= 0 && compressedDisk[rightCopy] == id)
// {
// count++;
// if (rightCopy == 0) break;
// rightCopy--;
//
// iter++;
// if (iter > kMaxIter) throw std::runtime_error("Max iter breached");
// }
//
// return { id, count };
// }
//
// void setDiskFinalState(std::vector<size_t>& compressedDisk)
// {
// size_t size = compressedDisk.size();
// size_t right = size - 1;
//
// while (right > 0 && right < size)
// {
// if (compressedDisk[right] == kEmptySpaceValue)
// {
// right--;
// continue;
// }
//
// std::pair<size_t, size_t> idSpaces = getIdSpaces(compressedDisk, right);
// size_t id = idSpaces.first;
// size_t spaceRequest = idSpaces.second;
//
// if (right - spaceRequest <= 0) break;
//
// size_t currentPos = findAvailableSpaceStartIndex(compressedDisk, 0, spaceRequest, right);
//
// if (currentPos != kEmptySpaceValue)
// {
// std::fill(compressedDisk.begin() + currentPos, compressedDisk.begin() + currentPos + spaceRequest, id);
// std::fill(compressedDisk.begin() + right - spaceRequest + 1, compressedDisk.begin() + right + 1, kEmptySpaceValue);
// }
//
// right -= spaceRequest;
// if (toLog)
// {
// coutVectorContents("", compressedDisk);
// std::cout << "id: " << id << "; spacerequest: " << spaceRequest << "; right: " << right << '\n';
// }
// }
// }
//
// std::vector<size_t> setDiskPrimaryState(const std::string& uncompressedLine)
// {
// std::vector<size_t> compressedDisk;
// size_t len = uncompressedLine.size();
// size_t id = 0;
//
// for (size_t i = 0; i < len; i++)
// {
// size_t parsedLen = static_cast<size_t>(uncompressedLine[i] - '0'); // only digits in input data, ASCII conversion
// if (i % 2 == 0)
// {
// compressedDisk.insert(compressedDisk.end(), parsedLen, id);
// id++;
// }
// else
// {
// compressedDisk.insert(compressedDisk.end(), parsedLen, kEmptySpaceValue);
// }
// }
//
// if (toLog) coutVectorContents("Primary state:\n", compressedDisk);
//
// return compressedDisk;
// }
//
// BigInt handleUncompressedLine(const std::string& uncompressedLine)
// {
// std::vector<size_t> compressedDisk = setDiskPrimaryState(uncompressedLine);
//
// setDiskFinalState(compressedDisk);
//
// if (toLog) coutVectorContents("Final state:\n", compressedDisk);
//
// return calculateChecksum(compressedDisk);
// }
//
// void handleFile(std::ifstream& inputFile)
// {
// std::pair<int, int> startingGuardCoord;
// if (inputFile.is_open()) {
// std::string line;
// BigInt checksum;
// while (getline(inputFile, line)) {
// checksum = handleUncompressedLine(line);
// }
//
// std::cout << "\nFinished reading file\n";
//
// inputFile.close(); // automatically happens when going out of scope but no longer needed. More about explicitness.
//
// std::cout << "Checksum: " << checksum;
//
// std::cout << "\nFinished running program";
// }
// else {
// std::cout << "Unable to open file\n";
// }
// }
//
// BigInt calculateChecksum(std::vector<size_t>& compressedDisk)
// {
// size_t len = compressedDisk.size();
// BigInt total = 0;
//
// for (size_t i = 0; i < len; i++)
// {
// if (compressedDisk[i] == kEmptySpaceValue)
// {
// continue;
// }
// if (toLog)
// {
// std::cout << total << " + (" << i << " * " << compressedDisk[i] << ") = ";
// }
// total += (i * compressedDisk[i]);
// if (toLog)
// {
// std::cout << total << '\n';
// }
// }
//
// return total;
// }
//
// void coutVectorContents(const std::string prefix, const std::vector<size_t>& vector)
// {
// std::cout << prefix;
// for (const auto& num : vector) {
// if (num == kEmptySpaceValue) {
// std::cout << "M"; //MAX_VAL
// }
// else {
// std::cout << num;
// }
// }
// std::cout << '\n';
// }
//
// }
// void dayNinePartTwo() {
// std::system("cls"); // clear terminal pre-boot
// std::cout << "Running program DayNinePartTwo" << '\n';
// const bool isFullFile = true; // true false
//
// std::string line;
// std::ifstream inputFile;
// if (isFullFile) {
// std::cout << "Using full file\n\n";
// }
// else {
// std::cout << "Using test file\n\n";
// }
// (isFullFile) ? inputFile.open("dayNineFull.txt") : inputFile.open("dayNineTest.txt");
// handleFile(inputFile);
// }
//}
//
//// 3310519109964 too low