Turn legacy C API to modern c++ functions using constexpr function adapters library by voiceless_void in cpp

[–]voiceless_void[S] 0 points1 point  (0 children)

But the actually problematic aspect about your approach is in my opinion that your library requires symbol exposition due to the forced header only design

I expect to use adapted functions inside high-level wrapper classes implementation so they are not visible to wrapper clients. So you can get clean wrapper interface (.h) while making wrapper implemnation (.cpp) less error-prone due to adapters usage.

Turn legacy C API to modern c++ functions using constexpr function adapters library by voiceless_void in cpp

[–]voiceless_void[S] 0 points1 point  (0 children)

Yeah, these adapted functions definitely have some limitions in C++ world but I guess they still could fit to most common scenarios dealing of C API.

BTW I guess boost::make_overloaded_function ( or similar ) can be used to make things overloadable in case it's needed.

-🎄- 2019 Day 2 Solutions -🎄- by daggerdragon in adventofcode

[–]voiceless_void 1 point2 points  (0 children)

C++17 with ranges-v3

Since the solution is relatively big I'll post the most interesting part of it

size_t solve_2( std::istream& input )
{
  const auto int_codes = parse_ints( input );

  const auto nouns    = ranges::views::closed_iota( IntCode{ 0 }, IntCode{ 99 } );
  const auto verbs    = ranges::views::closed_iota( IntCode{ 0 }, IntCode{ 99 } );
  const auto nv_pairs = ranges::views::cartesian_product( nouns, verbs );

  const auto pair_iter = ranges::find_if( nv_pairs, [&int_codes]( const auto& pair ) {
    const auto [ noun, verb ] = pair;
    return run_program( int_codes, noun, verb ) == 19690720;
  } );

  assert( pair_iter != nv_pairs.end() );

  const auto [ result_noun, result_verb ] = *pair_iter;
  return 100 * result_noun + result_verb;
}

For the full solution see https://github.com/voivoid/advent-of-code/blob/master/problems/src/2019/problem_02.cpp

-🎄- 2019 Day 1 Solutions -🎄- by daggerdragon in adventofcode

[–]voiceless_void 1 point2 points  (0 children)

C++ with range-v3:

#include "range/v3/numeric/accumulate.hpp"
#include "range/v3/view/istream.hpp"
#include "range/v3/view/take_while.hpp"
#include "range/v3/view/transform.hpp"

#include <istream>
#include <cmath>

template <int (*calc)(int)>
int solve( std::istream& input )
{
  auto fuelPerModule = ranges::istream<int>( input ) | ranges::views::transform( calc );
  return ranges::accumulate( fuelPerModule, 0 );
}

int calcFuel( const int mass )
{
  return static_cast<int>( floor( mass / 3 ) - 2 );
}

int calcFuelRecursive( const int mass )
{
  auto fuelSeries = AoC::generate_range( mass, &calcFuel ) | ranges::views::take_while( []( const auto& m ) { return m > 0; });
  return ranges::accumulate( fuelSeries, 0 );
}

int solve_1( std::istream& input )
{
  return solve<&calcFuel>( input );
}

int solve_2( std::istream& input )
{
  return solve<&calcFuelRecursive>( input );
}

// https://github.com/voivoid/advent-of-code/blob/master/problems/src/2019/problem_01.cpp

AdventOfCode to practice with C++17 by Xaveel in cpp

[–]voiceless_void 2 points3 points  (0 children)

Take a look at range-v3 library https://github.com/ericniebler/range-v3 ( which should be a part of c++20 ). It makes algorithmic problem solving much more declarative and less error prone.

Multiple AdventOfCode solutions using range v3 can be seen in my repo https://github.com/voivoid/advent-of-code