-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc2008_accumulator.cpp
155 lines (134 loc) Β· 5.16 KB
/
aoc2008_accumulator.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* ************************************************************************** */
/* */
/* \\ /`/`` */
/* ~\o o_ 0 0\ */
/* / \__) (u ); _ _ */
/* / \/ \/ / \ \/ \/ \ */
/* /( . . ) ( )\ */
/* / \_____/ \_______/ \ */
/* [] [] [[] [[] *. */
/* []] []] [[] [[] */
/* */
/* ****************************************************************** nuo *** */
#include "iostream"
#include "vector"
#include "utility"
int count_after_end(int p, std::vector<std::pair<std::string, std::pair<int, bool> > > i);
int count_before_rep(std::vector<std::pair<std::string, std::pair<int, bool> > > i);
std::vector<int> find_nop_and_jmp(std::vector<std::pair<std::string, std::pair<int, bool> > > i);
bool check_pos(int p, std::vector<std::pair<std::string, std::pair<int, bool> > > i);
void put_ops(std::vector<std::pair<std::string, std::pair<int, bool> > > i);
int main()
{
std::vector< std::pair<
std::string, std::pair< int, bool > > > ins;
std::vector< int > pos;
std::string s;
int acc_before_rep, acc_after_end, i;
while (std::cin >> s >> i)
{
std::pair< int, bool > command = std::make_pair(i, false);
std::pair< std::string, std::pair<int, bool> >
op = std::make_pair( s, command );
ins.push_back(op);
}
//put_ops(ins); // test
acc_before_rep = count_before_rep(ins);
pos = find_nop_and_jmp(ins);
i = -1;
while (++i < (int) pos.size())
{
if (check_pos(pos[i], ins)) break;
}
acc_after_end = count_after_end(pos[i], ins);
std::cout << "Star 1 : " << acc_before_rep << std::endl;
std::cout << "Star 2 : " << acc_after_end << std::endl;
return (0);
}
//
int count_after_end(int p, std::vector< std::pair< std::string, std::pair< int, bool > > > ins)
{
int accumulator, i;
if (ins[p].first == "nop") ins[p].first = "jmp";
else ins[p].first = "nop";
i = accumulator = 0;
while (i < (int) ins.size())
{
if (ins[i].first == "nop") i += 1;
else if (ins[i].first == "jmp") i += ins[i].second.first;
else if (ins[i].first == "acc")
{
accumulator += ins[i].second.first;
i += 1;
}
}
return (accumulator);
}
bool check_pos(int p, std::vector<std::pair< std::string, std::pair<int, bool> > > ins)
{
int i;
if (ins[p].first == "nop") ins[p].first = "jmp";
else ins[p].first = "nop";
i = 0;
while (i < (int) ins.size())
{
if ( ins[i].second.second == true ) return (false);
else if (ins[i].first == "nop" || ins[i].first == "acc")
{
ins[i].second.second = true;
i += 1;
}
else if (ins[i].first == "jmp")
{
ins[i].second.second = true;
i += ins[i].second.first;
}
}
return (true);
}
std::vector< int > find_nop_and_jmp(std::vector<std::pair< std::string, std::pair< int, bool > > > n)
{
std::vector< int > res;
int i;
i = -1;
while (++i < (int) n.size())
{
if (n[i].first == "nop" || n[i].first == "jmp") res.push_back(i);
}
return (res);
}
int count_before_rep(std::vector< std::pair< std::string, std::pair< int, bool > > > ins)
{
int accumulator, i;
i = accumulator = 0;
while (1)
{
if ( ins[i].second.second == true ) break;
else if ( ins[i].first == "nop" )
{
ins[i].second.second = true;
i += 1;
}
else if ( ins[i].first == "jmp" )
{
ins[i].second.second = true;
i += ins[i].second.first;
}
else if ( ins[i].first == "acc" )
{
accumulator += ins[i].second.first;
ins[i].second.second = true;
i += 1;
}
}
return (accumulator);
}
void put_ops(std::vector< std::pair<
std::string, std::pair< int, bool > > > ins)
{
int i = -1;
while (++i < (int) ins.size())
{
std::cout << ins[i].first << " " << ins[i].second.first << " " << ins[i].second.second << std::endl;
}
}