-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day9.cpp
74 lines (68 loc) · 1.44 KB
/
Day9.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <istream>
#include <fstream>
#include <ostream>
#include <string>
#include <algorithm>
#include <vector>
#define cin infile
#define cout outfile
using namespace std;
bool partTwo = true;
int main()
{
ifstream infile("input.txt");
ofstream outfile("output.txt");
string input;
long long totalSum = 0;
while (getline(cin, input))
{
vector<long long> nums;
bool numDetected = false;
bool minDetected = false;
long long number = 0;
for (int it = 0; it < input.size(); it++)
{
if (input[it] == '-')
{
minDetected = true;
continue;
}
if (input[it] >= '0' && input[it] <= '9')
{
numDetected = true;
number *= 10;
number += input[it] - '0';
}
else if (numDetected)
{
if (minDetected)
number *= -1;
nums.push_back(number);
number = 0;
numDetected = false;
minDetected = false;
}
}
if(numDetected){
if (minDetected)
number *= -1;
nums.push_back(number);
}
reverse(nums.begin(), nums.end());
bool allZeros = false;
while (!allZeros)
{
allZeros = true;
totalSum += nums[nums.size()-1];
for (int q = 0; q < nums.size() - 1; q++)
{
nums[q] = nums[q + 1] - nums[q];
allZeros = allZeros && nums[q] == 0;
}
nums.pop_back();
}
}
cout << totalSum << endl;
return 0;
}