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 / day9.apl
Last active December 9, 2022 17:56
day9
⍝ Read data, separate into vectors for direction and magnitude
d←⍉↑' '(≠⊆⊢)¨⊃⎕nget'd/9'1
mag←⍎¨1⌷d⋄dir←'URDL'⍳∊0⌷d
⍝ Convert to complex offsets of the magnitude in specified direction
moves←1 0J1 ¯1 0J¯1[dir]×mag
⍝ Expand the 'gaps' -- 0 4 means 0 1 2 3 4 etc. This gives the path of
⍝ the head knot
@xpqz
xpqz / OwidCovidData.aplf
Created May 17, 2022 14:11
Modifed version of Nic's OWID plot
{sp}←OwidCovidData;Causeway;InitCauseway;View;countries_to_plot;csv;data;date;dates;field;fields_to_plot;location;locations;miss;row;sp;values
miss ← ¯1E300 ⍝ missing value
csv ← {⎕CSV ⍵ ⍬ 4} 'covid_subset2.csv'
dates ← {⍵[⍋⍵]}∪date ← 20 1⎕DT csv[;2]
csv[;2] ← date
locations ← ∪location←csv[;1]
row ← csv[;1 2]⍳↑locations∘.{⍺ ⍵}dates ⍝ row for each [location;date]
csv ⍪← (⊂'')(⊂'')miss miss
data ← csv[row;3 4] ⍝ data[location;date;fields]
[W 09:27:38.093 NotebookApp] Notebook learnapl/contents/trees.ipynb is not trusted
ERROR:asyncio:Exception in callback <TaskWakeupMethWrapper object at 0x7fba843b3fa0>(<Future finis...C: 1\r\n\r\n'>)
handle: <Handle <TaskWakeupMethWrapper object at 0x7fba843b3fa0>(<Future finis...C: 1\r\n\r\n'>)>
Traceback (most recent call last):
File "/opt/anaconda3/envs/py39/lib/python3.9/asyncio/events.py", line 80, in _run
self._context.run(self._callback, *self._args)
RuntimeError: Cannot enter into task <Task pending name='Task-79' coro=<HTTP1ServerConnection._server_request_loop() running at /opt/anaconda3/envs/py39/lib/python3.9/site-packages/tornado/http1connection.py:823> wait_for=<Future finished result=b'GET /api/co...PC: 1\r\n\r\n'> cb=[IOLoop.add_future.<locals>.<lambda>() at /opt/anaconda3/envs/py39/lib/python3.9/site-packages/tornado/ioloop.py:688]> while another task <Task pending name='Task-69' coro=<MappingKernelManager.start_kernel() running at /opt/anaconda3/envs/py39/lib/python3.9/site-packages/no
Run make k-libc
/Applications/Xcode_13.2.1.app/Contents/Developer/usr/bin/make a N=k-libc R=k O='-O3 -march=native -Dlibc' L='-lm' STRIP=true
mkdir -p o/k-libc;cc @opts -O3 -march=native -Dlibc -o o/k-libc/0.o -c 0.c
mkdir -p o/k-libc;cc @opts -O3 -march=native -Dlibc -o o/k-libc/1.o -c 1.c
mkdir -p o/k-libc;cc @opts -O3 -march=native -Dlibc -o o/k-libc/2.o -c 2.c
mkdir -p o/k-libc;cc @opts -O3 -march=native -Dlibc -o o/k-libc/3.o -c 3.c
mkdir -p o/k-libc;cc @opts -O3 -march=native -Dlibc -o o/k-libc/a.o -c a.c
mkdir -p o/k-libc;cc @opts -O3 -march=native -Dlibc -o o/k-libc/b.o -c b.c
mkdir -p o/k-libc;cc @opts -O3 -march=native -Dlibc -o o/k-libc/e.o -c e.c
mkdir -p o/k-libc;cc @opts -O3 -march=native -Dlibc -o o/k-libc/f.o -c f.c
@xpqz
xpqz / wordle.apl
Last active February 8, 2022 12:55
Wordle helper in Dyalog APL
⍝ Function to say if a given word contains every letter in a set
wordle←∧/∊
⍝ Load dictionary, find 5-letter words, downcase
flw←⎕C words/⍨5=≢¨words←⊃⎕NGET'/usr/share/dict/words'1
⍝ All 5-letter words containing the letters 'reld'
↑flw/⍨'reld'∘wordle¨flw
alder
@xpqz
xpqz / manacher.apl
Last active February 5, 2022 20:02
Manacher's algorithm for finding the longest palindromic substring in Dyalog APL (⎕IO←0)
∇ res←manacher str;S;c;r;radii;i;ir
c r radii←0 0 (0↑⍨≢S←'|',∊'|',⍨⍪str)
:for i :in ⍳≢S
radii[i]←{r>i:radii[(2×c)-i]⌊r-i⋄0}⍬
:trap 3 ⍝ Catch index errors if the below goes out of bounds
:while =/S[i(+,-)1+i⊃radii]
radii[i]+←1
:endwhile
:endtrap
@xpqz
xpqz / search.md
Last active September 3, 2022 23:31
Linear vs binary search in Dyalog APL

The following question was asked on the APL Orchard chatroom (https://chat.stackexchange.com/transcript/message/59923809#5992380):

so I was reading https://xpqz.github.io/learnapl/iteration.html a bit ago and was confused by the binary search example, so wanted to test. the numbers I'm seeing for linear search are both nothing like his and don't make a lot of sense. I did randInts ← 100000 ? 100000 and compared execution times of randInts ⍳ 1, 19326, and 46729. the return values of those are 94438, 1001, and 1 respectively. the execution times are 0.13ms, 0.47ms, and 0.12us respectively. can anyone explain what's happening?

Obviously, we can't recreate the randInts exactly, but let's run a few timed iterations:

@xpqz
xpqz / day15.apl
Created December 15, 2021 13:14
AoC 2021, day 15, Dyalog APL
⎕IO←0
'wpath'⎕cy'dfns'
d←↑⍎¨¨⊃⎕NGET'../d/15'1
shifts←{(⍺,2⊣/⍵)(2⊢/⍵,⍺)(2⊢⌿⍵⍪⍺)(⍺⍪2⊣⌿⍵)}
day15←{
nnnn←{⍵~¯1}¨,↓2 0 1⍉↑¯1shifts(⍴⍵)⍴(⍳×/⍴⍵)
flat←∊⍵
values←{flat[⍵]}¨nnnn
graph←↑(nnnn)(values)
path←graph wpath 0 (¯1+≢flat)
@xpqz
xpqz / day9.apl
Last active December 10, 2021 17:02
⍝ Blatantly plagiated from @rak1507
data←↑⍎¨¨⊃⎕NGET'../d/9'1
shifts←{(⍺,2⊣/⍵)(2⊢/⍵,⍺)(2⊢⌿⍵⍪⍺)(⍺⍪2⊣⌿⍵)}
lowpoints←data<⊃⌊/10 shifts data
+/1+data[⍸lowpoints] ⍝ part 1
fill←{(×⍵)×⍵⌈⊃⌈/0 shifts ⍵}⍣≡{(2+⍳+/,⍵)@⊢⍵}9≠data
×/3↑{⍵[⍒⍵]}⊢∘≢⌸(∊fill)~0 ⍝ part 2
@xpqz
xpqz / aocd.apl
Created December 8, 2021 16:05
Fetch the data for a given AoC day
aocd←{
(year day)←⍵
cookie←⊃⊃⎕NGET'/Users/stefan/.aoc'1
h←⎕NEW HttpCommand
h.Headers←(1 2⍴'cookie' ('session=',cookie))
h.URL←'https://adventofcode.com/',(⍕year),'/day/',(⍕day),'/input'
¯1↓(h.Run).Data
}