Skip to content

Commit

Permalink
Add part one of the polymerization one and the next one
Browse files Browse the repository at this point in the history
  • Loading branch information
iglesias committed Jul 25, 2024
1 parent 474ec4c commit a3334eb
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 0 deletions.
56 changes: 56 additions & 0 deletions adventofcode/2021/day/14/extended_polymerization.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <climits>
#include <map>
#include <iostream>
#include <string>
#include <vector>

using std::string;
using std::vector;

void step(string& compound);

char rules['Z' - 'A' + 1]['Z' - 'A' + 1];

int main() {
string Template;
std::cin >> Template;
std::string line;
while (std::getline(std::cin, line)) {
if (line.empty()) continue;
const auto pos = line.find("->");
rules[line.at(pos - 3) - 'A'][line.at(pos - 2) - 'A'] = line.at(pos + 3);
}
std::cout << "Template: " << Template << '\n';
//TODO simulation too slow for part two (40 steps) even for sample input,
//it runs until 30 for sample input, the counter may need 64-bit int
const int num_steps = 40;
string compound = Template;
for (int s = 0; s < num_steps; s++) {
step(compound);
//std::cout << "After step " << s + 1 << ": " << compound << '\n';
}
std::map<char, int> counter;
int max = 0, min = INT_MAX;
for (const auto& c : compound) {
counter[c]++;
}
for (const auto& [_, count] : counter) {
max = std::max(max, count);
min = std::min(min, count);
}
std::cout << max - min << '\n';

}

void step(string& compound) {
string new_characters;
for (int i = 0; i < compound.size() - 1; i++)
new_characters.push_back(rules[compound.at(i) - 'A'][compound.at(i + 1) - 'A']);
string new_compound;
for (int i = 0; i < compound.size(); i++) {
new_compound.push_back(compound.at(i));
if (i == compound.size() - 1) break;
new_compound.push_back(new_characters.at(i));
}
compound = new_compound;
}
84 changes: 84 additions & 0 deletions adventofcode/2021/day/15/chiton.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include <array>
#include <climits>
#include <cstddef>
#include <iostream>
#include <queue>
#include <string>
#include <tuple>
#include <vector>

using std::string;
using std::vector;

int shortest_path(const vector<string>&);
void enlarge(vector<string>&);

int main() {
vector<string> G;
{ string line;
while (std::getline(std::cin, line)) G.push_back(std::move(line)); }
std::cout << "Part one: " << shortest_path(G) << "\n";
enlarge(G);
std::cout << "Part two: " << shortest_path(G) << "\n";
}

int shortest_path(const vector<string>& G) {
using state = std::tuple<int, int, int>;
std::priority_queue<state, vector<state>, std::greater<state>> pq;

const int R = static_cast<int>(G.size());
const int C = static_cast<int>(G.at(0).size());

auto dist = vector(R, vector(C, INT_MAX/2));
dist[1][0] = G[1][0] - '0';
dist[0][1] = G[0][1] - '0';

pq.emplace(dist[1][0], 1, 0);
pq.emplace(dist[0][1], 0, 1);

while(!pq.empty()){
const auto [cost, row, col] = pq.top();
pq.pop();

if (row == R - 1 and col == C - 1) return cost;

constexpr std::array<std::pair<int, int>, 4> deltas = {std::array{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
for (const auto& [dr, dc] : deltas) {
const int r = row + dr;
const int c = col + dc;
if (not(0 <= r and r < R and 0 <= c and c < C)) continue;
const int try_cost = cost + G[r][c] - '0';
if (try_cost >= dist[r][c]) continue;
dist[r][c] = try_cost;
pq.emplace(try_cost, r, c);
}
}

return -1;
}

void enlarge(vector<string>& G) {
// initial dimensions
size_t R0 = G.size();
size_t C0 = G.at(0).length();
// allocate extending current data
constexpr int scaling{5};
G.resize(G.size() * scaling);
for (string& row : G)
if (row.length()) row.resize(row.length() * scaling);
else row.append(scaling * C0, ' ');
// extend columns in the first row starting from the second column
for (size_t j = 1; j < scaling; j++)
for (size_t row = 0; row < R0; row++) for (size_t col = 0; col < C0; col++)
if (G[row][(j - 1) * C0 + col] == '9')
G[row][j * C0 + col] = '1';
else
G[row][j * C0 + col] = char(G[row][(j - 1) * C0 + col] + 1);
// extend all other cells start from the second row
for (size_t i = 1; i < scaling; i++) for (size_t j = 0; j < scaling; j++)
for (size_t row = 0; row < R0; row++) for (size_t col = 0; col < C0; col++)
if (G[(i - 1) * R0 + row][j * C0 + col] == '9')
G[i * R0 + row][j * C0 + col] = '1';
else
G[i * R0 + row][j * C0 + col] = char(G[(i - 1) * R0 + row][j * C0 + col] + 1);
}

0 comments on commit a3334eb

Please sign in to comment.