Created
December 10, 2024 21:12
-
-
Save maudzekod4000/2bf821b56bb38d233d6a16a6eed2f565 to your computer and use it in GitHub Desktop.
Advent Of Code 2024 Day 1 - Problem 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <string> | |
#include <ctype.h> | |
#include <vector> | |
#include <algorithm> | |
#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, secondList; | |
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); | |
secondList.push_back(list2Location); | |
} | |
infile.close(); | |
std::sort(firstList.begin(), firstList.end()); | |
std::sort(secondList.begin(), secondList.end()); | |
long long totalDistance = 0; | |
for (size_t i = 0; i < firstList.size(); i++) { | |
totalDistance += abs(firstList[i] - secondList[i]); | |
} | |
std::cout << totalDistance << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment