Skip to content

Instantly share code, notes, and snippets.

View twopoint718's full-sized avatar
yo

Christopher Wilson twopoint718

yo
View GitHub Profile
@twopoint718
twopoint718 / calbot.rb
Last active May 2, 2024 22:18
CalBot schedule your book reading for the year. Produces an iCal file for importing into your reading calendar.
#!/usr/bin/env ruby
require 'date'
require 'securerandom'
require 'optparse'
def header()
<<~EOS
BEGIN:VCALENDAR
CALSCALE:GREGORIAN
PRODID:-//SENCJW Heavy Industries, Ltd.//CalBot//EN
@twopoint718
twopoint718 / osm.html
Created March 29, 2024 22:18
Short example of using OpenStreetMap with Leaflet.js
<!doctype html>
<html lang="en-US">
<head>
<meta charset="utf-8" />
<title>OpenStreetMap with Leaflet.js</title>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css"
integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY="
crossorigin=""/>
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"
integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo="
@twopoint718
twopoint718 / custom_types.sql
Created August 22, 2023 20:30
List custom types along with their enum values
-- list types and their enum values
SELECT
pg_type.typname AS enumtype,
array_agg(pg_enum.enumlabel) AS enumlabels
FROM
pg_type
JOIN pg_enum ON pg_enum.enumtypid = pg_type.oid
GROUP BY pg_type.typname;
@twopoint718
twopoint718 / app.ru
Created June 9, 2023 21:28
Single-file Rails application
# Run with:
# $ rackup app.ru
require 'bundler/inline'
gemfile(true) do
source 'https://rubygems.org'
gem 'rails', '~> 7.0.5'
gem 'puma'
end
@twopoint718
twopoint718 / dc_cheatsheet.md
Last active April 17, 2023 03:40
UNIX `dc` cheatsheet

DC Cheatsheet

Mathematical operators

Operator Stack effect notes
+ a b -- $a+b$
- a b -- $a-b$
* a b -- $a*b$
/ a b -- $\frac{a}{b}$
% a b -- $mod(a,b)$
@twopoint718
twopoint718 / cjw_grep.el
Created March 16, 2023 21:47
Wrapper for Emacs' default grep. It uses `git grep`, passes in the git project directory, and also uses the current symbol under cursor as a default.
(defun cjw-git-grep (my-word)
"Use git-grep for search in current project (vc-root-dir). Default
search term uses symbol under cursor"
(interactive
(list
(read-string
(format "Search (%s): " (thing-at-point 'symbol)) ; prompt
nil ; initial input
nil ; history list
(thing-at-point 'symbol) ; evaluated. will be default
@twopoint718
twopoint718 / easter.rb
Created February 1, 2023 18:48
Calculate the date of Easter
def computus(year)
a = year % 19 # year in the 19-year metonic cycle
k = year / 100 # century index
p = (13 + 8*k) / 25 # shift of metonic cycle, add a day offset every 300 years
q = k / 4 # correction for non-observed leap days
m = (15 - p + k - q) % 30 # correction to starting point each century
d = (19*a + m) % 30 # num of days from 3/21 until full moon
n = (4 + k - q) % 7 # find next sunday; century-based offset in weekly calc.
b = year % 4 # correct for leap days
c = year % 7 # correct for leap days
@twopoint718
twopoint718 / jwt.rb
Last active December 19, 2022 20:46
Build a JWT on the command line
#! /usr/bin/env ruby
require 'optparse'
require 'base64'
require 'json'
require 'openssl'
require 'set'
options = {}
OptionParser.new do |parser|
@twopoint718
twopoint718 / formatter.rb
Created December 6, 2022 20:51
Advent of Code 2022 Day 4 in SQL (run with `\I program_04.sql` from psql console)
#! /usr/bin/env ruby
# Just reformats the input into Postgres inclusive range syntax in CSV:
# 2-4,6-8 => "[2,4]","[6,8]"
File.readlines('input_04.txt').each do |line|
r1, r2 = line.split(',')
a, b = r1.split('-')
x, y = r2.chomp.split('-')
puts "\"[#{a},#{b}]\",\"[#{x},#{y}]\""
end
@twopoint718
twopoint718 / program_03.sql
Created December 6, 2022 00:19
Advent of Code 2022 Day 3 with some SQL + a little PL/pgSQL
drop table if exists rucksacks;
create table rucksacks (
id serial primary key,
content text
);
copy rucksacks (content) from '/Users/cjw/Documents/aoc22/input_03.txt'
with csv;
create or replace function letter_value(letter char) returns integer as $$