Sunday, December 6, 2020

Digest for comp.lang.c++@googlegroups.com - 10 updates in 3 topics

"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Dec 06 11:39PM +0100

On 06.12.2020 13:29, Jorgen Grahn wrote:
> Advent of Code
> https://adventofcode.com/2020
 
Minimal support library: <url:
https://github.com/alf-p-steinbach/kickstart/blob/master/source/examples/hello-world.md>
 
------------------------------------------------------------------------
const int data[] =
{
1310,
// ...
};
 
#include <unordered_set>
using std::unordered_set;
 
#include <kickstart/all.hpp>
using namespace kickstart::all; // out, endl, fail, begin, end
 
void cpp_main()
{
const auto values = unordered_set<int>( begin( data ), end( data ) );
for( const int v: values ) {
const int other = 2020 - v;
if( values.count( other ) > 0 ) {
out << v*other << endl;
return;
}
}
fail( "No such value" );
}
 
auto main() -> int { return with_exceptions_displayed( cpp_main ); }
------------------------------------------------------------------------
 
 
- Alf (coding mode)
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Dec 06 11:56PM +0100

On 06.12.2020 13:29, Jorgen Grahn wrote:
> Advent of Code
> https://adventofcode.com/2020
 
 
 
Minimal support library: <url:
https://github.com/alf-p-steinbach/kickstart/blob/master/source/examples/hello-world.md>
 
------------------------------------------------------------------------
const int data[] = ...;
 
#include <unordered_set>
using std::unordered_set;
 
#include <kickstart/all.hpp>
using namespace kickstart::all; // out, endl, fail, begin, end
 
void cpp_main()
{
const auto values = unordered_set<int>( begin( data ), end( data ) );
for( const int v: values ) {
const int remainder = 2020 - v;
for( const int w: values ) {
const int third = remainder - w;
if( values.count( third ) > 0 ) {
out << int64_t(1)*v*w*third << endl;
return;
}
}
}
fail( "No such value" );
}
 
auto main() -> int { return with_exceptions_displayed( cpp_main ); }
------------------------------------------------------------------------
 
 
- Alf (coding mode)
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Dec 07 12:14AM +0100

On 06.12.2020 13:29, Jorgen Grahn wrote:
> Advent of Code
> https://adventofcode.com/2020
 
 
Minimal support library: <url:
https://github.com/alf-p-steinbach/kickstart/blob/master/source/examples/hello-world.md>
 
------------------------------------------------------------------------
#include <fstream>
#include <sstream>
using std::ifstream,
std::istringstream;
 
#include <kickstart/all.hpp>
using namespace kickstart::all;
 
 
void cpp_main()
{
const auto& filename = "pw-data.txt";
ifstream f( filename );
hopefully( not f.fail() )
or fail( ""s << "Failed to open '" << filename << "'." );
string line;
int n_valid = 0;
while( getline( f, line ) ) {
auto line_stream = istringstream( line );
int min;
int max;
char ch;
char dummy;
string password;
 
line_stream >> min >> dummy >> max >> ch >> dummy >> password
or fail( ""s << "Parsing '" << line << "' failed." );
 
int ch_count = 0;
for( const char pw_char: password ) {
ch_count += (pw_char == ch);
}
n_valid += (min <= ch_count and ch_count <= max);
}
out << n_valid << endl;
}
 
auto main() -> int { return with_exceptions_displayed( cpp_main ); }
------------------------------------------------------------------------
 
- Alf (coding mode)
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Dec 07 12:20AM +0100

On 06.12.2020 13:29, Jorgen Grahn wrote:
> Advent of Code
> https://adventofcode.com/2020
 
Minimal support library: <url:
https://github.com/alf-p-steinbach/kickstart/blob/master/source/examples/hello-world.md>
 
------------------------------------------------------------------------
#include <algorithm>
#include <fstream>
#include <sstream>
using std::max,
std::ifstream,
std::istringstream;
 
#include <kickstart/all.hpp>
using namespace kickstart::all;
 
 
void cpp_main()
{
const auto& filename = "pw-data.txt";
ifstream f( filename );
hopefully( not f.fail() )
or fail( ""s << "Failed to open '" << filename << "'." );
string line;
int n_valid = 0;
while( getline( f, line ) ) {
auto line_stream = istringstream( line );
int i1;
int i2;
char ch;
char dummy;
string password;
 
line_stream >> i1 >> dummy >> i2 >> ch >> dummy >> password
or fail( ""s << "Parsing '" << line << "' failed." );
 
n_valid +=
(ssize(password) >= max( i1, i2 ) )
and (password.at( i1 - 1 ) == ch) != (password.at( i2 - 1 )
== ch);
}
out << n_valid << endl;
}
 
auto main() -> int { return with_exceptions_displayed( cpp_main ); }
------------------------------------------------------------------------
 
 
- Alf (coding mode)
"Öö Tiib" <ootiib@hot.ee>: Dec 06 11:48AM -0800

On Sunday, 6 December 2020 at 20:41:02 UTC+2, Bonita Montero wrote:
 
> > May be. Why you have those shared_ptrs instead of having
> > deque<void_task> directly.
> I don't want to move the task-parameters.
 
What it means? These are not moved but pointers to those
are. So without explaining it feels like
std::deque<std::shared_ptr<std::string>> that is added
inefficiency without benfits on general case.
Bonita Montero <Bonita.Montero@gmail.com>: Dec 06 09:23PM +0100

> are. So without explaining it feels like
> std::deque<std::shared_ptr<std::string>> that is added
> inefficiency without benfits on general case.
 
The parameters encapsulated in a packaged task would be moved if i
move the packaged task. Depending on the length of the parameter-set
that might have a higher overhead than moving just a pointer.
Bonita Montero <Bonita.Montero@gmail.com>: Dec 06 09:27PM +0100

So here's my little thread-pool engine:
First a little debug-header:
 
// debug_exceptions.h
#pragma once
#if !defined try_debug
#if !defined(NDEBUG)
#define try_debug try
#else
#define try_debug

No comments: