-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day 12.cpp
159 lines (145 loc) · 3.7 KB
/
Day 12.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
156
157
158
159
#include <iostream>
#include <istream>
#include <fstream>
#include <ostream>
#define cin infile
#define cout outfile
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
using namespace std;
bool isMatchingCount(string row, vector<int> currArr)
{
int hCount = 0;
int index = 0;
bool continueProcessing = true;
for (auto r : row)
{
if (r == '#')
{
hCount++;
if (index >= currArr.size() || hCount > currArr[index])
return false;
}
if (r == '.' && hCount)
{
if (index >= currArr.size() || hCount != currArr[index])
return false;
index++;
hCount = 0;
}
if (r == '?')
{
return continueProcessing;
}
}
if (hCount)
{
if (index >= currArr.size() || hCount != currArr[index])
return false;
index++;
hCount = 0;
}
return index == currArr.size();
}
bool hasNoQuotes(string row)
{
for (auto r : row)
if (r == '?')
return false;
return true;
}
long long countArrs(string row, vector<int> arrangement, set<string> &visited)
{
// cout << row << endl;
if (visited.count(row))
return 0;
if (!isMatchingCount(row, arrangement))
{
visited.insert(row);
return 0;
}
if (hasNoQuotes(row))
{
if (visited.count(row))
return 0;
visited.insert(row);
return 1;
}
long long subRes = 0;
for (int q = 0; q < row.size(); q++)
{
if (row[q] == '?')
{
row[q] = '#';
if (!visited.count(row))
subRes += countArrs(row, arrangement, visited);
visited.insert(row);
row[q] = '.';
if (!visited.count(row))
subRes += countArrs(row, arrangement, visited);
visited.insert(row);
}
}
visited.insert(row);
return subRes;
}
int main()
{
ifstream infile("input.txt");
ofstream outfile("output.txt");
string input;
long long res = 0;
while (getline(cin, input))
{
string chars = "";
bool charMode = true;
bool numDetected = false;
vector<int> desiredArrs;
int number = 0;
for (int it = 0; it < input.size(); it++)
{
if (input[it] == ' ')
charMode = false;
if (charMode)
chars.push_back(input[it]);
if (input[it] >= '0' && input[it] <= '9')
{
numDetected = true;
number *= 10;
number += input[it] - '0';
}
else if (numDetected)
{
desiredArrs.push_back(number);
number = 0;
numDetected = false;
}
}
desiredArrs.push_back(number);
set<string> visited;
string c5 = chars + '?';
string c5x = '?' + chars;
string c6x = '?' + chars + '?';
vector<int> dA5 = desiredArrs;
long long p1 = countArrs(chars, desiredArrs, visited);
visited.clear();
long long p2 = countArrs(c5, dA5, visited);
visited.clear();
long long p3 = countArrs(c5x, dA5, visited);
visited.clear();
long long p4 = countArrs(c6x, dA5, visited);
long long hs = max({
p1 * p2 * p2 * p2 * p2,
p1 * p3 * p3 * p3 * p3,
p1 * p1 * p1 * p4 * p4,
p4 * p1 * p2 * p2 * p1,
p4 * p1 * p3 * p3 * p1,
});
res += chars[chars.size() - 1] == '#' || chars[0] == '#' ? p1 : hs;
}
cout << res << endl;
return 0;
}