Skip to content

Instantly share code, notes, and snippets.

View ryangraham's full-sized avatar
😝

Ryan Graham ryangraham

😝
View GitHub Profile
cat ~/.tmux.conf
# Enable mouse mode
set -g mouse on
# Create 2x2 grid layout on new sessions
set-hook -g after-new-session {
split-window -h
split-window -v
select-pane -t 0
split-window -v
@ryangraham
ryangraham / aws-apigw-delete-base-path-mapping.sh
Created February 12, 2021 21:14 — forked from mikaelvesavuori/aws-apigw-delete-base-path-mapping.sh
AWS API Gateway: Delete base path mapping
aws apigateway delete-base-path-mapping --domain-name domain.tld --base-path "(none)"
# Reference: https://docs.aws.amazon.com/cli/latest/reference/apigateway/delete-base-path-mapping.html
@ryangraham
ryangraham / ecr_purge.py
Created February 12, 2021 19:34
Purge all images from an ECR repository so it can be deleted cleanly
#!/usr/bin/env python3
import argparse
import boto3
def get_account_id():
sts = boto3.client('sts')
identity = sts.get_caller_identity()
account_id = identity["Account"]
return account_id
@ryangraham
ryangraham / purge_bucket.py
Last active February 11, 2021 00:12
Purge all object versions from a bucket so terraform can destroy it
#!/usr/bin/env python3
import argparse
import boto3
def main(bucket_name):
session = boto3.Session()
s3 = session.resource(service_name='s3')
bucket = s3.Bucket(bucket_name)
bucket.object_versions.delete()
@ryangraham
ryangraham / print_tree.c
Created May 25, 2020 03:32 — forked from ximik777/print_tree.c
Printing Binary Trees in Ascii
/*
Copy from: http://web.archive.org/web/20090617110918/http://www.openasthra.com/c-tidbits/printing-binary-trees-in-ascii/
Source: http://web.archive.org/web/20071224095835/http://www.openasthra.com:80/wp-content/uploads/2007/12/binary_trees1.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ryangraham
ryangraham / luhn.cc
Created May 21, 2020 03:56
luhn range-v3
bool luhn_check(const std::string& pan) {
auto to_int = [](char c) { return c - 48; };
auto sum_digits = [](int n) { return n / 10 + n % 10; };
auto do_digits = [&sum_digits](auto&& values) {
auto const& [index, digit] = values;
return (index % 2 == 0) ? digit : sum_digits(digit * 2);
};
auto rng = pan
@ryangraham
ryangraham / rtn_checksum.cc
Created May 20, 2020 06:51
rtn checksum in c++
bool rtn_checksum(const std::string& routing_number) {
std::vector<int> const multipliers{3, 7, 1, 3, 7, 1, 3, 7, 1};
auto to_int = [](char c) { return std::stoul(&c, nullptr, 10); };
auto const routing_digits = routing_number | views::transform(to_int);
auto multiply = [](auto a, auto b) { return a * b; };
auto sum =
accumulate(views::zip_with(multiply, routing_digits, multipliers), 0);
@ryangraham
ryangraham / rtn_checksum.ex
Created May 20, 2020 06:19
rtn checksum in elixir
"325081403"
|> String.graphemes() # => ["3", "2", "5", "0", "8", "1", "4", "0", "3"]
|> Stream.map(&String.to_integer/1) # => [3, 2, 5, 0, 8, 1, 4, 0, 3]
|> Stream.zip([3, 7, 1, 3, 7, 1, 3, 7, 1]) # => [{3, 3}, {2, 7}, {5, 1}, {0, 3}, {8, 7}, {1, 1}, {4, 3}, {0, 7}, {3, 1}]
|> Stream.map(fn {a, b} -> a * b end) # => [9, 14, 5, 0, 56, 1, 12, 0, 3]
|> Enum.sum() # => 100
|> rem(10) == 0 # => true
@ryangraham
ryangraham / main.cpp
Created May 10, 2020 03:46
masked password prompt
#include <iostream>
#include <string>
#include <termios.h>
#include <unistd.h>
int password_prompt(std::string &password)
{
struct termios tty;
// Get current terminal settings
// track2 decode
std::string decoded = "";
uint8_t left = 0;
uint8_t right = 0;
for (uint8_t c : encoded)
{
left = c >> 4;
right = c & 0b00001111;
decoded.append(1, left + 0x30);
decoded.append(1, right + 0x30);