-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_25a.cpp
110 lines (99 loc) · 2.6 KB
/
day_25a.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
#include <fstream>
#include <iostream>
#include <string>
#include <tuple>
#include <vector>
template<typename T>
void print(std::vector<std::vector<T>>& v) {
for (const auto& row : v) {
for(const auto ele : row) {
std::cout << ele;
}
std::cout << '\n';
}
}
std::tuple<int, int> get_next(
const std::vector<std::vector<char>>&cucumbers,
const int row,
const int col,
const char dir
) {
int next_row = 0;
int next_col = 0;
if (dir == 'v') {
next_col = col;
if ((row + 1) == cucumbers.size()) {
next_row = 0;
} else {
next_row = row + 1;
}
}
else if (dir == '>') {
next_row = row;
if ((col + 1) == cucumbers[0].size()) {
next_col = 0;
} else {
next_col = col + 1;
}
}
return {next_row, next_col};
}
bool move(std::vector<std::vector<char>>& cucumbers) {
bool changed = false;
auto next_step_cucumbers = cucumbers;
int c1 = 0, c2 = 0, c3 = 0;
for (int row = 0; row < cucumbers.size(); row++) {
for (int col = 0; col < cucumbers[row].size(); col++) {
if (cucumbers[row][col] == '>') {
const auto [next_row, next_col] = get_next(cucumbers, row, col, cucumbers[row][col]);
if (cucumbers[next_row][next_col] == '.') {
next_step_cucumbers[next_row][next_col] = cucumbers[row][col];
next_step_cucumbers[row][col] = '.';
changed = true;
}
}
}
}
cucumbers = next_step_cucumbers;
for (int row = 0; row < cucumbers.size(); row++) {
for (int col = 0; col < cucumbers[row].size(); col++) {
if (cucumbers[row][col] == 'v') {
const auto [next_row, next_col] = get_next(cucumbers, row, col, cucumbers[row][col]);
if (cucumbers[next_row][next_col] == '.') {
next_step_cucumbers[next_row][next_col] = cucumbers[row][col];
next_step_cucumbers[row][col] = '.';
changed = true;
}
}
}
}
cucumbers = next_step_cucumbers;
return changed;
}
int main(int argc, char * argv[]) {
std::string input = "../input/day_25_input";
if (argc > 1) {
input = argv[1];
}
std::string line;
std::fstream file(input);
std::vector<std::vector<char>> cucumbers;
while(std::getline(file, line)) {
cucumbers.emplace_back();
for (const auto c : line) {
// Some error in file formatting on local machine;
// adding additional check. TBDebugged later
if (c == '.' || c == '>' || c == 'v') {
cucumbers.back().emplace_back(c);
}
}
}
bool moved = true;
int step = 0;
while(moved) {
moved = move(cucumbers);
step++;
}
std::cout << step << '\n';
return 0;
}