Skip to content

Instantly share code, notes, and snippets.

View orlp's full-sized avatar
⚠️
This user's social credit score is unknown.

Orson Peters orlp

⚠️
This user's social credit score is unknown.
  • Leiden, Netherlands
View GitHub Profile
@orlp
orlp / chacha.h
Last active April 25, 2024 12:36
C++11 ChaCha implementation.
/*
Copyright (c) 2024 Orson Peters <orsonpeters@gmail.com>
This software is provided 'as-is', without any express or implied warranty. In no event will the
authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose, including commercial
applications, and to alter it and redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you wrote the
@orlp
orlp / ipow.c
Last active April 21, 2024 15:11
int64_t ipow(int64_t base, uint8_t exp) {
static const uint8_t highest_bit_set[] = {
0, 1, 2, 2, 3, 3, 3, 3,
4, 4, 4, 4, 4, 4, 4, 4,
5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5,
6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 255, // anything past 63 is a guaranteed overflow with base > 1

Villager mechanics in 1.14. All this data has been deduced by using the following commands:

/data get entity @e[type=minecraft:villager,limit=1,sort=nearest] Brain.memories
/execute as orlp at orlp run tellraw @s {"nbt":"Brain.memories","entity":"@e[type=villager,sort=nearest,limit=1]"}

A villager is awake from 0-11999 and tries to sleep between 12000 and 23999.

A villager will claim a bed if:

@orlp
orlp / gist:4666248
Last active January 11, 2024 00:17

A short proof why this works is this:

I assume the original version works (x & (x - 1)) == 0, barring zero. The change made is that we set the highest bit on the left hand side of the AND operator (by OR'ing with 0x8000000000000000).

We do not incorrectly recognize a previously recognized non-POT (power of two) as a POT. The reasoning is simple - we SET a bit before AND'ing, so the result can only increase (but not overflow). Since a previous nonzero result marked a non-POT, and the result could have only increased the stated is true.

The only other possible error is recognizing a a previous POT as a non-POT (not counting zero). When you look at POT's in binary they look like this:

use std::collections::HashMap;
use rand::{Rng, seq::SliceRandom};
use std::time::Instant;
fn rand_input(rng: &mut impl Rng) -> String {
let theirs = ["A", "B", "C"].choose(rng).unwrap();
let ours = ["X", "Y", "Z"].choose(rng).unwrap();
format!("{theirs} {ours}\n")
}
@orlp
orlp / gist:1657509
Created January 22, 2012 15:57
Pure genius
# Matthew N. Brown copyright 2007
# Here is an example program in wich
# balls hit walls and other balls:
#
# This program draws circles using: pygame.draw.circle
#
# You can copy this program on to
# your own computer and run it.
#
#include <stdint.h>
const uint32_t squares[] = {
0ul, 1ul, 4ul, 9ul, 16ul, 25ul, 36ul, 49ul,
64ul, 81ul, 100ul, 121ul, 144ul, 169ul, 196ul, 225ul,
256ul, 289ul, 324ul, 361ul, 400ul, 441ul, 484ul, 529ul,
576ul, 625ul, 676ul, 729ul, 784ul, 841ul, 900ul, 961ul,
1024ul, 1089ul, 1156ul, 1225ul, 1296ul, 1369ul, 1444ul, 1521ul,
1600ul, 1681ul, 1764ul, 1849ul, 1936ul, 2025ul, 2116ul, 2209ul,
2304ul, 2401ul, 2500ul, 2601ul, 2704ul, 2809ul, 2916ul, 3025ul,
@orlp
orlp / configure.py
Last active October 15, 2022 21:39
A template for a ninja configuration file.
import os
import sys
import fnmatch
__doc__ = """Build configurator.
Usage:
configure.py [options] [-D var[=val]]...
#include <stdint.h>
const uint16_t squares[] = {
0, 1, 4, 9,
16, 25, 36, 49,
64, 81, 100, 121,
144, 169, 196, 225,
256, 289, 324, 361,
400, 441, 484, 529,
576, 625, 676, 729,
@orlp
orlp / 1_18_seeds.csv
Created December 2, 2021 10:56
Minecraft 1.18 seeds
seed shx shy mushroom outpost fortress frozen warm desert
6549009348085030062 2272 -1244 232052 1899 860 1534 384 923
992588754612856117 -1960 -1800 218284 2314 240 2384 2072 1897
8115723686485296971 -1928 -736 211024 2050 830 2960 467 1272
-5535894614307895477 1980 -1384 205828 2028 819 555 1679 2017
-79422374837388415 2516 200 205212 2110 583 2079 966 1089
7770996459503435924 1912 -1764 202200 2134 927 2921 224 961
4966063911710165295 -2264 476 199088 2865 478 3348 339 782
-2080965392937654313 1856 1508 197172 2072 965 2482 1477 1650
3906848925020110572 -2000 684 194548 1396 112 2851 1137 1173