Skip to content

Commit

Permalink
More guards for GCC 11 avoiding 23's std::format and ranges::fold_left.
Browse files Browse the repository at this point in the history
  • Loading branch information
iglesias committed Apr 16, 2024
1 parent eea793c commit d42817d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <utility>
#include <vector>

#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)

int main()
{
auto ans{std::make_pair(0,0)};
Expand Down Expand Up @@ -39,7 +41,9 @@ int main()
for(int j{0}; j < matching_numbers_per_card[i]; j++)
num_cards[i+1+j] += num_cards[i];

#if GCC_VERSION > 120000
ans.second = std::ranges::fold_left(num_cards, 0, std::plus<int>());
#endif

std::cout << "Part one: " << ans.first
<< ".\nPart two: " << ans.second << ".\n";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <bits/stdc++.h>

#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)

using std::string;

void read_input();
Expand Down Expand Up @@ -56,6 +58,7 @@ void read_input()
}
}

// TODO end-to-end part two (cf. d251eef)
template<typename integer> integer solve(bool is_part_two)
{
integer ans{0};
Expand All @@ -71,7 +74,11 @@ template<typename integer> integer solve(bool is_part_two)
else if(instructions[i]=='R')
s = M.at(s).second;
else
#if GCC_VERSION > 120000
throw std::domain_error(std::format("Instruction is neither L nor R: {}.", instructions[i]));
#else
throw std::domain_error("Instruction is neither L nor R: " + std::string(1, instructions[i]) + ".");
#endif

i = (i+1)%static_cast<int>((instructions.length()));
ans++;
Expand Down
15 changes: 11 additions & 4 deletions adventofcode/2023/day/22/sand_slabs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
#include <vector>

#define FOR(i, a, b) for (decltype(b) i = (a) ; i < (b) ; ++i)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, b, a) for (decltype(a) i = (b) ; i > (a) ; --i)
#define RREP(i, n) RFOR(i, n, 0)

struct point { int x, y, z; point(int x, int y, int z) : x(x), y(y), z(z) {} };

Expand All @@ -20,8 +17,14 @@ bool operator==(point const& p, point const& q)
using std::pair;
using std::vector;

#define GCC_VERSION (__GNUC__ * 10000 \
+ __GNUC_MINOR__ * 100 \
+ __GNUC_PATCHLEVEL__)

using std::ranges::count_if;
#if GCC_VERSION > 120000
using std::ranges::fold_left;
#endif
using std::ranges::sort;

using brick = pair<point, point>;
Expand Down Expand Up @@ -107,7 +110,7 @@ bool is_supported(brick b, vector<brick> const& bricks){
}

void gravity(vector<brick>& bricks){
std::ranges::sort(bricks, [](brick const& lhs, brick const& rhs){ return lhs.first.z < rhs.first.z; });
sort(bricks, [](brick const& lhs, brick const& rhs){ return lhs.first.z < rhs.first.z; });
vector<brick> lower_bricks;
for(brick& b : bricks){
while(!is_supported(b, lower_bricks)) b.first.z--, b.second.z--;
Expand Down Expand Up @@ -141,8 +144,12 @@ int chainsize(brick b, vector<brick> const& bricks)
pair<int, int> solve(vector<brick>& bricks)
{
gravity(bricks);
#if GCC_VERSION > 120000
return std::make_pair(count_if(bricks,
[&bricks](brick b){ return disintegrable(b, bricks);}),
fold_left(bricks, 0,
[&bricks](int n, brick b){ return n+chainsize(b, bricks);}));
#else
return std::make_pair(0, 0);
#endif
}

0 comments on commit d42817d

Please sign in to comment.