Skip to content

Commit

Permalink
Add 2022 day 18 cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
vss2sn committed Jan 1, 2023
1 parent 12714f4 commit 87f6ecb
Show file tree
Hide file tree
Showing 7 changed files with 292 additions and 38 deletions.
1 change: 1 addition & 0 deletions 2022/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ This folder contains solutions to each of the problems in Advent of Code 2022 in
| <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> |
| <nobr> [Day 18: Boiling Boulders](https://adventofcode.com/2022/day/18) </nobr> | <nobr> [Part 1](/2022/cpp/day_18a.cpp) [Part 2](/2022/cpp/day_18b.cpp) </nobr> | </nobr> [Link](/2022/input/day_18_input) </nobr> | </nobr> [Link](/2022/sample_input/day_18_sample_input) </nobr> | </nobr> [Link](/2022/puzzles/day_18_puzzle) </nobr> |
31 changes: 1 addition & 30 deletions 2022/cpp/day_17a.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include <array>
#include <unordered_set>
#include <fstream>
#include <iostream>
#include <string>
#include <unordered_set>
#include <vector>

struct Point {
Expand Down Expand Up @@ -50,13 +50,6 @@ void add_rock_to_chamber(std::vector<std::array<char, 7>>& chamber, const Rock&
}

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) {
Expand Down Expand Up @@ -133,42 +126,20 @@ int main(int argc, char * argv[]) {
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';
}
17 changes: 10 additions & 7 deletions 2022/cpp/day_17b.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
// even when adding in jet index and rock type to the == comparator.
// 5 rows of the chamber is arbitrary, might be able to make do with less.

// TODO: use bitset and optimize
// TODO: use bitset instead of array<char>
// though array<bool> might be faster than bitset

struct Point {
Point(const std::size_t row, const int col) : row (row), col(col) {}
Expand Down Expand Up @@ -78,9 +79,9 @@ void add_rock_to_chamber(
const Rock& rock
) {
for (const auto& ele : rock) {
std::array<char, n_cols_in_chamber> temp;
std::fill(std::begin(temp), std::end(temp), '.');
while(chamber.size() <= ele.row) {
std::array<char, n_cols_in_chamber> temp;
std::fill(std::begin(temp), std::end(temp), '.');
chamber.push_back(temp);
}
}
Expand All @@ -90,6 +91,7 @@ void add_rock_to_chamber(
}

void apply_jet(Rock& rock, const std::string& jets, const int jet_index, const std::vector<std::array<char, n_cols_in_chamber>>& chamber) {
// Debug
// for (int i = 0; i < jet_index; i++) {
// std::cout << ' ';
// }
Expand Down Expand Up @@ -125,7 +127,7 @@ void print(const Rock& rock) {
}

void print_chamber(const Chamber& chamber) {
std::cout << chamber.size() << '\n';
std::cout << "Chamber size: " << chamber.size() << '\n';
for (std::size_t i = chamber.size()-1; i > 0; i--) {
std::cout << "|";
for (const auto & ele : chamber[i]) {
Expand All @@ -149,7 +151,7 @@ std::tuple<bool, std::size_t, std::size_t> check_if_seen_else_add(
return {true, it->second, it->first.highest};
}
history[memory] = iteration;
return {false, -1, -1};
return {false, 0, 0};
}

int main(int argc, char * argv[]) {
Expand Down Expand Up @@ -179,14 +181,15 @@ int main(int argc, char * argv[]) {
int rock_count = 0;
int jet_index = 0;
std::vector<std::array<char, n_cols_in_chamber>> chamber;
std::unordered_map<Memory, std::size_t, MemoryHash> history;
std::array<char, n_cols_in_chamber> temp;
std::fill(std::begin(temp), std::end(temp), '#');
chamber.push_back(temp);
Chamber top_n;

Chamber top_n; // Mini chamber, top_n rows of chamber
for (auto& row : top_n) {
std::fill(std::begin(row), std::end(row), 0);
}
std::unordered_map<Memory, std::size_t, MemoryHash> history;

while (rock_count < 1000000000000) {
auto rock = rocks[rock_count % 5];
Expand Down
68 changes: 68 additions & 0 deletions 2022/cpp/day_18a.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <algorithm>
#include <array>
#include <fstream>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>

using Point = std::array<int, 3>;

struct PointHash {
std::size_t operator () (const Point& p) const {
std::size_t ans = 0;
for (const auto ele : p) {
ans = ans * 10 + ele;
}
return ans;
}
};

int main(int argc, char * argv[]) {
std::string input = "../input/day_18_input";
if (argc > 1) {
input = argv[1];
}
std::string line;
std::fstream file(input);

std::unordered_map<Point, int, PointHash> points;

const auto get_neighbours = [] (const Point& p) {
return std::array<Point, 6>{{
Point{{p[0]-1, p[1], p[2]}},
Point{{p[0]+1, p[1], p[2]}},
Point{{p[0], p[1]-1, p[2]}},
Point{{p[0], p[1]+1, p[2]}},
Point{{p[0], p[1], p[2]-1}},
Point{{p[0], p[1], p[2]+1}}
}};
};

while(std::getline(file, line)) {
std::size_t start = 0;
std::size_t end = line.find(',');
Point p;
int index = 0;
while(end != std::string::npos) {
p[index] = std::stoi(line.substr(start, end - start));
start = end + 1;
end = line.find(',', start);
index++;
}
p[index] = std::stoi(line.substr(start, end - start));
points[p] = 0;
for (const auto& neighbour: get_neighbours(p)) {
if (const auto it = points.find(neighbour); it != points.end()) {
points[p]++;
it->second += 1;
}
}
}
int total_sa = points.size() * 6;
for (const auto& [point, n_neighbours] : points) {
total_sa -= n_neighbours;
}
std::cout << total_sa << '\n';
return 0;
}
Loading

0 comments on commit 87f6ecb

Please sign in to comment.