Skip to content

Instantly share code, notes, and snippets.

View jan-g's full-sized avatar

jan grant jan-g

  • UTC
View GitHub Profile
@jan-g
jan-g / v1.py
Last active February 5, 2020 14:56
travesty, naive version and then an approach undreamt of back in 1984
#!/usr/bin/env python3
# Invoke with
# ./v1.py {source-file} {n} {m}
# where {n} is the size of the n-gram used (3 for trigrams)
# {m} is the amount of output text required.
from collections import Counter
import os
from random import choice
This is an image!
# IMAGE: foo.jpg
+ restart?
# RESTART
-> END
@jan-g
jan-g / NOTE
Created October 21, 2022 08:41
Sed doesn't do lookbehind/lookahead.
This works because the tests don't try foo-7abcdefghijklmnopqrstuvwxyz; using capture groups around the expression
and backrefs in the replacement doesn't work if you have two bogusCoin addresses separated by a single space.
Replacing the implementation of munge with some other stdio pipe that does the business is left as an exercise to the reader.
socat TCP-LISTEN:12345,fork,reuseaddr SYSTEM:"cat -u"
@jan-g
jan-g / NOTE
Created October 21, 2022 10:23
Protohackers primality
This has a performance problem with the multiclient tests on the host I run it on.
I initially used an alternative jq-only naive primality test - this was too slow as well, so I dropped into openssl's checker.
```
# replace from `number=`
<<<"$req" jq -c '
def isInteger:
(. | type == "number") and
(. | floor == .)
;
This is a bit ugly.
We need to ensure that socat doesn't discard zero-size payloads (because they're a request for the value assigned to the
null identifier.)
We use a single standalone single-threaded process to serve up the persistent DB.
We can't shutdown sockets, which means that we can't just `cat` content from the `downstream` packet handler into a receiving
`cat` in the zdb; this'll hang because nothing is closing the TCP connection to it. If we close the outgoing connection, the
read connection (that associates a request to a response) gets closed too.
@jan-g
jan-g / day5a.hs
Created December 5, 2022 13:33
AoC 2022 day 5 part 1
type Column = Int
type Crate = Char
type Stacks = Map.Map Column [Crate]
data Move = Move Int Column Column deriving (Show, Eq)
parse :: [String] -> (Stacks, [Move])
parse ls =
let
[stackDesc, moveDesc] = ls & splitOn [""]
stacks = foldl' stackLine Map.empty stackDesc
@jan-g
jan-g / NOTES
Created December 9, 2022 13:35
canonicalise cyphers
For simplicity, the cypher spec uses a pair of bytes for each entry: opcode, and (optional) argument