Skip to content

Instantly share code, notes, and snippets.

View xpqz's full-sized avatar
🏠
Working from home

Stefan Kruger xpqz

🏠
Working from home
View GitHub Profile
@xpqz
xpqz / day8.apl
Last active December 8, 2021 15:08
⍝ https://adventofcode.com/2021/day/8
d←⊃⎕NGET'../d/8'1
data←' '(≠⊆⊢)¨⍤1⊢(⎕CSV⍠'Separator' '|')(d)''4
+/∊2 3 4 7 8∘.=≢¨↑data[;1] ⍝ part 1
seg←{ ⍝ find segment mapping from ⍺ and decode ⍵
frq←{≢⍵}⌸{⍵[⍋⍵]}∊obl←⍺[⍋≢¨⍺]
a←'abcdefg'
s←7⍴''
s[0]←⊃(1⊃obl)~0⊃obl
@xpqz
xpqz / day7.apl
Last active December 7, 2021 09:40
d←∊⎕CSV(⊃⎕NGET'../d/7'1)''4
⌊/+/t←(⍳⌈/d)(|-)⍤0 1⊢d⋄⌊/+/{2÷⍨⍵×⍵+1}¨t ⍝ Gauss sum
⎕FR ⎕PP ⎕IO←1287 34 0
init←{⍺ (≢⍵)}⌸∊⎕CSV(⊃⎕NGET'../d/6'1)''4
state←init[;1]@(init[;0])⊢9⍴0
day6←{s←9⍴0⋄s[6 8]←⍵[0]⋄s[⍳8]+←⍵[1+⍳8]⋄s}
+/day6⍣80⊢state
+/day6⍣256⊢state
'iotag'⎕CY'dfns'
data←↑⍎¨'[^\d]+'⎕R' '⊢⊃⎕NGET'../d/5'1
f←{d←⍵⋄+/∊1<⊃+/{1∘+@⍵⊢(1+⌈⌿2 2⍴⌈⌿d)⍴0}¨⊃↓{⊃,¨/iotag⌿2 2⍴⍵}¨↓d}
f data⌿⍨{+⌿=⌿2 2⍴⍵}⍤1⊢data⋄f data
@xpqz
xpqz / readday4.apl
Last active December 4, 2021 09:27
⎕IO←0
d←⊃⎕NGET'd/4'1
nums←⍎0⊃d ⍝ first line are the numbers
starts←0=≢¨d ⍝ locate empty lines
bingo←{↑⍎¨1↓⍵}¨starts⊂d ⍝ partition-enclose based on empty lines, convert to int and mix
@xpqz
xpqz / verbs.md
Last active December 2, 2021 16:44

Built-in verbs

K (depending on generation) has around 20 built-in verbs. A complication is that they typically have multiple meanings, depending on both context and arity. Indeed, a significant part of the effort required to learn to read k is to be able to recognise the overloaded meanings of verbs and adverbs depending on context.

What is a verb? In other languages, much of the functionality that hides behind k's verbs (and adverbs) tend to be functions residing in various libraries. Indeed, in Python you'd probably need at least half a dozen import statements to match k's built-in verbs and adverbs. It follows that instead of thinking of k as a language with no libraries, you can think of it as a tiny language providing an extremely compact way of accessing a very complete standard library.

ngn/k has 22 built-in verbs: : + - * % ! &amp; | &lt; &gt; = ~ , ^ # _ $ ? @ . 0: 1:, most of which have both monadic and dyadic versions, and occasionally different meanings depending on the types of arguments. Thi

@xpqz
xpqz / aoc21.apl
Last active March 20, 2022 04:36
AoC 21, Dyalog APL
⎕FR ⎕PP ⎕IO←1287 34 0
'iotag' 'cmat' 'foldl'⎕CY'dfns'
⍝ https://adventofcode.com/2021/day/1
1⊥2<⌿d←⍎¨⊃⎕NGET'../d/1'1⋄(¯3↓d)+.<3↓d ⍝ Note: a+b+c<b+c+d is equivalent to a<d
⍝ https://adventofcode.com/2021/day/2
d←↑' '(≠⊆⊢)¨⊃⎕NGET'../d/2'1
(fw dn up)←+/m←('forward' 'down' 'up'∘.≡⊣/d)×⍤1⍎¨⊢/d
fw×dn-up
@xpqz
xpqz / aoc21.py
Last active December 2, 2021 10:30
AoC 21, Python
import numpy as np
from numpy.lib.stride_tricks import sliding_window_view # numpy.__version__ >= 1.20
def ints(name):
with open(name, 'r') as fh:
s = fh.readlines()
return list(map(int, s))
def tok(name, sep=" "):
with open(name, 'r') as fh:
@xpqz
xpqz / aoc21.k
Last active December 2, 2021 09:27
Advent of Code 2021, ngn/k
/ day 1
d:`I$0:"../d/1"
+/>':d
+/(-3_d)<3_d / no need to sum the 3-window
/ day 2
d:" "\'d:0:"../d/2"
mag:`I$d[;1];dir:d[;0]
m:(("forward";"down";"up")~/:\:dir)*\:mag
s:+/'m;s[0]*s[1]-s[2]
@xpqz
xpqz / kembed.c
Created November 29, 2021 11:21
Embed ngn/k or ktye/k in c
/*
This is https://codeberg.org/ngn/k/src/branch/master/x/embed/a.c
with some added commentary.
See: https://github.com/ktye/i/blob/master/kc
*/
#include<stdio.h>
#include"../../k.h"