Skip to content

Instantly share code, notes, and snippets.

@tiegz
Last active December 2, 2017 05:52
Show Gist options
  • Save tiegz/aa45a90eb7b15a6a442bfbcb29afed4a to your computer and use it in GitHub Desktop.
Save tiegz/aa45a90eb7b15a6a442bfbcb29afed4a to your computer and use it in GitHub Desktop.
Advent of Code 2017
#include <string>
#include <iostream>
using namespace std;
// const string INPUT = "1122";
// const string INPUT = "1111";
// const string INPUT = "1234";
const string INPUT = "91212129";
int main () {
int sum = 0;
int next = 0;
for (int i = 0; i < INPUT.length(); i++) {
next = i == INPUT.length() - 1 ? 0 : i + 1;
if (INPUT[i] == INPUT[next]) {
sum += (int)INPUT[i] - 48;
}
}
cout << "The sum of the digits that match their successor (looping) is " << sum << endl;
return 0;
}
#include <string>
#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;
int main () {
ifstream inputFile;
string currLine;
int min;
int max;
int curr;
int sum = 0;
inputFile.open("day_2.txt");
while (std::getline(inputFile, currLine)) {
int min = INT_MAX;
int max = INT_MIN;
int curr;
istringstream cl(currLine);
while (cl >> curr) {
if (curr < min) min = curr;
if (curr > max) max = curr;
}
sum += (max - min);
}
inputFile.close();
cout << "The sum is " << sum << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment