Skip to content

Instantly share code, notes, and snippets.

@abeland
abeland / .bash_profile
Last active September 5, 2017 14:53
My bash profile (changes often)
export EDITOR='emacs'
# meta
alias eb='emacs ~/.bash_profile'
alias sb='source ~/.bash_profile'
# nav
alias ..='cd ..'
alias ~='cd ~'
alias ls='ls -G'
@abeland
abeland / hmac_example.rb
Created May 31, 2018 17:12
HMAC something in Ruby/Rails
# ruby 2.5.0, rails 5.1.4
key = SecureRandom.hex(32) # Need 256 bits = 32 bytes of entropy. Since in hex it will be 64 characters long.
data = 'example data that I want to hmac could be json string of a big Hash for example'
hmac_bytes = OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), key, data) # raw bytes (in string representation)
hmac_base64 = Base64.urlsafe_encode64(hmac_bytes) # often sending in a link so I use urlsafe version
bar
import re
from typing import List, Tuple
def get_lines() -> List[str]:
with open('input.txt', 'r') as f:
return f.readlines()
def parse_stacks() -> List[List[str]]:
@abeland
abeland / day6.py
Last active December 6, 2022 05:27
from collections import deque
from input import INPUT
def _detect_start_of_msg(s: str, n: int) -> int:
d = deque(s[0:n])
i = n
while len(set(d)) < n:
d.popleft()
d.append(s[i])
from collections import deque
from dataclasses import dataclass
import re
from typing import List, Optional, Tuple
FILE_METADATA_PATTERN = re.compile('^(?P<filesize>\d+)\s+(?P<filename>.+)')
DIR_METADATA_PATTERN = re.compile('^dir\s+(?P<dirname>.+)')
@dataclass
FILE = 'input.txt'
DIRECTION_TO_DELTA = {
'L': (-1, 0),
'R': (1, 0),
'U': (0, 1),
'D': (0, -1),
}
def parse_moves():