Skip to content

Instantly share code, notes, and snippets.

View mrvn's full-sized avatar

Goswin von Brederlow mrvn

  • Tübingen, Germany
View GitHub Profile
@mrvn
mrvn / day2.sh
Last active December 3, 2022 16:53
#!/bin/bash
SCORE=0
while read LINE; do
case $LINE in
("A X") SCORE=$(($SCORE+1+3));;
("A Y") SCORE=$(($SCORE+2+6));;
("A Z") SCORE=$(($SCORE+3+6));;
("B X") SCORE=$(($SCORE+1+0));;
@mrvn
mrvn / 5words.py
Created August 7, 2022 00:04
Find 5 words a 5 letters with no letters in common
#!/usr/bin/python3
# wordslist from https://github.com/dwyl/english-words
# extract 5 letter words (no repeats) from list of all words
def read_words():
with open("words_alpha.txt") as fd:
return [word for word in fd.read().split("\n") if len(word) == 5 and len(set(word)) == 5]
# eliminate anagrams of words
@mrvn
mrvn / miller-rabin-prime.cc
Last active July 2, 2022 17:50
Verify Miller-Rabin primality test
#include <iostream>
#include <cstdint>
#include <array>
#include <ranges>
#include <cassert>
#include <bitset>
uint32_t pow_n(uint32_t a, uint32_t d, uint32_t n) {
if (d == 0) return 1;
if (d == 1) return a;
@mrvn
mrvn / bignum.cc
Last active June 12, 2022 22:39
x86_64 adcx/adox
#include <cstdint>
#include <array>
#include <utility>
#include <x86intrin.h>
using Big = std::array<uint64_t, 4>;
/*
unsigned char sum(Big & __restrict__ a, const Big & __restrict__ b) {
unsigned char c = 0;
for (std::size_t i = a.size(); i > 0; --i) {
#include <array>
#include <bitset>
#include <climits>
template <typename T, std::size_t len>
constexpr std::bitset<sizeof(T) * CHAR_BIT * len> from_array(const std::array<T, len> &arr) {
std::bitset<sizeof(T) * CHAR_BIT * len> res;
std::size_t pos = 0;
for (auto x : arr) {
for(std::size_t i = 0; i < sizeof(T) * CHAR_BIT; ++i) {
@mrvn
mrvn / uninitialized_alloc.cpp
Created May 19, 2022 23:51
Allocate uninitialiced memory with new
static_cast<T*>(operator new[](sizeof(T) * rows * cols, static_cast<std::align_val_t>(alignof(T))))
@mrvn
mrvn / scope_exit.h
Last active May 14, 2022 21:02
Declarative Control Flow by Andrei Alexandrescu
// scope.h - Declarative Control Flow
/* Copyright (C) 2020 Goswin von Brederlow <goswin-v-b@web.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
@mrvn
mrvn / elf.ml
Created January 18, 2021 12:24
minmal elf
let magic = "\x7FELF"
let class_32_bit = '\x01'
let class_64_bit = '\x02'
let data_little_endian = '\x01'
let data_big_endian = '\x02'
let ident_version = '\x01'
@mrvn
mrvn / mersenne.cc
Last active December 25, 2020 02:35
// print 2**82589933-1
#include <stdio.h>
#include <math.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
const uint32_t exponent = 82589933;
//const uint32_t exponent = 100;
@mrvn
mrvn / hex2dtb.c
Last active October 12, 2020 19:02
/*
*/
#include <stdio.h>
#include <stdlib.h>
char get() {
int a = getchar();
if ((a >= '0') && (a <= '9')) {
return a - '0';