-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay10.cpp
More file actions
114 lines (104 loc) · 2.74 KB
/
Day10.cpp
File metadata and controls
114 lines (104 loc) · 2.74 KB
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
#include <bits/stdc++.h>
#define int long long
#define allr(x) (x).rbegin(), (x).rend()
#define all(x) (x).begin(), (x).end()
#define ld long double
#define INF (int)(1e18)
#define MOD 1000000007
using namespace std;
int bitmask(vector<vector<int> > &sets, string &x) {
bitset<10> num;
vector<int> button;
for (int i = 0; i < x.size(); i++) {
if (x[i] == '#') {
num[i] = 1;
} else {
num[i] = 0;
}
}
int tar = num.to_ulong();
for (int i = 0; i < sets.size(); i++) {
bitset<10> num2 = 0;
for (int j = 0; j < sets[i].size(); j++) {
num2[sets[i][j]] = 1;
}
button.push_back(num2.to_ulong());
}
int sz = button.size();
int ans = INF;
for (int i = 0; i < (1 << sz); i++) {
int mask = 0, cnt = 0;
for (int j = 0; j < sz; j++) {
if (i & (1 << j)) {
mask ^= button[j];
cnt++;
}
}
if (mask == tar) {
if (cnt < ans) {
ans = cnt;
}
}
}
return ans;
}
void Part1() {
string s;
int ans = 0;
while (getline(cin, s) && s != "*") {
vector<int> tar;
vector<vector<int> > sets;
string x = "";
for (int i = 0; i < s.length(); i++) {
if (s[i] == '#' || s[i] == '.') {
x += s[i];
}
if (s[i] == '{') {
i++;
while (i < s.size() && s[i] != '}') {
if (s[i] >= '0' && s[i] <= '9') {
string c = "";
while (i < s.size() && s[i] >= '0' && s[i] <= '9') {
c += s[i];
i++;
}
if (!c.empty()) {
int cc = stoi(c);
tar.push_back(cc);
}
}
i++;
}
}
if (s[i] == '(') {
vector<int> v;
i++;
while (i < s.size() && s[i] != ')') {
if (s[i] >= '0' && s[i] <= '9') {
int num = s[i] - '0';
v.push_back(num);
}
i++;
}
sets.push_back(v);
}
}
ans += bitmask(sets, x);
}
cout << ans << "\n";
}
void Part2() {
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen("promote.in","r",stdin);
// freopen("promote.out","w",stdout);
int tests = 1;
// cin >> tests;
while (tests--) {
Part1();
//Part2();
}
}