-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_15b.cpp
126 lines (109 loc) · 3.09 KB
/
day_15b.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
116
117
118
119
120
121
122
123
124
125
126
#include <fstream>
#include <iostream>
#include <queue>
#include <string>
#include <unordered_set>
#include <vector>
void print(const std::vector<std::vector<int>>& v) {
for (const auto& row : v) {
for (const auto& col : row) {
std::cout << col ;
}
std::cout << '\n';
}
std::cout << '\n';
}
struct Point {
int row, col;
Point(const int row, const int col) : row(row), col(col) {}
bool operator == (const Point& p) const {
return row == p.row && col == p.col;
}
friend std::ostream& operator << (std::ostream& os, const Point& p);
};
struct compare {
bool operator () (const std::pair<Point, int>& p1, const std::pair<Point, int>& p2) const {
return p1.second > p2.second;
}
};
std::ostream& operator << (std::ostream& os, const Point& p) {
os << "(" << p.row << ", " << p.col << ")";
return os;
}
struct hash_point {
std::size_t operator () (const Point& p) const {
return p.row * p.col;
}
};
int main(int argc, char * argv[]) {
std::string input = "../input/day_15_input";
if (argc > 1) {
input = argv[1];
}
std::string line;
std::fstream file(input);
std::vector<std::vector<int>> input_map;
while(std::getline(file, line)) {
input_map.emplace_back();
for (const auto ele : line) {
input_map.back().emplace_back(ele - '0');
}
}
std::vector<std::vector<int>> map(
input_map.size() * 5,
std::vector<int>(input_map[0].size() * 5, 0)
);
int offset = 0;
for (int m = 0; m < (5 * 5); m++) {
const auto row_offset = (offset / 5);
const auto col_offset = (offset % 5);
const auto row_offset_m = row_offset * input_map.size();
const auto col_offset_m = col_offset * input_map[0].size();
for (int i = 0; i < input_map.size(); i++) {
for (int j = 0; j < input_map[0].size(); j++) {
auto& v = map[i + row_offset_m][j + col_offset_m];
v = (input_map[i][j] + (row_offset + col_offset));
if (v > 9) {
v = v % 9;
if (v == 0) {
v = 1;
}
}
}
}
offset += 1;
}
std::priority_queue<std::pair<Point, int>, std::vector<std::pair<Point, int>>, compare> q;
std::unordered_set<Point, hash_point> already_inserted;
const auto get_neighbours = [](const Point& p) {
return std::vector<Point> {
Point(p.row + 1, p.col),
Point(p.row - 1, p.col),
Point(p.row, p.col + 1),
Point(p.row, p.col - 1)
};
};
const auto n_rows = map.size();
const auto n_cols = map[0].size();
const auto start = Point(0,0);
const auto goal = Point(n_rows - 1, n_cols - 1);
const auto in_bounds = [&n_rows, &n_cols](const Point& p) {
return p.row >= 0 && p.col >=0 && p.row < n_rows && p.col < n_cols;
};
q.emplace(start, 0);
while(!q.empty()) {
const auto [cp, cost] = q.top();
q.pop();
if(cp == goal) {
std::cout << cost << '\n';
break;
}
for (const auto& n : get_neighbours(cp)) {
if (in_bounds(n) && already_inserted.find(n) == already_inserted.end()) {
q.emplace(n, cost + map[n.row][n.col]);
already_inserted.insert(n);
}
}
}
return 0;
}