Skip to content

Instantly share code, notes, and snippets.

@io-developer
io-developer / pairs.js
Created March 25, 2018 16:02
javascript array to pairs
function pairs(items) {
return items
.map((item, index, items) => [item, items[(index + 1) % items.length]]);
}
@io-developer
io-developer / shuffle.js
Last active March 25, 2018 16:01
javascript array shuffle
function shuffle(items) {
return items
.map(item => [item, Math.random()])
.sort((a, b) => (a[1] > b[1]) - (a[1] < b[1]))
.map(item => item[0]);
}
<?php
// create whois instance
$whois = Whois::create();
// load hosts from json
$json = json_decode(file_get_contents('your.json'), true);
// add custom servers to whois instance
$servers = $whois->getServerProvider();
@io-developer
io-developer / one-step-square-spiral-coord-calc.as
Last active March 4, 2018 16:18
Optimized square spiral coord calculation
public static function calcSquareSpiralPos( outPos:Point, iteration:int, phaseShift:int=0, CCW:Boolean=false ) : void
{
var n:int = (2.0 * Math.sqrt(iteration + 1) - 0.000001);
var p:int = (n + phaseShift) % 4;
var h:int = (0.5 * (1.0 + n + (p & 0x1)));
var v:int = (0.5 * (2.0 + n - (p & 0x1)));
var d:int = iteration - int(0.5 * ((h * (h - 1)) + (v * (v - 1))));
outPos.x = ((2 * ((1.5 - p) & 0x1) - 1) * int(0.5 * h) + d * ((p - 1) * ((p + 1) & 0x1))) * (1 - 2 * int(CCW));
outPos.y = ((1 - 2 * ((0.5 * p) & 0x1)) * int(0.5 * v) + d * ((p - 2) * (p & 0x1)));
@io-developer
io-developer / adventofcode2017_day3_1_superfast.js
Last active March 4, 2018 16:21
AdventOfCode 2017 day 3-1
function calc(inputNum) {
var coord = spiralCoord(inputNum - 1, -1, true);
return Math.abs(coord.x) + Math.abs(coord.y);
}
function spiralCoord(iteration, phaseShift, isCCW) {
var n = Math.floor(2.0 * Math.sqrt(iteration + 1) - 0.000001);
var p = (n + phaseShift) % 4;
var h = Math.floor(0.5 * (1.0 + n + (p & 0x1)));
var v = Math.floor(0.5 * (2.0 + n - (p & 0x1)));
@io-developer
io-developer / adventofcode2017_day3_1.js
Last active March 4, 2018 16:21
AdventOfCode 2017 day 3-1
function calc(inputNum) {
var coord = spiralCoord(inputNum - 1);
return Math.abs(coord.x) + Math.abs(coord.y);
}
function spiralCoord(index) {
var coord = { x: 0, y: -1 };
var dir = 3;
var vecs = {
0: { x: 1, y: 0 },
@io-developer
io-developer / adventofcode2017_day2_2.js
Last active March 4, 2018 16:21
AdventOfCode 2017 day 2-2
function calc(input) {
var checksum = 0;
input.split('\n').forEach(row => {
var nums = row.split('\t').map(n => parseInt(n));
checksum += findIntDivs(nums).reduce((a, b) => a + b, 0);
});
return checksum;
}
function findIntDivs(nums) {
function calc(input) {
var checksum = 0;
input.split('\n').forEach(row => {
var nums = row.split('\t').map(n => parseInt(n));
var min = nums.reduce((a, b) => Math.min(a, b));
var max = nums.reduce((a, b) => Math.max(a, b));
checksum += max - min;
});
return checksum;
}
@io-developer
io-developer / generate_sets.php
Last active March 4, 2018 16:25
binary sets generator
<?php
function generate_sets($values) {
$total = 1 << count($values);
for ($i = 0; $i < $total; $i++) {
$subset = [];
foreach ($values as $j => $val) {
if ($i & (1 << $j)) {
$subset[] = $val;
}
@io-developer
io-developer / bin-sets.php
Last active March 4, 2018 16:25
binary sets
<?php
function sets_of($list) {
$sets = [];
$total = 1 << count($list);
for ($i = 0; $i < $total; $i++) {
$sub = [];
foreach ($list as $j => $val) {
if ($i & (1 << $j)) {
$sub[] = $val;