Skip to content

Instantly share code, notes, and snippets.

@jbetz34
jbetz34 / sha256.q
Created August 6, 2021 13:42
SHA-256 built in q
\d .sha
// frequently used config functions
cfg.zeroPadding:{*[x;ceiling abs count[y]%x]#$[0>x;,[x#0;];,[;x#0]] y}
cfg.hex2bin:{cfg.zeroPadding[-8]{$[x<2;:reverse y,x;y,:x mod 2];.z.s[;y] div[x;2]}[;()] `long$x};
cfg.bin2dec:{sum prd each x,'#\:[;2] reverse til count x};
cfg.bin2hex:{`byte$cfg.bin2dec each 8 cut x};
cfg.binary:{raze cfg.hex2bin each `byte$x};
cfg.rightrotate:{rotate[neg[y];x]};
cfg.rightshift:{while[y;x:0^prev x;y-:1];x};
@jbetz34
jbetz34 / bigMath.q
Last active February 11, 2023 07:35
Multiplication beyond the 18 digit limit
// ORIGINAL METHOD - EXPONENTS
v:{[x] "J"$/:string x}
mult:{[x;y]
m:reverse sum v[x]*/:{x*prd each #\:[;10]reverse til count x}v y;
while[any 9<m;m:(0^next p)+m-10*p:div[;10]m:0f,m];
(?[;1b]"b"$m)_m
}
pwr:{(mult/)y#x}
// NEW METHOD - MATRICES
@jbetz34
jbetz34 / qWordle.q
Created January 22, 2022 01:10
Find optimal wordle guess
// initialize word list
wordz:system "curl http://wiki.puzzlers.org/pub/wordlists/unixdict.txt";
wordz:wordz where all (5=count each wordz;all each wordz in .Q.a);
// create base word table
w:([]word:wordz;I:wordz[;0];II:wordz[;1];III:wordz[;2];IV:wordz[;3];V:wordz[;4]);
// generic wordzScore function
// x - words table
// y - column to analyze
@jbetz34
jbetz34 / leetcode649.q
Last active May 5, 2023 15:21
Q solution to LeetCode #649
// Generate Test Senate
PARTIES:"DR";
partyDict:"DR"!("Dire";"Radiant");
genSenate:{[x] x?PARTIES};
// Utility functions
// If one party controls 2/3 (2:1 ratio) or more of the senate, they will win
// This function will compare the party counts
// if one has super majority, it will return the party name
@jbetz34
jbetz34 / leetcode59.q
Created May 12, 2023 03:48
Q solution to LeetCode question 59. can also be used for 54
// Define our helper functions
// rotates the box 90 degrees ccw
turn:{[box] flip reverse each box}
// returns indices of first available nulls in box (top to bottom)
fni:{[box] 1 (where i@)\ first where any each i:null box}
// fills first nulls in the box with first numbers in the line
ffn:{[box;line;indices] .[box; indices; :; count[indices]#line]}