Skip to content

Instantly share code, notes, and snippets.

View mattboehm's full-sized avatar

Matthew Boehm mattboehm

View GitHub Profile
@mattboehm
mattboehm / tetris_dense.py
Created April 13, 2014 16:59
Pycon 2014 Thumbtack solution ("dense" version)
import sys, re #thumbtack pycon tetris challenge. "dense" solution by @mattboehm
def shift_down(col):#shift piece down 1 space
loc = "".join(col[::-1])
space = loc.find("#") - 1
return col if loc == -2 else list(loc[:space] + loc[space+1:] + ".")[::-1]
def top_row_blank(board):#check if the top row has only .
return all(tile == "." for tile in board[0])
def find_piece(board):#Replace the X's for the piece with #
board, marked_board = board[:], []
while top_row_blank(board):#skip top blank rows
function! SplitRegex()
    tabnew
    setlocal buftype=nofile bufhidden=hide noswapfile
    let numlines=len(@")
    execute "normal! V".numlines."pggjl"
    let tmp=@z
    let @z="hv0r wlv$r j"
    execute "normal! ".(numlines-2)."@z"
    let @z=tmp
    execute "normal! gglv$r\<space>"
import itertools
#ugly one-liner
def _ugly_split_at(pred, seq):
return list(itertools.ifilter(
None,
[
(
(seq[idx:idx+1] + list(itertools.takewhile(lambda x: not pred(x), seq[idx+1:])))
if idx == 0 or pred(el)
else []
@mattboehm
mattboehm / gist:4234781
Created December 7, 2012 17:14
Count method/property usage in vim
"Copy all matches to a vim search and paste in new buffer separated by lines
"Source this file to try it out.
"Let's say you have a python file and want to find all the methods/properties
"on self mentioned in a file (commence esoteric example):
"class Student(object):
" def __init__(self, name):
" self.name = name
" def set_gpa(self, gpa):
@mattboehm
mattboehm / merge.vim
Created December 12, 2012 21:19
Crappy Vim "mail merge"
"I recorded a macro of the command. These are the keystrokes I hit to do one line
"To do all lines, I recorded the macro in register q (starting on the first line of names.csv)
"then did 2@q to repeat macro twice more.
"This assumes the template is in the same dir and is very brittle
"You should use a templating engine like genshi for non-trivial templating, but just wanted to show that this is possible
"Creates $email.html file for each email address.
0"ayt,f,l"by$:new^M;read template.html^M:%s;\$EMAIL;^Ra;g^M:s;$NAME;^Rb;g^M:w! ^Ra.html^M:bd^Mj
#!/usr/bin/env bash
if [ "$#" -ne 2 ]
then
echo "move a directory to a new location.
While normally, git mv old-dir new-dir should work, let's say you do that and then try to merge in another branch where someone's added new files to old-dir.
these files will stay in old-dir, and we need a way to merge their (arbitrarily deeply nested) contents into new-dir. This script uses cp -r to merge the contents,
followed by git add/rm. When you do git status, it should now show that all those files have been moved.
Warning: script will add the whole target directory, including uncommitted changes on files that existed before it ran."
echo "usage: $0 <old-dir> <new-dir>"
@mattboehm
mattboehm / marks.vim
Created March 15, 2013 01:35
Notes from my February NoVA Vim presentation on marks, jumps and registers. Mostly just a list of suggested :help sections to read.
"These lines map a hotkey that allow you to easily execute the current line or
"selection by hitting F9. It's a cheesy hack that made demoing while reading
"through this more simple.
"
"This is also why commands start with colons. Normally colons would be
"unnecessary if trying to execute these in your vimrc.
:nnoremap <F9> "ayy@a
:vnoremap <F9> "ay@a
"Marks {{{
@mattboehm
mattboehm / gist:7456743
Last active December 28, 2015 06:28
Tmux aliases
#tl: list sessions
alias tl='tmux ls'
#tn <name>: create a session named <name>
alias tn='tmux -2 new -s'
#ta <name>: attach to a session named <name>
alias ta='tmux -2 attach -t'
//advent of code 2016 part 2; will give wrong answer if x/y/distance don't fit in a signed byte
// {x} {y} {tmp0=0} {tmp1=0} {dir = 0 to 3} {num}
// ^
//{tmp1}
, //read L/R
[
---------------------------------------------------------------------------- //subtract 76 to make L 0
[ //if it's not 0 (It was an R)
> //{tmp2(0)}
++ //{add 2 to dir}
//Please excuse the horrible comments. Maybe when my head works again I'll clean this up. Tested at https://sange.fi/esoteric/brainfuck/impl/interp/i.html
// {x} {y} {tmp0=0} {tmp1=0} {dir = 0 to 3} {num}
// ^
//{tmp1}
, //read L/R
[
---------------------------------------------------------------------------- //subtract 76 to make L 0
[ //if it's not 0 (It was an R)
> //{tmp2(0)}