-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15.cpp
138 lines (118 loc) · 2.84 KB
/
15.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
#include <fstream>
#include <iostream>
#include <vector>
int box_count(std::vector<std::string>& grid) {
int count = 0;
for (int i = 0; i != grid.size(); ++i)
for (int j = 0; j != grid[i].size(); ++j)
if (grid[i][j] == 'O')
count++;
return count;
}
void print_grid(std::vector<std::string>& grid) {
for (int i = 0; i != grid.size(); ++i)
std::cout << grid[i] << std::endl;
}
void p1() {
// Read file
std::ifstream f("input");
if (!f.is_open()) {
std::cerr << "No input file found" << std::endl;
return;
}
// Create grid and moves list from file
std::string line;
std::vector < std::string > grid;
std::string moves = "";
bool before = true;
while (getline(f, line)) {
if (line == "")
before = false;
if (before)
grid.push_back(line);
else
moves += line;
}
// Find bot location
int x, y;
for (int i = 0; i != grid.size(); ++i)
for (int j = 0; j != grid[i].size(); ++j)
if (grid[i][j] == '@') {
x = i;
y = j;
break;
}
// Perform moves
for (char c: moves) {
int dx = 0, dy = 0;
switch (c) {
case '<':
dy = -1;
break;
case '>':
dy = 1;
break;
case '^':
dx = -1;
break;
case 'v':
dx = 1;
break;
default:
break;
}
int cx = x + dx;
int cy = y + dy;
// Check in range
if (cx > 0 && cx < grid.size() && cy > 0 && cy < grid[cx].size()) {
if (grid[cx][cy] == '.') {
grid[x][y] = '.';
x = cx;
y = cy;
grid[x][y] = '@';
} else if (grid[cx][cy] == 'O') {
std::vector < std::pair <int, int>> boxes;
int check_x = cx;
int check_y = cy;
while (check_x > 0 && check_x < grid.size() && check_y > 0 &&
check_y < grid[check_x].size() &&
grid[check_x][check_y] == 'O') {
boxes.push_back({
check_x,
check_y
});
check_x += dx;
check_y += dy;
}
if (check_x > 0 && check_x < grid.size() && check_y > 0 &&
check_y < grid[check_x].size() && grid[check_x][check_y] == '.') {
for (int i = boxes.size() - 1; i >= 0; i--) {
int from_x = boxes[i].first;
int from_y = boxes[i].second;
int to_x = from_x + dx;
int to_y = from_y + dy;
grid[from_x][from_y] = '.';
grid[to_x][to_y] = 'O';
}
// Move player
grid[x][y] = '.';
x = cx;
y = cy;
grid[x][y] = '@';
}
}
}
}
// Calculate sum
int sum = 0;
for (int i = 0; i != grid.size(); ++i)
for (int j = 0; j != grid[i].size(); ++j)
if (grid[i][j] == 'O')
sum += i * 100 + j;
print_grid(grid);
std::cout << sum << std::endl;
}
int main() {
p1();
return 0;
}