Skip to content

Instantly share code, notes, and snippets.

@maudzekod4000
Created December 11, 2024 18:41
Show Gist options
  • Save maudzekod4000/3891b445ffa3face98e2e53dcf6962af to your computer and use it in GitHub Desktop.
Save maudzekod4000/3891b445ffa3face98e2e53dcf6962af to your computer and use it in GitHub Desktop.
Advent Of Code 2024 - Problem 1 Part 2
#include <iostream>
#include <fstream>
#include <string>
#include <ctype.h>
#include <vector>
#include <algorithm>
#include <unordered_map>
#include <sstream>
using namespace std;
int main()
{
ifstream infile("input.txt");
if (!infile.is_open()) {
cout << "File prob\n";
return -1;
}
std::vector<int> firstList;
std::unordered_map<int, size_t> nOccurences;
string line;
while (infile.good()) {
getline(infile, line);
if (line.empty()) continue;
std::stringstream ss(line);
int list1Location, list2Location;
ss >> list1Location >> list2Location;
firstList.push_back(list1Location);
nOccurences[list2Location]++;
}
infile.close();
long long totalSimilarity = 0;
for (int n : firstList) {
totalSimilarity += n * nOccurences[n];
}
std::cout << totalSimilarity << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment