Skip to content

Instantly share code, notes, and snippets.

function open_segments_intersect(a1, a2, b1, b2) {
if ((max(a1.x, a2.x) <= min(b1.x, b2.x)) || // a is left of b
(max(b1.x, b2.x) <= min(a1.x, a2.x)) ||
(max(a1.y, a2.y) <= min(b1.y, b2.y)) || // a is above b (in coordinates where (0,0) is topleft)
(max(b1.y, b2.y) <= min(a1.y, a2.y))
) {return false;}
else if ((vcross(vsub(b1,a1),vsub(a2,a1)) * vcross(vsub(b2,a1),vsub(a2,a1)) < 0) && // replace these with <= to make the segments act closed.
(vcross(vsub(a1,b1),vsub(b2,b1)) * vcross(vsub(a2,b1),vsub(b2,b1)) < 0)) {return true;}
else {return false;}
}
/* this code is licensed CC-0 https://creativecommons.org/publicdomain/zero/1.0/ . use as you see fit.*/
function distance(a, b) {
/* Find the Euclidean distance between two 2-dimensional points.
*
* a: the first point.
* b: the second point.
*
* returns: the distance.
*/
Apr 12 19:49:38 <zHafydd> You can derive 11^5 from the 11th row of Pascal's triangle: 1, 5, 10, 10, 5, 1.
Apr 12 19:50:13 <lemonade`> how would that work?
Apr 12 19:50:28 <zHafydd> 11^5 = 1 + 5*10 + 10*10^2 + 10*10^3 + 5*10^4 + 1*10^5
Apr 12 19:51:11 <zHafydd> A consequence of the binomial theorem.
Apr 12 19:51:14 <lemonade`> weird.
Apr 12 19:51:50 <zHafydd> If you think it's weird, you haven't thought about it enough.
Apr 12 19:52:25 <baxx> zHafydd: should the binomial be obvious?
Apr 12 19:52:30 <lemonade`> I likely haven't. I'm on it now :)
Apr 12 19:53:08 <zHafydd> baxx: should the binomial theorem be obvious? To a person practicing mathematics beyond an elementary level, yes.
Apr 12 19:53:35 <baxx> zHafydd: because they've been told or because, using a basic toolset, it's obvious
do
local ArcGroup_class_table = {}
function ArcGroup(x, y)
-- construct an ArcGroup around the chosen point. The starting interval is a single arc,
-- containing the whole circle.
return setmetatable({x = x, y = y, intervals = {{0, 2*math.pi}}}, { __index = ArcGroup_class_table })
end
function ArcGroup_class_table:width()
#!/usr/bin/env/python3
import random
gaps = []
events = 0
current_gap = 0
for trial in range(1000000):
if random.random() < (1/500):
gaps.append(current_gap)
current_gap = 0
events += 1
function aStar_compare(a, b) {
return a.cost + a.estimate < b.cost + b.estimate || (a.cost + a.estimate == b.cost + b.estimate && a.cost < b.cost);
}
var Heap = function(compare) {
if (!compare) {
compare = function(a, b) {return a < b;};
}
this.compare = compare;
this.heap = [];
@DUznanski
DUznanski / 12marbles.py
Created October 2, 2017 02:02
12 Marbles Problem
from collections import Counter, defaultdict
from itertools import combinations_with_replacement, product
def sgn(x):
"""Return -1, 0, or 1, whichever matches the sign of x."""
if x < 0: return -1
if x > 0: return 1
return 0
# generate the layouts and make it easy to find the signatures.
# random-fill testing:
def makes_sufficently_large_table():
unittest.assertEqual(generate_choices([('a',2),('b',1),('c',2)]), ['a','a','b','c','c'])
# ... similar. Include, among others, things with fallbacks that don't get used.
def fills_small_table():
choices = generate_choices([('a',2),('b',1),('c',0),('d',0)])
# make sure the first part is all the ones with known frequencies
# this actually suggests that what we really want is to break this down into more functions:
@DUznanski
DUznanski / abudhabi_greedy.py
Created July 12, 2018 16:12
Greedy algorithm for abudhabi
from collections import Counter
def abudhabi_greedy(target, candidates):
true_candidates = sorted(candidates + candidates, reverse=True)
signatures = set()
for result in greedy_algorithm(target, true_candidates):
count = Counter(result)
signature = tuple(count[k] for k in candidates)
if signature not in signatures:
yield signature
@DUznanski
DUznanski / faction_list.py
Created July 13, 2018 17:26
Magic the Gathering faction list generator
def check_faction_set_validity(factions):
# count the number of on bits in each position for our list of faction IDs. If they're all the same, then the faction list is valid.
return factions and len({sum((faction >> k) & 1 for faction in factions) for k in range(5)}) == 1
def faction_string(faction):
return ''.join('WUBRG'[i] for i in range(5) if (faction >> i) & 1)
def faction_list_string(factions):
return ','.join(faction_string(faction) for faction in factions)