Skip to content

Instantly share code, notes, and snippets.

View 4hg's full-sized avatar
🤯
-/ .*_3[\9?100

Luke 4hg

🤯
-/ .*_3[\9?100
View GitHub Profile
@4hg
4hg / jp.rb
Last active July 4, 2024 20:08
Jurassic Summer Challenge
# The following adjacency matrix includes a super source `i1` and a super sink `o1`.
# This allows us to implement max-flow where there is one source and sink.
# The edge weights used are the sums of all outgoing edge weights for the original sources
# and the sums for all incoming edge weights for the original sinks.
GRAPH = [
#i1 A B C D E F G H I J K L M N O P o1
[0, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 105, 0, 0, 0, 0, 0], #i1
[0, 0, 0, 0, 0, 23, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], #A
[0, 23, 0, 20, 0, 0, 0, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], #B
[0, 0, 0, 0, 4, 0, 0, 18, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0], #C
@4hg
4hg / wwrld.rb
Created February 11, 2024 00:05
Valentine's Day WireWorld
########################################################################
# Wireworld Valentine Challenge
# Objective: Implement Wireworld and create a Valentine circuit!
# Start Date: February 2, 2023
# End Date: February 14, 2023
########################################################################
class WireworldValentine
EMPTY = "🟪" # State 0
ELECTRON_HEAD = "🟥" # State 1
ELECTRON_TAIL = "🟦" # State 2
@4hg
4hg / J80.ijs
Created September 12, 2023 01:00
Skeleton code for mimicing TIC-80
require 'trig viewmat'
coinsert 'jgl2'
cs =: (3#256) #: 16b1a1c2c 16b5d275d 16bb13e53 16bef7d57 16bffcd75 16ba7f070 16b38b764 16b257179 16b29366f 16b3b5dc9 16b41a6f6 16b73eff7 16bf4f4f4 16b94b0c2 16b566c86 16b333c57
wh =: 960 554
wd'reset'
wd 'pc w0;minwh ',":wh
wd 'cc g0 isidraw;'
wd 'pshow; pmove 40 40 0 0'
@4hg
4hg / ht.ijs
Last active July 19, 2023 03:47
Separate chaining hash table
coclass 'ht'
create =: {{
table =: y # a:
ts =: # table NB. table size
o =: 0 NB. occupancy
}}
destroy =: codestroy
inspect =: {{ table }}

A collection of my submissions to http://golf.shinh.org/.

Each are as-is post mortem, therefore most could be improved.

J

echo(toupper a{~c)(c=:I.'#'=stdin'')}a=:433$LF,~30$'helloworld'

Ruby

$<.bytes{|b|putc ($/+"helloworld"*3)[$.%31]-b[0]*32;$.+=1}

Ruby2

$<.bytes{|b|putc ($/+"helloworld"*3)[($.+=1)%31].ord-b[0]*32}

@4hg
4hg / tca.ijs
Created May 4, 2022 15:02
1D Totalistic Cellular Automata code written for my writeup on the subject.
require 'viewmat'
NB. rules are base-4, but the base is dependent on the number of states
NB. output is 500x500
NB. colors are encoded as hex
NB. r - 0 for custom rule, 1 for random rule
NB. s - 0 for single 1-cell start, 1 for random start
NB. c - 0 for custom colors, 1 for random colors
NB. g - iter function
NB. edit these to change modes
@4hg
4hg / base32.ijs
Last active February 20, 2022 11:54
Base32 Encoder and Decoder in J
NB. ENCODER
NB. information about algorithm came from https://herongyang.com/Encoding/Base32-Encoding-Algorithm.html
NB. RFC 4648 character set
cs =: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'
NB. interval array
ia =: {{0 = 5 | i. # y}}
NB. padded length value
@4hg
4hg / hex_chaos.rb
Created September 29, 2021 20:44
Chaos game with a hexagon using the incenter of triangles for each step.
require 'ruby2d'
# origin is (320,240)
# Do not care to convert radians to degrees, 60 degrees = PI/3 radians
SIXTY_D = Math::PI / 3
def rotation_x(x, y, theta) = (x * Math.cos(theta)) - (y * Math.sin(theta))
def rotation_y(x, y, theta) = (x * Math.sin(theta)) + (y * Math.cos(theta))
@4hg
4hg / sierpinski.rb
Created August 26, 2021 21:45
Simple Sierpinski triangle using ruby2d.
require 'ruby2d'
set title: "Sierpinski Triangle", background: "white"
vertices = [[Window.width / 2, 50, "blue"], [Window.width / 2 - 150, 350, "green"], [Window.width / 2 + 150, 350, "red"]]
point = [250, 200]
update do
v = vertices.sample
point = [0.5 * (point[0] + v[0]), 0.5 * (point[1] + v[1])]
@4hg
4hg / bf.rb
Created August 25, 2021 18:37
My first and messy brainfuck interpreter written in ruby.
class Interpreter
def initialize
@stack = Array.new(30_000, 0)
@loop_stack = Array.new
@prog = ""
@instruction_pointer = 0
@data_pointer = 0
end
def load_file file_name