Skip to content

Instantly share code, notes, and snippets.

@0racle
0racle / point.py
Last active December 14, 2022 13:18
Point example and AoC 2022-09
from dataclasses import dataclass
@dataclass(frozen=True)
class Point:
x: float
y: float
def __repr__(self):
return f"({self.x},{self.y})"
@0racle
0racle / aoc-2022-05.py
Created December 5, 2022 18:36
AoC 2022 Day 5
import re
cx, mx = map(str.splitlines, open('input').read().split("\n\n"))
batch = lambda xs, n: (xs[i : n + i] for i in range(0, len(xs), n))
c = [[x[1].strip() for x in batch(line, 4)] for line in cx[:-1]]
c = [list(filter(len, x)) for x in zip(*c)]
d = [e.copy() for e in c.copy()] # deepcopy for part 2
@0racle
0racle / solve.md
Last active November 25, 2022 05:44
AoC 2016 Day 2 in J
NB. === Advent of Code [2016] ===
NB. --- Day 2: Bathroom Security ---

moves =. <@:+.@(0j1 ^ 'DRUL'&i.);._2 fread 'input'

Move =. {{
    x&{{ p =: p + y * ((< p + y) { x ~: '.') }}"1 y
    (< p) { x
}}
@0racle
0racle / j.raku
Last active October 12, 2022 12:17
J in Raku (and Perl)
#!/usr/bin/env raku
use NativeCall;
class J is repr('CPointer') {
my constant $BIN = "/opt/j903/bin";
my constant $LIB = "$BIN/libj.so";
my constant $PRO = "$BIN/profile.ijs";
@0racle
0racle / janitor.ijs
Last active December 20, 2023 07:09
Janitor - J code formatter
#!/usr/bin/env jconsole
NB. No space before these tokens
bef =: ;: ') @ @. @: & &. &: &.: " ` ;. !. !: / /. \ ~ }'
NB. No space after these tokens
aft =: ;: '( @ @. @: & &. &: &.: " ` ;. !. !:'
Cont =: {{ +./ ((dltb > x) (-: ;)"1 0 y) }}
Fmt =: {{
my %ops = (AND => &[+&], OR => &[+|], LSHIFT => &[+<], RSHIFT => &[+>]);
my (%cct, %mem);
my &get = -> \val { %mem{val} //= +val // %cct{val}() }
for $=finish.lines {
given .words {
when 3 { %cct{.[2]} = { get(.[0]) } }
when 4 { %cct{.[3]} = { +^get(.[1]) +& 0xFFFF } }
when 5 { %cct{.[4]} = { %ops{.[1]}(get(.[0]), get(.[2])) } }
use v6.d;
class A {
has $!a = 'private';
method !a { $!a }
}
multi sub trait_mod:<is>(Mu:U \obj, :$spying!) {
A.^add_trustee(obj);
A.^compose;
@0racle
0racle / partition.md
Last active January 23, 2024 03:07
A "partition" adverb in J (similar to APL's ⊆)

A "partition" adverb in J (similar to APL's ⊆)

P =: {{ (1, 2 </\ x) u;.1&((0 < x)&#) y }}

Partition on delimiter (à la (≠⊆⊢))

   ',' (~: <P ]) 'comma,separated,values'
┌─────┬─────────┬──────┐
@0racle
0racle / dyalog-inputrc
Last active May 4, 2022 00:19
inputrc file for Dyalog APL prefix mappings (à la TryAPL.org)
# If you want to use this with rlwrap, the following command works for me
# > INPUTRC='~/.dyalog/inputrc' rlwrap -a dyalog -s -b
# Which can then be aliased to whatever you like
"`[": "←"
"`-": "×"
"`=": "÷"
"`p": "*"
"`*": "⍟"
@0racle
0racle / teeko.md
Last active August 3, 2022 01:09
Teeko

One of the problems in the "2016 International APL Problem Solving Competition (General Computing Category)" was as follows

Write an APL function named 'winner' which

  • takes a right argument of a 5x5 character matrix representing a Teeko board
  • if there is a winner, returns the appropriate character for the winner, otherwise returns the character representing an unoccupied space

The competition was won by Joshua David and he briefly goes over his solution in this video, but elides most of the details.

A winner can be 4 in a row - either horizontally, vertically, or diagonally - or a square cluster of 4. Below is my attempt at writing this winner function in both J and BQN.