-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
83 lines (73 loc) · 1.78 KB
/
main.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
75
76
77
78
79
80
81
82
83
#include "file.h"
#include <iostream>
namespace aoc2019_day04 {
bool isAdjacent(int nr) {
int prev = nr % 10;
nr = nr / 10;
while (nr > 0) {
if (prev == nr % 10) return true;
prev = nr % 10;
nr = nr / 10;
}
return false;
}
bool neverDecrease(int nr) {
int prev = nr % 10;
nr = nr / 10;
while (nr > 0) {
if (prev < nr % 10) return false;
prev = nr % 10;
nr = nr / 10;
}
return true;
}
bool isAdjacentNoMoreThen2(int nr) {
int prev = nr % 10;
nr = nr / 10;
int k = 1;
while (nr > 0) {
if (prev == nr % 10) {
k++;
}
else {
if (k == 2) {
return true;
}
else {
k = 1;
}
}
prev = nr % 10;
nr = nr / 10;
}
if (k == 2) return true;
return false;
}
bool isValidPart1(int nr) {
return isAdjacent(nr) & neverDecrease(nr);
}
bool isValidPart2(int nr) {
return isAdjacentNoMoreThen2(nr) & neverDecrease(nr);
}
int part_1(int n = 109165, int m = 576723) {
int sol = 0;
for (int i = n; i <= m; ++i) {
if (isValidPart1(i)) sol++;
}
return sol;
}
int part_2(int n = 109165, int m = 576723) {
int sol = 0;
for (int i = n; i <= m; ++i) {
if (isValidPart2(i)) sol++;
}
return sol;
}
}
#ifndef TESTING
int main() {
std::cout << "Part 1: " << aoc2019_day04::part_1() << std::endl;
std::cout << "Part 2: " << aoc2019_day04::part_2() << std::endl;
return 0;
}
#endif