Skip to content

Instantly share code, notes, and snippets.

Darius Bacon darius

Block or report user

Report or block darius

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View domification.py
# See https://gist.github.com/kragen/6bee5b4529cbde133050e9dc2e0b4b7e
from parson import Grammar
g = Grammar(r"""
_: {' ' | '\n' | '\t'}*.
digit: '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'.
atom: :'<num>' {digit+ ('.' digit* | ) | '.' digit+} :'</num>' _ | {'('} _ a {')'} _.
e: :'<exp> <base>' atom :'</base>' {'**'} _ :'<pow>' atom :'</pow> </exp>' | atom.
m: :'<term>' e (:'<op>' {'/' | '*' | '%'} :'</op>' _ e)+ :'</term>' | e.
@darius
darius / aoc24.scm
Created Dec 25, 2018
advent of code 2018 day 24
View aoc24.scm
;; fails on the real input
(import (use "advent-utils")
simple-parser<-
grammar<- parson-parse feed)
(import (use "lib/pretty-print")
pp)
(import (use "lib/sort")
sort-by-key)
View aoc22.scm
;; Simple least-cost search was too slow.
(import (use "advent-utils")
simple-parser<- product<-)
(import (use "lib/memoize")
memoize)
(import (use "lib/pairing-heap")
priority-queues<-)
(let input (with-input-file '.read-all "advent22"))
@darius
darius / anotherparsonexample.py
Last active Jul 14, 2018
another parson example
View anotherparsonexample.py
from parson import Grammar
g = r"""
_: /\s*/.
ns: /[0-9]+/.
num: {ns ('.' ns | '.') | '.' ns | ns} _ .
e: num {'**'} _ num :to_prefix | num.
m: e {'/'|'*'} _ m :to_prefix | e.
a: m {'+'|'-'} _ a :to_prefix | m.
"""
View unmashing.py
from __future__ import division
# layout: group keys that are typed by the same finger.
dvorak_layout = '`1!\'a; 2@,oq 3#ej $4puk%5yix ^6fdb&7ghm *8ctw (9rnv )0lsz[{]}/?+=|\\-_'.split()
qwerty_layout = '''`~!1qaz @2wsx #3edc $4rfv%5tgb ^6yhn&7ujm *8ik,< (9ol.> )0p;:?/-_[{'"=+]}|\\'''.split()
layout = qwerty_layout #dvorak_layout
finger = {ch: i
for i, chars in enumerate(layout)
for ch in chars}
View bummed_segment2.py
from __future__ import division
from collections import defaultdict
# ... rest of original code from Norvig, ngrams.py ...
class Pdist(dict):
"A probability distribution estimated from counts in datafile."
def __init__(self, data=[], N=None, missingfn=None):
for key,count in data:
self[key] = self.get(key, 0) + int(count)
View data_defs.py
# h/t Dave Long
from collections import namedtuple
data = lambda s: {ws[0]: namedtuple(ws[0], ws[1:])
for term in s.split('|') for ws in [term.split()]}
Data = lambda s: globals().update(data(s))
Data("Nil | Cons head tail")
View leetcode_decode.py
"""
https://leetcode.com/problems/decode-string/
https://github.com/darius/parson
"""
from parson import Grammar
decode = Grammar(r""" seq :end.
seq: part* :join.
part: /(\d+)/ '[' seq ']' :repeat
View gist:16bf45a4f25343484e775a5735e7a5a6
;; Max path sum in a binary tree.
;; tree ::= {leaf} | {branch value tree tree}
;; Coded in https://github.com/darius/squeam
(define (max-path-sum tree)
(let (best best-to-root) (solve tree))
best)
(define (solve tree)
(match tree
View sort_of_overboard.hs
-- Like nfa.hs, but with numbered states, so that we can compare them
-- by index, to deduplicate them without looping forever.
-- (It does still loop on many (many (lit 'a')).)
-- Thanks to Libby Horacek for a bunch of help,
-- and Tom Murphy for pointing out Data.Vector.
import Data.List (nub)
import qualified Data.Vector as V
matches re cs = 0 `elem` foldl step starts cs
You can’t perform that action at this time.