-
Notifications
You must be signed in to change notification settings - Fork 0
/
day8.cpp
72 lines (65 loc) · 1.42 KB
/
day8.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
#include <iostream>
#include <set>
#include "data.h"
std::map<char, std::string> getReplacements()
{
std::map<char, std::string> ret{{'+', " "}};
return ret;
}
long run(Data& data, bool& status)
{
long ip = 0;
long acc = 0;
std::set<long> ips;
do
{
if (ip == data.lines.size())
{
status = true;
return acc;
}
ips.insert(ip);
if (data.lines[ip].words[0] == "nop")
{
ip++;
}
else if (data.lines[ip].words[0] == "acc")
{
acc += data.lines[ip].numbers[1];
ip++;
}
else if (data.lines[ip].words[0] == "jmp")
{
ip += data.lines[ip].numbers[1];
}
if (ips.find(ip) != ips.end())
{
status = false;
return acc;
}
}
while(true);
}
void process(Data& data)
{
bool status = true;
std::cout << run(data, status) << std::endl;
auto lines0 = data.lines;
for(int i=0; i < data.lines.size(); i++)
{
data.lines = lines0;
if (data.lines[i].words[0] == "nop")
{
data.lines[i].words[0] = "jmp";
}
else if (data.lines[i].words[0] == "jmp")
{
data.lines[i].words[0] = "nop";
}
long acc = run(data, status);
if (status)
{
std::cout << acc << std::endl;
}
}
}