-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1p1.txt
More file actions
54 lines (48 loc) · 1.04 KB
/
1p1.txt
File metadata and controls
54 lines (48 loc) · 1.04 KB
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
#include <iostream>
#include <string>
std::string input = "";
int halfLength = input.length() / 2;
int getSumOfNumbersThatMatchNextNumber(std::string input)
{
int sum = 0;
std::string temp;
if (input[input.length() - 1] == input[0])
{
temp = input[0];
sum += std::stoi(temp);
}
for (int i = 0; i < input.length() - 1; ++i)
{
if (input[i] == input[i + 1])
{
temp = input[i];
sum += std::stoi(temp);
}
}
return sum;
}
int getSumOfNumbersThatMatchNumberHalfOfListAway(std::string input)
{
int sum = 0;
std::string temp;
for (int i = 0; i < input.length(); ++i)
{
if ((i + halfLength) > (input.length() - 1) && input[(i + halfLength) - input.length()] == input[i])
{
temp = input[i];
sum += std::stoi(temp);
}
else if (input[i] == input[i + halfLength])
{
temp = input[i];
sum += std::stoi(temp);
}
}
return sum;
}
int main()
{
std::cout << "Part 1: " << getSumOfNumbersThatMatchNextNumber(input) << '\n';
std::cout << "Part 2: " << getSumOfNumbersThatMatchNumberHalfOfListAway(input) << '\n';
return 0;
}