-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_15a.cpp
117 lines (109 loc) · 3.29 KB
/
day_15a.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
#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 move_obstacles(const Point& start, const Point& possible_move, std::vector<std::string>& map) {
auto current = start;
while(map[current.row][current.col] == 'O') {
current.row += possible_move.row;
current.col += possible_move.col;
}
if (map[current.row][current.col] == '.') {
std::swap(map[current.row][current.col], map[start.row][start.col]);
return true;
}
return false;
}
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()) {
map.push_back(line);
for (const auto [idx,ele] : std::views::enumerate(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] == 'O') {
if(move_obstacles(new_point, possible_moves[move_idx], map)) {
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] == '.') {
std::swap(map[new_point.row][new_point.col], map[current.row][current.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 == 'O') total += (100 * row_idx + col_idx);
}
}
std::cout << total << '\n';
return 0;
}