Skip to content

Instantly share code, notes, and snippets.

View saxbophone's full-sized avatar
🏳️‍⚧️

saxbophone

🏳️‍⚧️
View GitHub Profile
@saxbophone
saxbophone / constexpr_vector.cpp
Last active April 7, 2022 18:42
Demo of constexpr vector usage and erroneous usage in C++20
// https://godbolt.org/z/vsacWP36z
// Note: Only works with x86_64 GCC (trunk) on Godbolt.org --must have a C++ stdlib that supports constexpr vector
#include <cstddef>
#include <vector>
class Storage {
private:
std::vector<int> foo;
public:
@saxbophone
saxbophone / twelve_days.cpp
Created December 16, 2021 12:52
On the twelfth day of Christmas, my true love sent to me: A program written in CPP!
#include <array>
#include <iostream>
#include <string>
#include <cstddef>
std::array<std::string, 12> ORDINAL_DAYS = {
"first",
"second",
"third",
@saxbophone
saxbophone / factorio_crafting_tree.lua
Last active December 8, 2021 22:43
Produce a Graphviz DOT graph for the entire Factorio crafting tree
-- Produce a Graphviz DOT graph for the entire Factorio crafting tree
-- Written by Joshua Saxby (saxbophone) 2021
-- This work is released into the public domain.
-- set recipe_data to a table using the official Wube Factorio recipe data Lua
-- file, available at:
-- https://github.com/wube/factorio-data/blob/7cab1a0d2d0df5029d3208a56636abf5548e955d/base/prototypes/recipe.lua
print("digraph {")
@saxbophone
saxbophone / dot.py
Last active October 14, 2021 05:11
Comparison of the algebraic and geometric methods for calculating the dot product
from math import acos, cos, sqrt
def dot(x: tuple, y: tuple):
return (x[0] * y[0]) + (x[1] * y[1])
def vector_length(v: tuple):
return sqrt(v[0] ** 2 + v[1] ** 2)
def angle_between(x: tuple, y: tuple):
x_len = vector_length(x)
@saxbophone
saxbophone / LinkedList.hpp
Last active September 25, 2021 12:53
Constant-expression linked list
#include <cstdio>
struct LinkedList {
constexpr LinkedList(const char* name) : next(nullptr), name(name) {}
constexpr LinkedList(const char* name, LinkedList* previous) : LinkedList(name) {
previous->assign(this);
}
constexpr void assign(LinkedList* next) {
@saxbophone
saxbophone / quine.py
Created September 23, 2021 21:50
An original Python quine
source='source=%s;print(source %% repr(source))';print(source % repr(source))
#include <functional>
// R - return type, Args - argument types, Func - Function taking Args returning R, to call
template <typename R, typename ...Args, R Func(Args... args)>
R do_something(Args... args) {
return Func(args...);
}
int twice(double v) {
return (int)v * 2;
@saxbophone
saxbophone / string_tparam.cpp
Created August 27, 2021 04:35
Passing string literals as template non-type params
#include <iostream>
#include <cstddef>
char f[] = "caba";
template <std::size_t L, char name[L]>
void something() {
std::cout << name << std::endl;
@saxbophone
saxbophone / bitset_subset.cpp
Created August 11, 2021 12:39
Subset of bitset
#include <algorithm>
#include <bitset>
#include <iostream>
#include <string>
#include <cstddef>
template<std::size_t INPUT, std::size_t OUTPUT> requires (INPUT >= OUTPUT)
std::bitset<OUTPUT> subset(std::bitset<INPUT> input, std::size_t start) {
@saxbophone
saxbophone / bundle.sh
Created August 4, 2021 04:53
Convenient Linux shell script for packaging Factorio mods to versioned ZIP file
#!/usr/bin/env bash
# NOTE: Set your mod's version in your info.json file to "___MOD_VERSION___" to
# allow this script to substitute the most recent Git tag in its place at
# bundle time. Tags are expected to be in the vX.X.X format.
# get current revision from git tag
GIT_DESCRIBE=$(git describe) &&
# omit first character of git-describe output to remove the leading "v"
CURRENT_REVISION=$(cut -d '-' -f 1 <<< ${GIT_DESCRIBE:1}) &&