Compare commits

..

No commits in common. "e74ca62d0267456265c945b9726b404bd1925365" and "095ddd091e26ca659bf8dde32da7541c7afa4589" have entirely different histories.

3 changed files with 68 additions and 48 deletions

View File

@ -72,7 +72,46 @@ To find the total distance between the left list and the right list, add up the
you found. In the example above, this is 2 + 1 + 0 + 1 + 2 + 5, a total distance of 11! you found. In the example above, this is 2 + 1 + 0 + 1 + 2 + 5, a total distance of 11!
Your actual left and right lists contain many location IDs. What is the total distance between your lists? Your actual left and right lists contain many location IDs. What is the total distance between your lists?
*/
void day01::Part1()
{
std::vector<int> col1;
std::vector<int> col2;
int sum = 0;
std::ifstream input("input/day01.txt");
if (!input)
{
std::cerr << "Failed to open input file." << std::endl;
std::cout << "Current path is " << std::filesystem::current_path() << '\n';
return;
}
std::string delimiter = " ";
auto delimiterLength = delimiter.length();
std::string line;
while (std::getline(input, line))
{
auto delimiterPos = line.find(delimiter);
col1.emplace_back(std::stoi(line.substr(0, delimiterPos)));
col2.emplace_back(std::stoi(line.substr(delimiterPos + delimiterLength, line.length())));
}
std::sort(col1.begin(), col1.end());
std::sort(col2.begin(), col2.end());
for (int i = 0; i < col1.size(); ++i)
{
sum += std::abs(col1[i] - col2[i]);
}
}
/*
--- Part Two --- --- Part Two ---
Your analysis only confirmed what everyone feared: the two lists of location IDs are indeed very different. Your analysis only confirmed what everyone feared: the two lists of location IDs are indeed very different.
@ -111,20 +150,15 @@ Once again consider your left and right lists. What is their similarity score?
*/ */
void day01::Calculate() void day01::Part2()
{ {
std::vector<int> col1; std::vector<int> col1;
std::vector<int> col2; std::vector<int> col2;
std::unordered_map<int, int> occurrence_map; std::unordered_map<int, int> occurrence_map;
int totalDistance = 0; int sum = 0;
int similarityScore = 0;
size_t estimated_size = 1000;
col1.reserve(estimated_size);
col2.reserve(estimated_size);
occurrence_map.reserve(estimated_size);
std::ifstream input("input/day01.txt"); std::ifstream input("input/day01.txt");
//std::ifstream input("input/day01short.txt");
if (!input) if (!input)
{ {
@ -134,28 +168,31 @@ void day01::Calculate()
return; return;
} }
std::string delimiter = " ";
auto delimiterLength = delimiter.length();
std::string line; std::string line;
int num1, num2; while (std::getline(input, line))
while (input >> num1 >> num2)
{ {
col1.emplace_back(num1); auto delimiterPos = line.find(delimiter);
col2.emplace_back(num2); col1.emplace_back(std::stoi(line.substr(0, delimiterPos)));
occurrence_map[num2]++; col2.emplace_back(std::stoi(line.substr(delimiterPos + delimiterLength, line.length())));
} }
std::sort(col1.begin(), col1.end()); for (auto number: col2)
std::sort(col2.begin(), col2.end());
for (size_t i = 0; i < col1.size(); i++)
{ {
totalDistance += std::abs(col1[i] - col2[i]); if (occurrence_map.contains(number))
similarityScore += col1[i] * occurrence_map[col1[i]]; {
occurrence_map[number]++;
} else
{
occurrence_map[number] = 1;
}
} }
printf("Total distance: %d\n", totalDistance); for (auto number: col1)
printf("Similarity score: %d\n", similarityScore); {
sum += number * occurrence_map[number];
}
printf("Sum of all occurrence pairs is: %d\n", sum);
} }
//Correct answers:
//part1: 1889772
//part2: 23228917

View File

@ -10,7 +10,7 @@ class day01
{ {
public: public:
static void Part1(); static void Part1();
static void Calculate(); static void Part2();
}; };

View File

@ -5,26 +5,9 @@
int main() int main()
{ {
double lowest = 10000;
double totalTime = 0;
constexpr int cycles = 1;
//constexpr int cycles = 10000;
for (int i = 1; i <= cycles; i++)
{
const auto startTime = std::chrono::high_resolution_clock::now(); const auto startTime = std::chrono::high_resolution_clock::now();
day01::Calculate(); day01::Part2();
const auto endTime = std::chrono::high_resolution_clock::now(); const auto endTime = std::chrono::high_resolution_clock::now();
const std::chrono::duration<double> diff = endTime - startTime; const std::chrono::duration<double> diff = endTime - startTime;
std::cout << "Execution time: " << diff.count() * 1e6 << " μs" << std::endl;
totalTime += diff.count();
if (diff.count() < lowest)
{
lowest = diff.count();
}
}
const double average = totalTime / cycles;
std::cout << "Ran " << cycles << " cycles. "
<< "Execution time avg: " << average * 1e6 << " μs. Lowest: "
<< lowest
* 1e6 << " μs" << std::endl;
} }