Skip to content

Commit 87f6ecb

Browse files
committed
Add 2022 day 18 cpp
1 parent 12714f4 commit 87f6ecb

7 files changed

Lines changed: 292 additions & 38 deletions

File tree

2022/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ This folder contains solutions to each of the problems in Advent of Code 2022 in
2525
| <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> |
2626
| <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> |
2727
| <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> |
28+
| <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> |

2022/cpp/day_17a.cpp

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#include <array>
2-
#include <unordered_set>
32
#include <fstream>
43
#include <iostream>
54
#include <string>
5+
#include <unordered_set>
66
#include <vector>
77

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

5252
void apply_jet(Rock& rock, const std::string& jets, const int jet_index, const std::vector<std::array<char, 7>>& chamber) {
53-
// std::cout << jets << '\n';
54-
// for (int i = 0; i < jet_index; i++) {
55-
// std::cout << ' ';
56-
// }
57-
// std::cout << '^';
58-
// std::cout << '\n';
59-
// std::cout << j << '\n';
6053
const auto prev = rock;
6154
if (const char j = jets[jet_index]; j == '>') {
6255
for (auto& ele : rock) {
@@ -133,42 +126,20 @@ int main(int argc, char * argv[]) {
133126
std::array<char, 7> temp;
134127
std::fill(std::begin(temp), std::end(temp), '#');
135128
chamber.push_back(temp);
136-
// print_chamber(chamber);
137-
// exit(0);
138-
// std::cout << __LINE__ <<'\n';
139129
while (rock_count < 2022) {
140-
// std::cout << "---------------- Begin -------------------" << '\n';
141130
auto rock = rocks[rock_count % 5];
142131
rock_count++;
143-
// std::cout << __LINE__ <<'\n';
144132
move_to_starting_height(rock, highest);
145-
// std::cout << __LINE__ <<'\n';
146133
auto prev = rock;
147-
// move(rock);
148-
// print(rock);
149-
150134
while (!intersection(chamber, rock)) {
151-
// std::cout << __LINE__ <<'\n';
152135
apply_jet(rock, jets, jet_index, chamber);
153-
// std::cout << __LINE__ <<'\n';
154-
// print(rock);
155-
// std::cout << __LINE__ <<'\n';
156136
jet_index++;
157137
if (jet_index == jets.size()) jet_index = 0;
158138
prev = rock;
159139
move(rock);
160-
// std::cout << __LINE__ <<'\n';
161-
// print(rock);
162140
}
163-
// print(prev);
164-
// std::cout << __LINE__ <<'\n';
165141
add_rock_to_chamber(chamber, prev);
166-
// std::cout << __LINE__ <<'\n';
167-
// print(chamber);
168142
highest = chamber.size();
169-
// std::cout << "---------------- End -------------------" << '\n';
170-
// print_chamber(chamber);
171-
// if (rock_count == 10) exit(0);
172143
}
173144
std::cout << highest - 1 << '\n';
174145
}

2022/cpp/day_17b.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
// even when adding in jet index and rock type to the == comparator.
1111
// 5 rows of the chamber is arbitrary, might be able to make do with less.
1212

13-
// TODO: use bitset and optimize
13+
// TODO: use bitset instead of array<char>
14+
// though array<bool> might be faster than bitset
1415

1516
struct Point {
1617
Point(const std::size_t row, const int col) : row (row), col(col) {}
@@ -78,9 +79,9 @@ void add_rock_to_chamber(
7879
const Rock& rock
7980
) {
8081
for (const auto& ele : rock) {
82+
std::array<char, n_cols_in_chamber> temp;
83+
std::fill(std::begin(temp), std::end(temp), '.');
8184
while(chamber.size() <= ele.row) {
82-
std::array<char, n_cols_in_chamber> temp;
83-
std::fill(std::begin(temp), std::end(temp), '.');
8485
chamber.push_back(temp);
8586
}
8687
}
@@ -90,6 +91,7 @@ void add_rock_to_chamber(
9091
}
9192

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

127129
void print_chamber(const Chamber& chamber) {
128-
std::cout << chamber.size() << '\n';
130+
std::cout << "Chamber size: " << chamber.size() << '\n';
129131
for (std::size_t i = chamber.size()-1; i > 0; i--) {
130132
std::cout << "|";
131133
for (const auto & ele : chamber[i]) {
@@ -149,7 +151,7 @@ std::tuple<bool, std::size_t, std::size_t> check_if_seen_else_add(
149151
return {true, it->second, it->first.highest};
150152
}
151153
history[memory] = iteration;
152-
return {false, -1, -1};
154+
return {false, 0, 0};
153155
}
154156

155157
int main(int argc, char * argv[]) {
@@ -179,14 +181,15 @@ int main(int argc, char * argv[]) {
179181
int rock_count = 0;
180182
int jet_index = 0;
181183
std::vector<std::array<char, n_cols_in_chamber>> chamber;
184+
std::unordered_map<Memory, std::size_t, MemoryHash> history;
182185
std::array<char, n_cols_in_chamber> temp;
183186
std::fill(std::begin(temp), std::end(temp), '#');
184187
chamber.push_back(temp);
185-
Chamber top_n;
188+
189+
Chamber top_n; // Mini chamber, top_n rows of chamber
186190
for (auto& row : top_n) {
187191
std::fill(std::begin(row), std::end(row), 0);
188192
}
189-
std::unordered_map<Memory, std::size_t, MemoryHash> history;
190193

191194
while (rock_count < 1000000000000) {
192195
auto rock = rocks[rock_count % 5];

2022/cpp/day_18a.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include <algorithm>
2+
#include <array>
3+
#include <fstream>
4+
#include <iostream>
5+
#include <string>
6+
#include <unordered_map>
7+
#include <vector>
8+
9+
using Point = std::array<int, 3>;
10+
11+
struct PointHash {
12+
std::size_t operator () (const Point& p) const {
13+
std::size_t ans = 0;
14+
for (const auto ele : p) {
15+
ans = ans * 10 + ele;
16+
}
17+
return ans;
18+
}
19+
};
20+
21+
int main(int argc, char * argv[]) {
22+
std::string input = "../input/day_18_input";
23+
if (argc > 1) {
24+
input = argv[1];
25+
}
26+
std::string line;
27+
std::fstream file(input);
28+
29+
std::unordered_map<Point, int, PointHash> points;
30+
31+
const auto get_neighbours = [] (const Point& p) {
32+
return std::array<Point, 6>{{
33+
Point{{p[0]-1, p[1], p[2]}},
34+
Point{{p[0]+1, p[1], p[2]}},
35+
Point{{p[0], p[1]-1, p[2]}},
36+
Point{{p[0], p[1]+1, p[2]}},
37+
Point{{p[0], p[1], p[2]-1}},
38+
Point{{p[0], p[1], p[2]+1}}
39+
}};
40+
};
41+
42+
while(std::getline(file, line)) {
43+
std::size_t start = 0;
44+
std::size_t end = line.find(',');
45+
Point p;
46+
int index = 0;
47+
while(end != std::string::npos) {
48+
p[index] = std::stoi(line.substr(start, end - start));
49+
start = end + 1;
50+
end = line.find(',', start);
51+
index++;
52+
}
53+
p[index] = std::stoi(line.substr(start, end - start));
54+
points[p] = 0;
55+
for (const auto& neighbour: get_neighbours(p)) {
56+
if (const auto it = points.find(neighbour); it != points.end()) {
57+
points[p]++;
58+
it->second += 1;
59+
}
60+
}
61+
}
62+
int total_sa = points.size() * 6;
63+
for (const auto& [point, n_neighbours] : points) {
64+
total_sa -= n_neighbours;
65+
}
66+
std::cout << total_sa << '\n';
67+
return 0;
68+
}

0 commit comments

Comments
 (0)