-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc1801_frequency.cpp
77 lines (66 loc) · 1.83 KB
/
aoc1801_frequency.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
/* ******************************************** */
/* */
/* ~ ~ ~ ~ ~ ~ */
/* ~ _ ~ _ o> */
/* ~ \ / \ / \ / ~ */
/* ~ ~ ~ ~ */
/* */
/* ******************************************** */
#include "iostream"
#include "vector"
int atoi_here_and_now(std::string s);
// DRIVE
int main()
{
int op, fq, t, f, i;
int toggle, count;
std::vector< int > arr_op, arr_fq;
std::string s;
// Part 1
fq = 0;
while (std::cin >> s)
{
op = atoi_here_and_now(s);
arr_op.push_back(op);
fq += op;
}
f = fq;
// Part 2
toggle = count = fq = i = 0;
while (i < (int) arr_op.size() && !toggle)
{
fq += arr_op[i];
if (std::find(arr_fq.begin(), arr_fq.end(), fq) != arr_fq.end())
{
t = fq;
toggle = 1;
}
arr_fq.push_back(fq);
count++;
i++;
if (i == (int) arr_op.size())
i = 0;
}
std::cout << "Star 1 : " << f << std::endl;
std::cout << "Star 2 : " << t << " found at step ";
std::cout << count << std::endl;
}
//
int atoi_here_and_now(std::string s)
{
int sign, tt, i;
tt = i = 0;
sign = 1;
while (s[i])
{
if (s[i] == '+') i++;
else if (s[i] == '-')
{
sign = -1;
i++;
}
tt = tt * 10 + s[i] - '0';
i++;
}
return (sign * tt);
}