-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
89 lines (81 loc) · 2.87 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
#include "file.h"
#include "utilities.h"
#include <iostream>
#include <set>
namespace aoc2017_day12 {
int part_1(std::string_view path, int id = 0) {
std::vector<std::pair<int, std::vector<int>>> input;
std::vector<std::string> lines = file::readFileAsArrayString(path);
for (const auto& line : lines) {
std::vector<int> tokens = utils::splitStringToInt(line, " <->,");
input.push_back({tokens[0], {tokens.begin() + 1, tokens.end()}});
}
const int SIZE = 3000;
std::vector<std::vector<bool>> pipes(SIZE, std::vector<bool>(SIZE, false));
for (const auto& line : input) {
for (const auto& x : line.second) {
pipes[line.first][x] = pipes[x][line.first] = true;
}
}
std::vector<bool> visited(SIZE, false);
std::vector<int> sol;
sol.push_back(0);
visited[0] = true;
int i = 0;
while (i < sol.size()) {
for (int j = 0; j < SIZE; ++j) {
if (pipes[sol[i]][j] && !visited[j]) {
sol.push_back(j);
visited[j] = true;
}
}
i++;
}
return sol.size();
}
int part_2(std::string_view path) {
std::vector<std::pair<int, std::vector<int>>> input;
std::vector<std::string> lines = file::readFileAsArrayString(path);
for (const auto& line : lines) {
std::vector<int> tokens = utils::splitStringToInt(line, " <->,");
input.push_back({tokens[0], {tokens.begin() + 1, tokens.end()}});
}
const int SIZE = 3000;
int max = 0;
std::vector<std::vector<bool>> pipes(SIZE, std::vector<bool>(SIZE, false));
for (const auto& line : input) {
for (const auto& x : line.second) {
pipes[line.first][x] = pipes[x][line.first] = true;
}
max = std::max(max, line.first);
}
std::vector<bool> visited(SIZE, false);
int groups = 0;
for (int k = 0; k <= max; ++k) {
if (!visited[k]) {
groups++;
std::vector<int> sol;
sol.push_back(k);
visited[k] = true;
int i = 0;
while (i < sol.size()) {
for (int j = 0; j < SIZE; ++j) {
if (pipes[sol[i]][j] && !visited[j]) {
sol.push_back(j);
visited[j] = true;
}
}
i++;
}
}
}
return groups;
}
}
#ifndef TESTING
int main() {
std::cout << "Part 1: " << aoc2017_day12::part_1("../2017/day12/input.in") << std::endl;
std::cout << "Part 2: " << aoc2017_day12::part_2("../2017/day12/input.in") << std::endl;
return 0;
}
#endif