-
Notifications
You must be signed in to change notification settings - Fork 9
/
day_22a.cpp
152 lines (141 loc) · 4.11 KB
/
day_22a.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
147
148
149
150
151
152
#include <algorithm>
#include <fstream>
#include <iostream>
#include <regex>
#include <string>
#include <vector>
struct Point {
Point (const long long row = 0, const long long col = 0) : row(row), col(col) {}
long long row, col;
bool operator == (const Point& p) const {
return p.row == row && p.col == col;
}
friend std::ostream& operator<<(std::ostream& os, const Point& p);
};
std::ostream& operator<<(std::ostream& os, const Point& p) {
os << '(' << p.row << ' ' << p.col << ')' << '\n';
return os;
}
Point move(const std::vector<std::string>& grid, const int dir, const int n, const Point& start) {
int i = 0;
auto current = start;
while (i < n) {
auto prev_acceptable = current;
if (dir == 0) {
current.col++;
if (current.col >= grid[0].size()) current.col = 0;
if(grid[current.row][current.col] == '#') {
current.col--;
break;
}
while (grid[current.row][current.col] == ' ') {
current.col++;
if (current.col >= grid[0].size()) current.col = 0;
}
} else if (dir == 1) {
current.row++;
if (current.row >= grid.size()) current.row = 0;
if(grid[current.row][current.col] == '#') {
current.row--;
break;
}
while (grid[current.row][current.col] == ' ') {
current.row++;
if (current.row >= grid.size()) current.row = 0;
}
} else if (dir == 2) {
current.col--;
if (current.col < 0) current.col = grid[0].size()-1;
if(grid[current.row][current.col] == '#') {
current.col++;
break;
}
while (grid[current.row][current.col] == ' ') {
current.col--;
if (current.col < 0) current.col = grid[0].size()-1;
}
} else if (dir == 3) {
current.row--;
if (current.row < 0) current.row = grid.size()-1;
if(grid[current.row][current.col] == '#') {
current.row++;
break;
}
while (grid[current.row][current.col] == ' ') {
current.row--;
if (current.row < 0) current.row = grid.size()-1;
}
}
if(grid[current.row][current.col] == '#') {
current = prev_acceptable;
}
i++;
// std::cout << current << '\n';
}
return current;
}
int main(int argc, char * argv[]) {
std::string input = "../input/day_22_input";
if (argc > 1) {
input = argv[1];
}
std::string line;
std::fstream file(input);
std::vector<std::string> grid;
int dir = 0;
while(std::getline(file, line)) {
if (line == "") break;
grid.push_back(line);
}
std::size_t max_len = 0;
for (const auto& row : grid) {
max_len = std::max(max_len, row.size());
}
for (auto& row : grid) {
if (row.size() < max_len) {
row.resize(max_len, ' ');
}
// std::cout << '|' << row << '|' << '\n';
}
auto start = Point();
for (int i = 0; i < grid[0].size(); i++) {
if(grid[0][i] == '.') {
start.row = 0;
start.col= i;
break;
}
}
// std::cout << start;
// exit(0);
std::getline(file, line);
const std::string path = line;
// std::cout << line << '\n';
std::vector<std::size_t> direction_indixes;
for (int i = 0; i < path.size(); i++) {
if (path[i] == 'L' || path[i] == 'R') {
direction_indixes.push_back(i);
}
}
std::size_t start_i = 0;
auto current = start;
for (int i = 0; i < direction_indixes.size(); i++) {
const int delta_val = std::stoi(path.substr(start_i, direction_indixes[i] - start_i));
// std::cout << delta_val << '\n';
current = move(grid, dir, delta_val, current);
// std::cout << "Now at: " << current << '\n';
const char delta_dir = path[direction_indixes[i]];
// std::cout << '|' << delta_dir << '|' << '\n';
dir += (delta_dir == 'R' ? 1 : -1);
dir += 4;
dir = dir % 4;
start_i = direction_indixes[i]+1;
}
if (direction_indixes.back() != path.size()) {
const int delta_val = std::stoi(path.substr(start_i, path.size() - start_i));
current = move(grid, dir, delta_val, current);
// std::cout << current << '\n';
}
// std::cout << dir << '\n';
std::cout << 1000 * (current.row+1) + 4 * (current.col+1) + dir << '\n';
return 0;
}