-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay4.cpp
More file actions
59 lines (58 loc) · 957 Bytes
/
Copy pathDay4.cpp
File metadata and controls
59 lines (58 loc) · 957 Bytes
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
#ifndef DAY4_CPP
#define DAY4_CPP
#include <string>
static bool isValidNum(const int number)
{
std::string num = std::to_string(number);
int prev = -1;
int pairCounter = 0;
bool pair = false;
for (int i = 0; i < num.length(); i++)
{
int currentVal = num[i] - '0';
if (currentVal == prev)
{
pairCounter++;
pair = true;
}
else if (currentVal < prev)
{
return false;
}
else if (pairCounter != 0) //prev != -1 && currentVal != prev
{
if (pairCounter % 2 == 0)
{
return false;
}
else
{
pairCounter = 0;
}
}
prev = currentVal;
}
/*if (number == 146888)
{
return pair && pairCounter % 2 == 1;
}*/
//if(pairCounter )
if (pairCounter != 0)
return pairCounter % 2 == 1;
return pair;
}
static int day4()
{
int passwords = 0;
int start = 146810;
int end = 612564;
for (; start <= end; start++)
{
if (isValidNum(start))
{
passwords++;
}
}
return passwords;
}
#endif // !DAY4_CPP