-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_17a.cpp
145 lines (132 loc) · 3.56 KB
/
day_17a.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
#include <array>
#include <fstream>
#include <iostream>
#include <string>
#include <unordered_set>
#include <vector>
struct Point {
Point(const int row, const int col) : row (row), col(col) {}
int row;
int col;
};
using Rock = std::vector<Point>;
void move_to_starting_height(Rock& rock, int highest) {
for (auto& ele : rock) {
ele.row += highest + 3;
}
}
void move(Rock& rock) {
for (auto& ele : rock) {
ele.row--;
}
}
bool intersection(const std::vector<std::array<char, 7>>& chamber, const Rock& rock) {
for (const auto& ele : rock) {
if (ele.row < chamber.size()) {
if (chamber[ele.row][ele.col] == '#') {
return true;
}
}
}
return false;
}
void add_rock_to_chamber(std::vector<std::array<char, 7>>& chamber, const Rock& rock) {
for (const auto& ele : rock) {
while(chamber.size() <= ele.row) {
std::array<char, 7> temp;
std::fill(std::begin(temp), std::end(temp), '.');
chamber.push_back(temp);
}
}
for (const auto& ele : rock) {
chamber[ele.row][ele.col] = '#';
}
}
void apply_jet(Rock& rock, const std::string& jets, const int jet_index, const std::vector<std::array<char, 7>>& chamber) {
const auto prev = rock;
if (const char j = jets[jet_index]; j == '>') {
for (auto& ele : rock) {
ele.col++;
if (ele.col > 6) {
rock = prev;
return;
}
}
} else if (j == '<') {
for (auto& ele : rock) {
ele.col--;
if (ele.col < 0) {
rock = prev;
return;
}
}
}
if (intersection(chamber, rock)) {
rock = prev;
}
}
void print(const Rock& rock) {
for (const auto ele : rock) {
std::cout << '(' << ele.row << ", " << ele.col << ')' << '\n';
}
std::cout << '\n';
}
void print_chamber(const std::vector<std::array<char, 7>>& chamber) {
std::cout << chamber.size() << '\n';
for (int i = chamber.size()-1; i > 0; i--) {
std::cout << "|";
for (const auto & ele : chamber[i]) {
std::cout << ele;
}
std::cout << "|" << '\n';
}
std::cout << "+";
for (int i = 0; i < chamber[0].size()+2; i++) {
std::cout << '-';
}
std::cout << "+" << '\n';
}
int main(int argc, char * argv[]) {
std::string input = "../input/day_17_input";
if (argc > 1) {
input = argv[1];
}
std::string jets;
std::fstream file(input);
std::getline(file, jets);
std::vector<Rock> rocks {
Rock{Point(0,0), Point(0,1), Point(0,2), Point(0,3)},
Rock{Point(0,1), Point(1,0), Point(1,1), Point(1,2), Point(2,1)},
Rock{Point(2,2), Point(1,2), Point(0,0), Point(0,1), Point(0,2)},
Rock{Point(0,0), Point(1,0), Point(2,0), Point(3,0)},
Rock{Point(0,0), Point(0,1), Point(1,0), Point(1,1)}
};
for (auto& rock : rocks) {
for (auto& ele : rock) {
ele.col += 2;
}
}
int highest = 1;
int rock_count = 0;
int jet_index = 0;
std::vector<std::array<char, 7>> chamber;
std::array<char, 7> temp;
std::fill(std::begin(temp), std::end(temp), '#');
chamber.push_back(temp);
while (rock_count < 2022) {
auto rock = rocks[rock_count % 5];
rock_count++;
move_to_starting_height(rock, highest);
auto prev = rock;
while (!intersection(chamber, rock)) {
apply_jet(rock, jets, jet_index, chamber);
jet_index++;
if (jet_index == jets.size()) jet_index = 0;
prev = rock;
move(rock);
}
add_rock_to_chamber(chamber, prev);
highest = chamber.size();
}
std::cout << highest - 1 << '\n';
}