-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_18a.cpp
68 lines (62 loc) · 1.6 KB
/
day_18a.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
#include <algorithm>
#include <array>
#include <fstream>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
using Point = std::array<int, 3>;
struct PointHash {
std::size_t operator () (const Point& p) const {
std::size_t ans = 0;
for (const auto ele : p) {
ans = ans * 10 + ele;
}
return ans;
}
};
int main(int argc, char * argv[]) {
std::string input = "../input/day_18_input";
if (argc > 1) {
input = argv[1];
}
std::string line;
std::fstream file(input);
std::unordered_map<Point, int, PointHash> points;
const auto get_neighbours = [] (const Point& p) {
return std::array<Point, 6>{{
Point{{p[0]-1, p[1], p[2]}},
Point{{p[0]+1, p[1], p[2]}},
Point{{p[0], p[1]-1, p[2]}},
Point{{p[0], p[1]+1, p[2]}},
Point{{p[0], p[1], p[2]-1}},
Point{{p[0], p[1], p[2]+1}}
}};
};
while(std::getline(file, line)) {
std::size_t start = 0;
std::size_t end = line.find(',');
Point p;
int index = 0;
while(end != std::string::npos) {
p[index] = std::stoi(line.substr(start, end - start));
start = end + 1;
end = line.find(',', start);
index++;
}
p[index] = std::stoi(line.substr(start, end - start));
points[p] = 0;
for (const auto& neighbour: get_neighbours(p)) {
if (const auto it = points.find(neighbour); it != points.end()) {
points[p]++;
it->second += 1;
}
}
}
int total_sa = points.size() * 6;
for (const auto& [point, n_neighbours] : points) {
total_sa -= n_neighbours;
}
std::cout << total_sa << '\n';
return 0;
}