-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 8.cpp
86 lines (74 loc) · 1.79 KB
/
Day 8.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
#include <iostream>
#include <istream>
#include <fstream>
#include <ostream>
#include <string>
#include <map>
#include <utility>
#include <vector>
#include <numeric>
#define cin infile
#define cout outfile
using namespace std;
bool partTwo = false;
int main()
{
ifstream infile("input.txt");
ofstream outfile("output.txt");
map<string, pair<string, string>> nodesMap;
vector<string> ghostNodes;
if (!partTwo)
ghostNodes.push_back("AAA");
string command;
getline(cin, command);
string input;
getline(cin, input);
while (getline(cin, input))
{
int it = 0;
string nodeId = "";
string left = "", right = "";
while (input[it] != ' ')
{
nodeId.push_back(input[it]);
it++;
}
if (partTwo && nodeId[nodeId.size() - 1] == 'A'){
ghostNodes.push_back(nodeId);
}
while (input[it] != '(')
it++;
it++;
while (input[it] != ',')
{
left.push_back(input[it]);
it++;
}
it += 2;
while (input[it] != ')')
{
right.push_back(input[it]);
it++;
}
nodesMap[nodeId] = make_pair(left, right);
}
int opSize = command.size();
if (!opSize)
return 0;
vector<long long> foundZ;
for(auto &ghost : ghostNodes){
long long i = 0;
while(partTwo ? ghost[2] != 'Z' : ghost.compare("ZZZ")){
ghost = command[i%command.size()] == 'L' ? nodesMap[ghost].first : nodesMap[ghost].second;
i++;
}
foundZ.push_back(i);
}
long long res = foundZ[0];
for(int i = 1; i< foundZ.size(); i++)
{
res = res*foundZ[i] / gcd(res, foundZ[i]);
}
cout << res;
return 0;
}