My solutions to http://adventofcode.com/2017
Last active
December 2, 2017 05:52
-
-
Save tiegz/aa45a90eb7b15a6a442bfbcb29afed4a to your computer and use it in GitHub Desktop.
Advent of Code 2017
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 <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; | |
} |
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 <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