Skip to content

Commit

Permalink
Add 2022 day 17 cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
vss2sn committed Jan 1, 2023
1 parent 296058f commit 12714f4
Show file tree
Hide file tree
Showing 6 changed files with 413 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ temp*
*.sh
*answers*
Advent of code*
backup*
1 change: 1 addition & 0 deletions 2022/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ This folder contains solutions to each of the problems in Advent of Code 2022 in
| <nobr> [Day 14: Regolith Reservoir](https://adventofcode.com/2022/day/14) </nobr> | <nobr> [Part 1](/2022/cpp/day_14a.cpp) [Part 2](/2022/cpp/day_14b.cpp) </nobr> | </nobr> [Link](/2022/input/day_14_input) </nobr> | </nobr> [Link](/2022/sample_input/day_14_sample_input) </nobr> | </nobr> [Link](/2022/puzzles/day_14_puzzle) </nobr> |
| <nobr> [Day 15: Beacon Exclusion Zone](https://adventofcode.com/2022/day/15) </nobr> | <nobr> [Part 1](/2022/cpp/day_15a.cpp) [Part 2](/2022/cpp/day_15b.cpp) </nobr> | </nobr> [Link](/2022/input/day_15_input) </nobr> | </nobr> [Link](/2022/sample_input/day_15_sample_input) </nobr> | </nobr> [Link](/2022/puzzles/day_15_puzzle) </nobr> |
| <nobr> [Day 16: Proboscidea Volcanium](https://adventofcode.com/2022/day/16) </nobr> | <nobr> [Part 1](/2022/cpp/day_16a.cpp) [Part 2](/2022/cpp/day_16b.cpp) </nobr> | </nobr> [Link](/2022/input/day_16_input) </nobr> | </nobr> [Link](/2022/sample_input/day_16_sample_input) </nobr> | </nobr> [Link](/2022/puzzles/day_16_puzzle) </nobr> |
| <nobr> [Day 17: Pyroclastic Flow](https://adventofcode.com/2022/day/17) </nobr> | <nobr> [Part 1](/2022/cpp/day_17a.cpp) [Part 2](/2022/cpp/day_17b.cpp) </nobr> | </nobr> [Link](/2022/input/day_17_input) </nobr> | </nobr> [Link](/2022/sample_input/day_17_sample_input) </nobr> | </nobr> [Link](/2022/puzzles/day_17_puzzle) </nobr> |
174 changes: 174 additions & 0 deletions 2022/cpp/day_17a.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
#include <array>
#include <unordered_set>
#include <fstream>
#include <iostream>
#include <string>
#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) {
// std::cout << jets << '\n';
// for (int i = 0; i < jet_index; i++) {
// std::cout << ' ';
// }
// std::cout << '^';
// std::cout << '\n';
// std::cout << j << '\n';
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);
// print_chamber(chamber);
// exit(0);
// std::cout << __LINE__ <<'\n';
while (rock_count < 2022) {
// std::cout << "---------------- Begin -------------------" << '\n';
auto rock = rocks[rock_count % 5];
rock_count++;
// std::cout << __LINE__ <<'\n';
move_to_starting_height(rock, highest);
// std::cout << __LINE__ <<'\n';
auto prev = rock;
// move(rock);
// print(rock);

while (!intersection(chamber, rock)) {
// std::cout << __LINE__ <<'\n';
apply_jet(rock, jets, jet_index, chamber);
// std::cout << __LINE__ <<'\n';
// print(rock);
// std::cout << __LINE__ <<'\n';
jet_index++;
if (jet_index == jets.size()) jet_index = 0;
prev = rock;
move(rock);
// std::cout << __LINE__ <<'\n';
// print(rock);
}
// print(prev);
// std::cout << __LINE__ <<'\n';
add_rock_to_chamber(chamber, prev);
// std::cout << __LINE__ <<'\n';
// print(chamber);
highest = chamber.size();
// std::cout << "---------------- End -------------------" << '\n';
// print_chamber(chamber);
// if (rock_count == 10) exit(0);
}
std::cout << highest - 1 << '\n';
}
Loading

0 comments on commit 12714f4

Please sign in to comment.