-
Notifications
You must be signed in to change notification settings - Fork 9
/
day_09a.cpp
78 lines (73 loc) · 2.56 KB
/
day_09a.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
#include <algorithm>
#include <fstream>
#include <iostream>
#include <limits>
#include <regex>
#include <string>
#include <vector>
struct Distance {
std::string from;
std::string to;
int value;
};
void util (int current, int& min_distance, int distance, std::vector<bool>& visited, const std::vector<std::vector<int>>& am) {
if (std::all_of(visited.begin(), visited.end(), [](const auto ele ) {return ele;})) {
min_distance = std::min(distance, min_distance);
return;
}
for (int i = 0; i < visited.size(); i++) {
if (am[current][i] == std::numeric_limits<int>::max()) continue;
if (visited[i]) continue;
visited[i] = true;
distance += am[current][i];
util(i, min_distance, distance, visited, am);
distance -= am[current][i];
visited[i] = false;
}
}
int find_shortest_path (const std::vector<std::string>& cities, const std::vector<std::vector<int>>& am) {
std::vector<bool> visited(cities.size(), false);
int min_distance = std::numeric_limits<int>::max();
int distance = 0;
for (int start_city = 0; start_city < cities.size(); start_city++) {
visited[start_city] = true;
util(start_city, min_distance, distance, visited, am);
visited[start_city] = false;
}
return min_distance;
}
int main(int argc, char* argv[]) {
std::string input = "../input/day_09_input";
if (argc > 1) {
input = argv[1];
}
std::ifstream file(input);
std::string line;
std::vector<Distance> distances;
const std::regex pattern(R"(([a-zA-Z]+) to ([a-zA-Z]+) = ([0-9]+))");
while(std::getline(file, line)) {
std::smatch match;
std::regex_search(line, match, pattern);
Distance distance;
distance.from = match[1];
distance.to = match[2];
distance.value = std::stoi(match[3]);
distances.push_back(distance);
}
std::vector<std::string> cities;
for (const auto& d : distances) {
cities.push_back(d.from);
cities.push_back(d.to);
}
std::sort(std::begin(cities), std::end(cities));
cities.erase(std::unique(cities.begin(), cities.end()), cities.end());
std::vector<std::vector<int>> am(cities.size(), std::vector<int>(cities.size(), std::numeric_limits<int>::max()));
for (const auto& d : distances) {
const int i1 = std::distance(cities.begin(), std::find(cities.begin(), cities.end(), d.from));
const int i2 = std::distance(cities.begin(), std::find(cities.begin(), cities.end(), d.to));
am[i1][i2] = d.value;
am[i2][i1] = d.value;
}
std::cout << find_shortest_path(cities, am) << '\n';
return 0;
}