-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_15b.cpp
146 lines (137 loc) · 5.42 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <array>
#include <fstream>
#include <iostream>
#include <ranges>
#include <string>
#include <vector>
struct Point {
int row;
int col;
bool operator == (const Point& p) const {
return row == p.row && col == p.col;
}
bool operator < (const Point& p) const {
return row < p.row || (row == p.row && col < p.col);
}
};
struct Hasher {
std::size_t operator () (const Point& p) const {
return p.row;
}
};
bool in_map(const Point& p, const std::vector<std::string>& map ) {
return p.row >= 0 && p.row < map.size() && p.col >= 0 && p.col < map[0].size();
}
bool attempt_move_obstacles(const Point& current, const Point& possible_move, std::vector<std::string>& map) {
if(map[current.row][current.col] == '.') return true;
if(map[current.row][current.col] == '#') return false;
if(map[current.row][current.col] == '[') {
const bool possible_1 = attempt_move_obstacles(Point(current.row + possible_move.row, current.col + possible_move.col), possible_move, map);
bool possible_2 = true;
if (possible_move.row != 0) possible_2 = attempt_move_obstacles(Point(current.row + possible_move.row, current.col + 1), possible_move, map);
return possible_1 && possible_2;
}
if(map[current.row][current.col] == ']') {
const bool possible_1 = attempt_move_obstacles(Point(current.row + possible_move.row, current.col + possible_move.col), possible_move, map);
bool possible_2 = true;
if (possible_move.row != 0) possible_2 = attempt_move_obstacles(Point(current.row + possible_move.row, current.col - 1), possible_move, map);
return possible_1 && possible_2;
}
return false;
}
void move_obstacles(const Point& current, const Point& possible_move, std::vector<std::string>& map) {
if(map[current.row][current.col] == '.') return;
if (map[current.row][current.col] == '[') {
move_obstacles(Point(current.row + possible_move.row, current.col + possible_move.col), possible_move, map);
std::swap(map[current.row][current.col], map[current.row+possible_move.row][current.col+possible_move.col]);
if (possible_move.row != 0 && map[current.row][current.col+1] == ']') move_obstacles(Point(current.row, current.col + 1), possible_move, map);
}
else if (map[current.row][current.col] == ']') {
move_obstacles(Point(current.row + possible_move.row, current.col + possible_move.col), possible_move, map);
std::swap(map[current.row][current.col], map[current.row+possible_move.row][current.col+possible_move.col]);
if (possible_move.row != 0 && map[current.row][current.col-1] == '[') move_obstacles(Point(current.row, current.col - 1), possible_move, map);
}
}
int main(int argc, char* argv[]) {
std::string input = "../input/day_15_input";
if (argc > 1) {
input = argv[1];
}
std::ifstream file(input);
std::string line;
std::vector<std::string> map;
Point start;
while(std::getline(file, line) && !line.empty()) {
std::string updated_line = "";
for (const auto ele : line) {
if (ele == '#') updated_line += "##";
else if (ele == '.') updated_line += "..";
else if (ele == '@') updated_line += "@.";
else if (ele == 'O') updated_line += "[]";
}
map.push_back(updated_line);
for (const auto [idx,ele] : std::views::enumerate(updated_line)) {
if (ele == '@') {
start.row = map.size()-1;
start.col = idx;
}
}
}
std::string moves;
while(std::getline(file, line)) {
moves += line;
}
Point current = start;
std::array<Point, 4> possible_moves {{
Point(-1,0),
Point(0,1),
Point(1,0),
Point(0,-1)
}};
for (const auto move : moves) {
// for (const auto& row : map) {
// std::cout << row << '\n';
// }
// std::cout << move << '\n';
int move_idx = 0;
if (move == '^') {
move_idx = 0;
} else if (move == '>') {
move_idx = 1;
} else if (move == 'v') {
move_idx = 2;
} else if (move == '<') {
move_idx = 3;
}
Point new_point;
new_point.row = current.row + possible_moves[move_idx].row;
new_point.col = current.col + possible_moves[move_idx].col;
if (!in_map(new_point, map)) {
continue;
}
if (map[new_point.row][new_point.col] == '#') {
continue;
}
if (map[new_point.row][new_point.col] == '.') {
std::swap(map[new_point.row][new_point.col], map[current.row][current.col]);
current = new_point;
}
if (map[new_point.row][new_point.col] == '[' || map[new_point.row][new_point.col] == ']') {
if (attempt_move_obstacles(new_point, possible_moves[move_idx], map)) {
move_obstacles(new_point, possible_moves[move_idx], map);
std::swap(map[current.row][current.col], map[new_point.row][new_point.col]);
current = new_point;
}
}
}
int total = 0;
for (const auto& [row_idx, row] : std::views::enumerate(map)) {
for (const auto& [col_idx, ele] : std::views::enumerate(row)) {
if (ele == '[') {
total += (100 * row_idx + col_idx);
}
}
}
std::cout << total << '\n';
return 0;
}