Skip to content

Commit

Permalink
♻️ Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
iglesias committed Jul 13, 2024
1 parent c925558 commit 6616b51
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 43 deletions.
56 changes: 25 additions & 31 deletions adventofcode/2022/day/18/aoc_22_day_18.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
#include <bits/stdc++.h>
#include <algorithm>
#include <array>
#include <cassert>
#include <functional>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

constexpr int N{24};

using world = std::array<std::array<std::array<bool, N>, N>, N>;

// anonymous
namespace
{
struct p
{
struct p {
p(int _x, int _y, int _z) : x(_x), y(_y), z(_z) {}
p() : x(0), y(0), z(0) {}
int x, y, z;
Expand All @@ -25,8 +30,7 @@ int main()
{
std::string line;
::p max(0,0,0), min(N-1,N-1,N-1);
while(std::getline(std::cin, line))
{
while(std::getline(std::cin, line)) {
int x, y, z;
char c1, c2;
std::istringstream ss(line);
Expand Down Expand Up @@ -58,8 +62,7 @@ int main()
std::cout << ">> diff=(" << max.x-min.x << ", " << max.y-min.y << ", " << max.z-min.z << ")\n";

int part_one_ans{0};
for(const auto &a : cubes)
{
for(const auto &a : cubes) {
if(!droplet_world[a.x-1][a.y][a.z]) part_one_ans++;
if(!droplet_world[a.x+1][a.y][a.z]) part_one_ans++;
if(!droplet_world[a.x][a.y-1][a.z]) part_one_ans++;
Expand All @@ -69,20 +72,19 @@ int main()
}

int part_two_ans{part_two()};
//int part_two_ans{};

std::cout << "Part One: " << part_one_ans << std::endl;
std::cout << "Part Two: " << part_two_ans << std::endl;
std::cout << "Part One: " << part_one_ans <<
"\nPart Two: " << part_two_ans << '\n';
}

int part_two()
{
auto within_bounds = [](const ::p &p) -> bool
{
auto within_bounds = [](const ::p &p) -> bool {
return p.x>=0 && p.x<N && p.y>=0 && p.y<N && p.z>=0 && p.z<N;
};

std::function<void(int, int, int)> flood = [&](int x, int y, int z)
{
std::function<void(int, int, int)> flood = [&](int x, int y, int z) {
// std::cout << ">> flood(" << x << ", " << y << ", " << z << ")\n";
assert(within_bounds(::p(x,y,z)));
if(droplet_world[x][y][z]) return;
Expand All @@ -92,38 +94,30 @@ int part_two()

std::vector<::p> deltas = {{+1,0,0}, {-1,0,0},{0,+1,0} ,{0,-1,0},{0,0,+1},{0,0,-1}};

for(const auto &delta : deltas)
{
for(const auto &delta : deltas) {
::p trypos{x+delta.x, y+delta.y, z+delta.z};
if(within_bounds(trypos))
flood(trypos.x, trypos.y, trypos.z);
}
};

for(int x{0}; x<N; x++)
for(int y{0}; y<N; y++)
{
for(int x{0}; x<N; x++) for(int y{0}; y<N; y++) {
flood(x,y,0);
// flood(x,y,N-1); // not necessary? and didn't change, but seems intuitibe
}
// flood(x,y,N-1); // not necessary? and didn't change, but seems intuitive
}

for(int y{0}; y<N; y++)
for(int z{0}; z<N; z++)
{
for(int y{0}; y<N; y++) for(int z{0}; z<N; z++) {
flood(0,y,z);
// flood(N-1,y,z); // not necessary?
}
}

for(int x{0}; x<N; x++)
for(int z{0}; z<N; z++)
{
for(int x{0}; x<N; x++) for(int z{0}; z<N; z++) {
flood(x,0,z);
// flood(x,N-1,z); // not necessary?
}
}

int part_two_ans{0};
for(const auto &a : cubes)
{
for(const auto &a : cubes) {
if(!droplet_world[a.x-1][a.y][a.z] && fluid_reachable[a.x-1][a.y][a.z]) part_two_ans++;
if(!droplet_world[a.x+1][a.y][a.z] && fluid_reachable[a.x+1][a.y][a.z]) part_two_ans++;
if(!droplet_world[a.x][a.y-1][a.z] && fluid_reachable[a.x][a.y-1][a.z]) part_two_ans++;
Expand Down
30 changes: 18 additions & 12 deletions adventofcode/2023/day/01/trebuchet.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// g++ trebuchet.cpp -std=c++23 -fsanitize=address,pointer-overflow,signed-integer-overflow,undefined -Wall -Wextra -pedantic -O3
// g++ trebuchet.cpp -std=c++23 -fsanitize=address,pointer-overflow,signed-integer-overflow,undefined -Wall -Wextra -pedantic -ggdb

#include <algorithm>
#include <array>
Expand Down Expand Up @@ -142,6 +142,18 @@ constexpr std::pair<int, int> find_first_and_last_digits(std::string_view in) {
return std::make_pair(find_first(in), find_first(in, true));
}

std::pair<int, int> find_first_and_last_digits_re(const std::string& line) {
std::smatch matches;
std::cout << ">> " << line << std::endl;
//assert(std::regex_search(line, matches, std::regex("(\\d)+")));
assert(std::regex_search(line, matches, std::regex("(?<!\\d)\\d(?!\\d).*?(?<!\\d)\\d(?!\\d)")));
std::pair<int, int> a_re{0, 0};
std::cout << ">> " << matches.size() << ' ' << matches[0].str() << ' ' << matches[1].str() << ' ' << matches[4].str() << std::endl;
a_re.first = std::stoi(matches[1].str());
a_re.second = std::stoi(matches[2].str());
return a_re;
}

int main() {
std::string line;
int ans_a{0}, ans_b{0}, ans_a_re{0};
Expand All @@ -150,21 +162,15 @@ int main() {
if (line.empty())
break;

auto a{find_first_and_last_digits(line)};
auto b{find_first_and_last_digits(transform(line))};
const auto a{find_first_and_last_digits(line)};
const auto b{find_first_and_last_digits(transform(line))};

ans_a += a.first * 10 + a.second;
ans_b += b.first * 10 + b.second;

std::smatch matches;
std::cout << ">> " << line << std::endl;
//assert(std::regex_search(line, matches, std::regex("(\\d)+")));
assert(std::regex_search(line, matches, std::regex("(?<!\\d)\\d(?!\\d).*?(?<!\\d)\\d(?!\\d)")));
std::pair<int, int> a_re{0, 0};
std::cout << ">> " << matches.size() << ' ' << matches[0].str() << ' ' << matches[1].str() << ' ' << matches[4].str() << std::endl;
a_re.first = std::stoi(matches[1].str());
a_re.second = std::stoi(matches[2].str());
ans_a_re += a_re.first*10 + a_re.second;
//const auto c{find_first_and_last_digits_re(line)};

//ans_a_re += c.first * 10 + c.second;
}

std::cout << "Part one: " << ans_a << ' ' << ans_a_re << '\n';
Expand Down

0 comments on commit 6616b51

Please sign in to comment.