Skip to content

Instantly share code, notes, and snippets.

@Quackward
Created April 8, 2022 21:36
Show Gist options
  • Save Quackward/f1c1d262fe07dbf6d55156a03e664a01 to your computer and use it in GitHub Desktop.
Save Quackward/f1c1d262fe07dbf6d55156a03e664a01 to your computer and use it in GitHub Desktop.
//, ________
//, + //////// \
//, /) /) H | + .
//, [ ^ ^] . + ___H__ |__
//, // ' v')__ __ ||___|___|_| +
//, ~/ \_6/_6 + |_|_____|_||
//, _____ _ / _) _) . _____Wv v _| v W____,
// `````````` lm`lm ``````` Clairvoire@gmail.com ```
// ` ` ` ` ` ` ```````````````````` `
// all code is written by me yesterday, including the testing nonsense
// feel free to use any of it, if any of it is even scrutible to you lol
// --- Settings ---
//#undef TESTING // undef only when wanting to experience our output as the host will (without debug data)
// --- Boiling Place (skip to near the end to get to the guts of the algorithm) ---
#define _CRT_SECURE_NO_WARNINGS
#include <cstdint>
#include <cstdarg>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <inttypes.h>
#include <vector>
#include <list>
#include <string>
#include <map>
#include <algorithm>
typedef int64_t int64; typedef uint32_t uint32; // i32 & u32 -> upto 10^9
typedef int32_t int32; typedef uint64_t uint64;
typedef int16_t int16; typedef uint16_t uint16;
typedef int8_t int8; typedef uint8_t uint8; // yeah we are formatting code for vertical length
template <class T> struct vec2_t{T x{0}, y{0}; static uint32 size() {return 2;} T & operator[](uint32 i){return (T*)this + i;} };
template <class T> struct vec3_t{T x{0}, y{0}, z{0}; static uint32 size() {return 3;} T & operator[](uint32 i){return (T*)this + i;} };
template <class T> struct vec4_t{T x{0}, y{0}, z{0}, w{0}; static uint32 size() {return 4;} T & operator[](uint32 i){return (T*)this + i;} };
typedef vec2_t<uint32> u32vec2; typedef vec2_t<uint64> u64vec2; typedef vec2_t< int32> i32vec2; typedef vec2_t< int64> i64vec2; typedef vec2_t< float> vec2;
typedef vec3_t<uint32> u32vec3; typedef vec3_t<uint64> u64vec3; typedef vec3_t< int32> i32vec3; typedef vec3_t< int64> i64vec3; typedef vec3_t< float> vec3;
typedef vec4_t<uint32> u32vec4; typedef vec4_t<uint64> u64vec4; typedef vec4_t< int32> i32vec4; typedef vec4_t< int64> i64vec4; typedef vec4_t< float> vec4;
#ifdef TESTING
#pragma warning(disable : 6031)
namespace Dbg {
extern const char * testIn;
extern const char * testTruth;
const char * testInAcc = NULL;
int testInCount = 0;
bool hasBegun = false;
bool needsComma = false;
bool needsLine = false;
std::string testOut;
}
void dbgPrintName(const char* x){ if(!Dbg::hasBegun) printf(" __________________\n | "); printf(Dbg::needsComma?", (%s)=":"(%s)=" , x); Dbg::needsComma = false; Dbg::needsLine = true; Dbg::hasBegun = true;}
uint32 dbgPrint(uint32 x) { if(!Dbg::hasBegun) printf(" __________________\n | "); printf(Dbg::needsComma?", %u" :"%u" , x); Dbg::needsComma = true; Dbg::needsLine = true; Dbg::hasBegun = true; return x;}
uint64 dbgPrint(uint64 x) { if(!Dbg::hasBegun) printf(" __________________\n | "); printf(Dbg::needsComma?", %llu" :"%llu" , x); Dbg::needsComma = true; Dbg::needsLine = true; Dbg::hasBegun = true; return x;}
int32 dbgPrint( int32 x) { if(!Dbg::hasBegun) printf(" __________________\n | "); printf(Dbg::needsComma?", %i" :"%i" , x); Dbg::needsComma = true; Dbg::needsLine = true; Dbg::hasBegun = true; return x;}
int64 dbgPrint( int64 x) { if(!Dbg::hasBegun) printf(" __________________\n | "); printf(Dbg::needsComma?", %lli" :"%lli" , x); Dbg::needsComma = true; Dbg::needsLine = true; Dbg::hasBegun = true; return x;}
float dbgPrint( float x) { if(!Dbg::hasBegun) printf(" __________________\n | "); printf(Dbg::needsComma?", %f" :"%f" , x); Dbg::needsComma = true; Dbg::needsLine = true; Dbg::hasBegun = true; return x;}
const char * dbgPrint(const char * x){ if(!Dbg::hasBegun) printf(" __________________\n | "); printf(Dbg::needsComma?", \"%s\"":"\"%s\"", x); Dbg::needsComma = true; Dbg::needsLine = true; Dbg::hasBegun = true; return x;}
template<class T> void dbgPrint(T * x, uint32 cnt, bool lenPrt) {
lenPrt?printf(Dbg::needsComma?", cnt=%u[ ":"cnt=%u[ ",cnt):printf(Dbg::needsComma?", [":"["); Dbg::needsComma=false; for(uint64 i=0; i<cnt; ++i) dbgPrint(x[i], i!=cnt); printf("]"); Dbg::needsLine=true;
}
template<class T> vec2_t<T> dbgPrint(vec2_t<T> x){ dbgPrint((T*)&x, 2, false); return x;}
template<class T> vec3_t<T> dbgPrint(vec3_t<T> x){ dbgPrint((T*)&x, 3, false); return x;}
template<class T> vec4_t<T> dbgPrint(vec4_t<T> x){ dbgPrint((T*)&x, 4, false); return x;}
template<class T> std::vector<T> & dbgPrint(std::vector<T> & x){ dbgPrint(x.data(), x.size(), true); return x;}
void dbgPrintf(const char * fmt, ...) {
va_list params; va_list paramsBackup;
va_start(params, fmt); va_copy(paramsBackup, params);
int prevSize = std::max(size_t(1), Dbg::testOut.size())-1;
int newSize = vsnprintf(NULL, 0, fmt, params) + prevSize+1;
Dbg::testOut.resize(newSize);
newSize = vsnprintf(&Dbg::testOut[0] + prevSize, newSize, fmt, paramsBackup);
va_end(paramsBackup); va_end(params);
printf(Dbg::needsLine?"\n%s":"%s", Dbg::testOut.c_str() + prevSize);
Dbg::needsLine = false; Dbg::needsComma = false; Dbg::hasBegun = false;
}
void dbgCheckAnswer() {
const char * a = Dbg::testTruth;
const char * b = Dbg::testOut.c_str();
const char * lastLineA = a;
const char * lastLineB = b;
while (a && b && *a && *b) {
while(isspace(*a)) { if(*a=='\n'){ lastLineA = a+1; } ++a; }
while(isspace(*b)) { if(*b=='\n'){ lastLineB = b+1; } ++b; }
if (*a != *b) break;
++a; ++b;
}
if(a && b && *a == *b)
printf("\n ~ Passed! ~\n");
else {
printf("\n !! Failed !! \n\n truth: \"");
while(lastLineA && *lastLineA && *lastLineA != '\n') putchar(*lastLineA++);
printf("\" \n output: \"");
while(lastLineB && *lastLineB && *lastLineB != '\n') putchar(*lastLineB++);
printf("\"\n");
}
}
#define VAR(x) (dbgPrintName(#x), dbgPrint(x))
#define PK(x) (dbgPrint(x))
#define NEWL() printf("\n | ")
#define PRINT(fmt,...) dbgPrintf(fmt, __VA_ARGS__)
#define SCAN(fmt,...) sscanf(Dbg::testInAcc=(Dbg::testInAcc?(Dbg::testInAcc+Dbg::testInCount):(Dbg::testIn?Dbg::testIn:" ")), fmt"%n", __VA_ARGS__, &Dbg::testInCount)
#define CHECK() dbgCheckAnswer()
#else
#define VAR(x) (x)
#define PK(x) (x)
#define NEWL() ((void)0)
#define PRINT(fmt,...) printf(fmt, __VA_ARGS__)
#define SCAN(fmt,...) scanf(fmt, __VA_ARGS__)
#define CHECK() ((void)0)
#endif
#define FOR (indexName,start,end) for(uint64 indexName = (start); indexName < (end); ++indexName)
#define FORr (indexName,start,end) for(uint64 indexName = (end) - 1u; indexName > (start) && indexName < (end); --indexName)
#define FORit (iterName,container) for(auto iterName = (container).begin(); iterName != (container).end(); ++iterName)
#define FORrit(iterName,container) for(auto iterName = (container).rbegin(); iterName != (container).rend(); ++iterName)
#define SUM (acc,arr,start,end) FOR(_index, start, end) acc += arr[_index]
#define SUMit(acc,container) FORit(_iter, container) acc += _iter
//,,, ^^^ end of boiler plate code ^^^ ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
//``` vvv beginning of fun stuff!! vvv ``````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````
#ifdef TESTING // >>> Input <<<
const char * Dbg::testIn=R"QwQ(
8
1 2 3 5
5 6 7 8
)QwQ"; //````````````!!! Truth !!!
const char * Dbg::testTruth=R"QwQ(
Case #1: 2
Case #2: 4
Case # 3 : 6
Case #4: 8
Case #5: 10
Case #6: 12
Case #7: 14
Case #8: 16
)QwQ"; // ````````````````````````
#endif
// reminders
// - the answer is prolly "elegant" and without tuning (remember the cave?? sick)
// - invoke the strength of the insane
// - our crimes will go unpunished
// notes
// -
//
//
//
//
//
//
int main(int argc, char * argv[]) {
uint64 testCount = 0;
SCAN("%llu", &testCount);
for (uint64 test = 1; test <= testCount; ++test) {
uint64 a = 0;
uint64 out = 0; // final result
SCAN("%llu", &a);
VAR(a);
VAR(out=a*2);
PRINT("Case #%llu: %llu\n", test, out); //fflush(stdout);
}
CHECK();
return 0;
}
#if 0 // extra clean main() to copy+paste
int main(int argc, char * argv[]) {
uint64 testCount = 0;
SCAN("%llu", &testCount);
struct Note {
uint64 a {0};
};
std::vector<Note> notes;
for (uint64 test = 1; test <= testCount; ++test) {
notes.clear();
uint64 a = 0;
uint64 b = 0;
uint64 c = 0;
uint64 out = 0; // final result
SCAN("%llu%llu%llu", &a, &b, &c);
VAR(a); VAR(b); VAR(c);
PRINT("Case #%llu: %llu\n", test, out); //fflush(stdout);
}
CHECK(); // compare our input against some example truths we prepared
return 0;
}
#endif
@Quackward
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment