-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
115 lines (100 loc) · 4.19 KB
/
main.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
#include "file.h"
#include "utilities.h"
#include <iostream>
#include <vector>
namespace aoc2017_day24 {
void dfs(const std::vector<std::vector<int>>& connections, std::vector<std::pair<int, int>>& nodes, int& maxStrength) {
for (int i = 0; i < connections[nodes.back().first].size(); ++i) {
int value = connections[nodes.back().first][i];
auto it = std::find_if(nodes.begin(), nodes.end(), [value](auto p) {
return p.second == value;
});
if (value != -1 && it == nodes.end()) {
nodes.emplace_back(i, value);
int sum = 0;
for (int j = 1; j < nodes.size(); ++j) {
sum += nodes[j].first + nodes[j - 1].first;
}
maxStrength = std::max(maxStrength, sum);
dfs(connections, nodes, maxStrength);
nodes.pop_back();
}
}
}
void dfs(const std::vector<std::vector<int>>& connections, std::vector<std::pair<int, int>>& nodes, int& maxStrength, int& maxLength) {
for (int i = 0; i < connections[nodes.back().first].size(); ++i) {
int value = connections[nodes.back().first][i];
auto it = std::find_if(nodes.begin(), nodes.end(), [value](auto p) {
return p.second == value;
});
if (value != -1 && it == nodes.end()) {
nodes.emplace_back(i, value);
int sum = 0;
for (int j = 1; j < nodes.size(); ++j) {
sum += nodes[j].first + nodes[j - 1].first;
}
if (nodes.size() > maxLength) {
maxLength = nodes.size();
maxStrength = sum;
}
else if (nodes.size() == maxLength) {
maxStrength = std::max(sum, maxStrength);
}
dfs(connections, nodes, maxStrength, maxLength);
nodes.pop_back();
}
}
}
int part_1(std::string_view path) {
std::vector<std::pair<int, int>> connections;
std::vector<std::string> lines = file::readFileAsArrayString(path);
for (const auto& line : lines) {
std::vector<int> tokens = utils::splitStringToInt(line, "/");
connections.emplace_back(tokens[0], tokens[1]);
}
int max = std::numeric_limits<int>::min();
for (const auto& c : connections) {
max = std::max({max, c.first, c.second});
}
std::vector<std::vector<int>> map(max + 1, std::vector<int>(max + 1, -1));
for (int i = 0; i < connections.size(); ++i) {
map[connections[i].first][connections[i].second] = i;
map[connections[i].second][connections[i].first] = i;
}
std::vector<std::pair<int, int>> nodes;
nodes.emplace_back(0, -1);
int maxStrength = 0;
dfs(map, nodes, maxStrength);
return maxStrength;
}
int part_2(std::string_view path) {
std::vector<std::pair<int, int>> connections;
std::vector<std::string> lines = file::readFileAsArrayString(path);
for (const auto& line : lines) {
std::vector<int> tokens = utils::splitStringToInt(line, "/");
connections.emplace_back(tokens[0], tokens[1]);
}
int max = std::numeric_limits<int>::min();
for (const auto& c : connections) {
max = std::max({max, c.first, c.second});
}
std::vector<std::vector<int>> map(max + 1, std::vector<int>(max + 1, -1));
for (int i = 0; i < connections.size(); ++i) {
map[connections[i].first][connections[i].second] = i;
map[connections[i].second][connections[i].first] = i;
}
std::vector<std::pair<int, int>> nodes;
nodes.emplace_back(0, -1);
int maxStrength = 0;
int maxLength = 0;
dfs(map, nodes, maxStrength, maxLength);
return maxStrength;
}
}
#ifndef TESTING
int main() {
std::cout << "Part 1: " << aoc2017_day24::part_1("../2017/day24/input.in") << std::endl;
std::cout << "Part 2: " << aoc2017_day24::part_2("../2017/day24/input.in") << std::endl;
return 0;
}
#endif