Skip to content

Instantly share code, notes, and snippets.

View vjeranc's full-sized avatar
🏋️‍♂️

Vjeran Crnjak vjeranc

🏋️‍♂️
View GitHub Profile
@vjeranc
vjeranc / test.md
Created May 11, 2019 14:34
leptonica tests on mac os x
$ ./alltests_reg generate
Running alltests_reg:
This currently tests 129 of the 129 regression test
programs in the /prog directory.

////////////////////////////////////////////////
////////////////   adaptmap_reg   ///////////////
////////////////////////////////////////////////
leptonica-1.79.0 : libgif 5.1.4 : libjpeg 9c : libpng 1.6.37 : libtiff 4.0.10 : zlib 1.2.11 : libwebp 1.0.2 : libopenjp2 2.3.1
// Run in https://thebookofshaders.com/edit.php#11/
// TODO:
// - would be nice to just use the polygon instead of triangle
// + keeping track of the orientation as one descends to lower levels
// - to put the borders
// - to normalize the width/height regardless of zoom level
// + probably find the biggest tile containing the tiles of given size on the Xth level
// - add other tiles
#ifdef GL_ES
@vjeranc
vjeranc / day12.py
Last active December 13, 2022 20:30
Day 12 solution without queues for Advent of Code 2022 at https://adventofcode.com/2022/day/12
g = []
for line in open('day12.input'):
g.append([ord(c) for c in line.strip()])
n = len(g)
# find S and E and transform to ord('a') and ord('z')
s, e = None, None
for i in range(n):
for j in range(len(g[i])):
if g[i][j] == ord('S'):
g[i][j] = ord('a')
@vjeranc
vjeranc / day12.nim
Last active December 13, 2022 22:39
Day 12 solution without queues in Nim for Advent of Code 2022 at https://adventofcode.com/2022/day/12
import std/strutils
import std/sequtils
type Loc = tuple
x, y: int
proc parseGrid(): tuple[s: Loc, e: Loc, g: seq[seq[int]]] =
var
g: seq[seq[int]]
s, e: Loc
@vjeranc
vjeranc / alea-prng.ts
Last active October 21, 2023 16:20
A JavaScript module implementing the Alea pseudo-random number generator with seed, offering functions to generate float32, uint32, fract53, and Gaussian-distributed random numbers for non-cryptographic purposes. Original author: Baagøe https://github.com/nquinlan/better-random-numbers-for-javascript-mirror
interface AleaPRNG {
float32: () => number;
uint32: () => number;
fract53: () => number;
gaussian: (mean?: number, std?: number) => number;
}
/**
* Alea PRNG that is not cryptographically secure.
*/
export const mkAlea = (seed: string): AleaPRNG => {
@vjeranc
vjeranc / penrose-tiling.frag
Last active December 17, 2023 06:08
Fragment shader tiling the Las Vegas sphere in Penrose https://whenistheweekend.com/theSphere.html
#define phi 1.61803398874989484820
#define h 1.53884176858762670128
uniform float time;
varying vec2 vUv;
varying vec3 vNormal;
float cx(in vec2 a, in vec2 b) {
return a.x * b.y - a.y * b.x;
}
# ts, ds = [[int(i) for i in l.split()[1:]] for l in open(0).read().split('\n') if l] # part 1
ts, ds = [[int(i) for i in [''.join(l.split()[1:])]] for l in open(0).read().split('\n') if l] # part 2
def binary_search(f, l, r):
while l < r:
m = (l+r)//2
if f(m):
l = m+1
else:
r = m
from collections import Counter
SND = True # False for part 1
def hand(cnts: Counter):
if SND and 'J' in cnts:
for c, _ in cnts.most_common(2):
if c == 'J': continue
cnts[c] += cnts['J']
cnts.pop('J', 0)
G = open(0).read().splitlines()
N, M = len(G), len(G[0])
# dest needs to be valid and the dest direction change is predetermined
# (dx, dy, valid_dest, next MS index)
MS = [(1, 0, "|LJS", [0, 1, 3, 2]), (0, 1, "-J7S", [1, 2, 0, 3]),
(-1, 0, "|7FS", [2, 3, 1, 0]), (0, -1, "-FLS", [3, 0, 2, 1])]
sj, si = max((l.find('S'), i) for i, l in enumerate(G))
best = 0, 0
for d in range(3): # trying 3 directions is enough
def collect(u):
ri, i = [], 0
for r in u:
n = r.count('#')
if n: ri.extend([i]*n)
i += 1 if n else D
return ri
D = 1000000 # 2 # for part 1
manhattan_sum = lambda xs: sum((1+i+i-len(xs))*x for i, x in enumerate(xs))