-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_24b.cpp
80 lines (74 loc) · 2.05 KB
/
day_24b.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
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <limits>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <regex>
#include <cmath>
#include <queue>
#include <utility>
struct Component {
std::array<int, 2> ends;
bool reverse = false;
bool used = false;
int first() {
if (reverse) return ends[1];
return ends[0];
}
int second() {
if (reverse) return ends[0];
return ends[1];
}
std::pair<bool, int> check_match(const int n) {
if (n == ends[0]) return {true, 0};
else if (n == ends[1]) return {true, 1};
return {false, -1};
}
};
// TODO: refactor this
void dfs (std::vector<Component>& components, const int pins, std::size_t& ans, std::size_t& current_total, const int depth, std::size_t& max_depth) {
for (int c_idx = 0; c_idx < components.size(); c_idx++) {
auto& c = components[c_idx];
if (c.used) continue;
if (const auto [match, idx] = c.check_match(pins); match) {
if (idx == 1) {
c.reverse = true;
}
c.used = true;
current_total += (c.first() + c.second());
if (depth >= max_depth) {
ans = std::max(ans, current_total);
max_depth = depth;
}
dfs(components, c.second(), ans, current_total, depth + 1, max_depth);
c.used = false;
current_total -= (c.first() + c.second());
if (idx == 1) {
c.reverse = false;
}
}
}
}
int main(int argc, char* argv[]) {
const std::string input = (argc > 1) ? argv[1] : "../input/day_24_input" ;
std::ifstream file(input);
std::string line;
std::vector<Component> components;
while(std::getline(file, line)) {
Component c;
const auto idx = line.find('/');
c.ends[0] = std::stoi(line.substr(0, idx));
c.ends[1] = std::stoi(line.substr(idx + 1, line.size() - idx - 1));
components.push_back(c);
}
// exit(0);
std::size_t ans = 0;
std::size_t current_total = 0;
std::size_t max_depth = 0;
dfs(components, 0, ans, current_total, 0, max_depth);
std::cout << ans << '\n';
return 0;
}