JohnE
  • Member for 6 years, 10 months
  • Last seen more than a month ago
Minimal NetHack
44 votes

CHIP-8, 48 bytes This may not be considered legal, but why the hell not. I wrote my program in CHIP-8, a bytecode-based programming language for a virtual game console. You can try the complete ...

View answer
Flatten the Array!
Accepted answer
40 votes

K, 3 bytes ,// This is a fairly common idiom. "Join over converge". try it here with oK. How it works: Join (,) fuses together atoms or lists to produce a list. Over (/) takes a verb (in this ...

View answer
Check if words are isomorphs
37 votes

K, 5 bytes This has a delightfully elegant solution in K! ~/=:' The "group" operator (monadic =) creates precisely the signature we want for word isomorphism; gathering vectors of the indices of ...

View answer
Determine the depth of an array
Accepted answer
21 votes

K, 4 bytes #,/\ In K, ,/ will join all the elements of a list. The common idiom ,// iterates to a fixed point, flattening an arbitrarily nested list completely. ,/\ will iterate to a fixed point in ...

View answer
Goodness, it's covered in tabs!
19 votes

K5, 53 45 bytes {{,/(b+a&~b:x*&\a:9=y){$[x;x#" ";y]}'y}[x]'y} In action: {{,/(b+a&~b:x*&\a:9=y){$[x;x#" ";y]}'y}[x]'y}[4;(,"a";"\t\tb\tc";,"d")] (,"a" " b c" ,"d") I ...

View answer
Non Unique Elements
Accepted answer
17 votes

K5, 5 bytes Assuming the input is already in a variable called d, ?d^?d Take the distinct elements (?) of d except (d^) the distinct elements of d (?d). Nicely symmetrical, no? This works because ...

View answer
Print all colorings of a 3x3 grid
Accepted answer
16 votes

K, 11 bytes (3 3#)'!9#2 Output example: ((0 0 0 0 0 0 0 0 0) (0 0 0 0 0 0 0 0 1) (0 0 0 0 0 0 0 1 0) (0 0 0 0 0 0 0 1 1) … This is K's native prettyprinted representation of a ...

View answer
Pixel-art, episode 1: display Super Mario
10 votes

Octo/XO-Chip, 70 bytes Octo is a high level assembly language that compiles down into bytecoded instructions for the CHIP-8 virtual machine. Octo provides some custom extensions called "XO-Chip" to ...

View answer
Draw the national flag of France
8 votes

iKe, 43 bytes ,(0 0;"|"\"#0055A4|#FFF|#EF4135";52#,&3#26) This is an example of a "raw tuple" iKe program- it's just a description of an origin (0 0), a palette (3 7#"#0055A4#FFFFFF#EF4135") and ...

View answer
Randomize points on a disc
8 votes

iKe, 53 51 bytes Nothing particularly special, but I suppose we ought to have at least one graphical solution: ,(80+160*t@&{.5>%(x*x)+y*y}.+t:0N 2#-.5+?9999;cga;3) Try it in your browser. ...

View answer
Surround a string with hashes
7 votes

K, 21 bytes 4(|+"#",)/4(|+" ",)/, Enlist the string, Add a space to all four sides of a string, then add an octothorpe to each side of the string. In action: 4(|+"#",)/4(|+" ",)/,"Hello." ("######...

View answer
Show tree rings age
7 votes

K5, 27 30 26 25 22 bytes "0"{4(|+y,)/x}/"0*+"3!1_! This approach iteratively "wraps" a core (beginning with "0") on all four sides using some other character ({4(|+y,)/x}). The sequence of seasonal ...

View answer
Mode (most common element) of a list
Accepted answer
6 votes

K5, 6 bytes *>#:'= The first (*) of the descending elements (>) of the count of each (#:') of the group (=). Step by step: i 4 3 1 0 6 1 6 4 4 0 3 1 7 7 3 4 1 1 2 8 =i 4 3 1 0 6 7 2 8!(0 ...

View answer
Generate a Rectangle from a Specification
Accepted answer
6 votes

K, 123 110 bytes I used a similar approach to cardboard_box's solution. r:{y,x#,*|y};h:{t:(#x)|#y;r[t-#x;x],'r[t-#y]y};a:{(,x .|2#y),2_ y};*(){(a[{+h[+x;+y]}]x;a[h]x;(,,y),x)"-|"?y}/ This program ...

View answer
Convert Brainfuck to a Non-Golfing Language
5 votes

K, 143 129 113 bytes 1!"}int a[99];int*p=a;main(){",,/{("p++;";"p--;";"*p++;";"*p--;";"*p=getchar();if(*p<0)*p=0;";"putchar(*p);";"while(*p){";"}";"")"><+-,.[]"?x}' This is an extremely ...

View answer
Iterated partial sums
5 votes

K, 7 3 bytes {y+\/x} Very similar to the J solution. +\ precisely performs a partial sum, and when / is provided with a monadic verb and an integer left argument it iterates a specified number of ...

View answer
Making Future Posts Runnable Online with Stack Snippets
5 votes

K5 This is a thin, simple wrapper around my JavaScript based K implementation, oK, and references the github repo. I've already built a much nicer browser-based frontend. oK implements a very ...

View answer
Compute the Digit Difference Sum of a Number
5 votes

oK, 37 32 24 23 bytes +/(10/{%x*x}1_-':.:'$)\ In action: +/(10/{%x*x}1_-':.:'$)\8675309 8898683 (+/(10/{%x*x}1_-':.:'$)\)'!20 0 1 2 3 4 5 6 7 8 9 11 11 13 15 17 19 21 23 25 27 K5 has a few ...

View answer
Tips for golfing in K
5 votes

Calling a Dyad Assuming you had a dyadic (2-argument) function f: f: {x+2*y} You would ordinarily call it like so: f[3;47] You can save a character by instead currying in the first argument and ...

View answer
Generalised Array Riffle
5 votes

K, 22 21 bytes {,//+(y@<z;(z@<z)_ x)} We define a 3 argument function {…} with the implicit variables x, y and z representing the starting list, the value list and the index list, respectively....

View answer
Fake Marquee Text
5 votes

K, 19 bytes {x#'![1]\(x#" "),y} Tack x spaces (x#" ") onto the beginning of the string y. Then use the "fixed-point scan" form of the operator \ to create the set of rotated strings. A fixed-point ...

View answer
Atbash Self Palindromes
4 votes

Kerf, 73 bytes Kerf is a proprietary language in the same general family as APL, J and K. It's possible to write cryptic, compact oneliners and avoid the use of explicit loops: {[s]s:_ s;n:?isnull i:...

View answer
AGM Series Hole 1: Calculate the Arithmetic–Geometric Mean
4 votes

K5, 15 bytes Very literal: {(+/x%2;%*/x)}/ In action: {(+/x%2;%*/x)}/[0; 24 6] 24 6 {(+/x%2;%*/x)}/[5; 24 6] 1.345817e1 1.345817e1 Unfortunately, this does not work in oK because that ...

View answer
Running length of the movie in a human readable format
4 votes

K5, 55 51 bytes " "/(" hour";" minute"){y,x,("s";"")1=.y}'$25 60\.*" "\ This is more general than it strictly has to be; might still golf it down further. In action: f: " "/(" hour";" minute"){y,...

View answer
Lossy Sorting (Implement Dropsort)
4 votes

K, 11 bytes {x@&~x<|\x} In action: f: {x@&~x<|\x} f'(1 2 5 4 3 7 10 -1 12 -7 -8 -5 0 -1 1 9 8 7 6 5 10 13 17 21 10 10 10 9 10) (1 2 5 7 10 12 -7 -5 0 1 ...

View answer
Non-repeating random numbers
4 votes

K5, 19 bytes Not particularly clever: t:-1;r:{t::*1?10^t} Each time r is invoked, it picks 1 random number (*1?) from the set formed by taking the range 0-9 except (^) the previous random number ...

View answer
Fibonacci Spiral
4 votes

K, 48 bytes {{`0:1_',/'(1+#$|//x)$x}(x-1){+|x,\:t#t:#x}/,,1} And in action: {{`0:1_',/'(1+#$|//x)$x}(x-1){+|x,\:t#t:#x}/,,1}7 8 8 8 8 8 8 8 8 5 5 5 5 5 8 8 8 8 8 8 8 8 5 5 ...

View answer
Palindrome Reversal-Addition
4 votes

K, 25 bytes #1_{~x~|x}{$. x,"+",|x}\$ Not very elegant. The overall form ({monad 1}{monad 2}\x) is K's equivalent of a general "while" loop, where the first monad is the halting condition and the ...

View answer
Tips for golfing in K
4 votes

Ranges Normally if you want to create a vector of sequential numbers you use !: !5 0 1 2 3 4 If you want to create a range that starts at a number other than zero, you'd then add an offset to the ...

View answer
Fibonacci-style matrix expansion
3 votes

K, 23 bytes {x(2({x,+/-2#x}'+)/)/y} In action: {x(2({x,+/-2#x}'+)/)/y}[3;(1 1 1;2 3 4)] (1 1 1 2 3 5 2 3 4 7 11 18 3 4 5 9 14 23 5 7 9 16 25 41 8 11 14 25 39 64) Try it here.

View answer