Skip to content

Instantly share code, notes, and snippets.

@Westacular
Westacular / AoC-2021-Day21-Part2.swift
Last active December 21, 2021 15:47
Solution for Part 2 of https://adventofcode.com/2021/day/21 ... Runs in 0.0004 seconds on M1 MacBook Pro
import Foundation
/// Typealias used in case we need to use some BigInt type
/// for counting, but for games to 21, a 64-bit Int is sufficient
typealias CountingInt = Int
struct DiracDie {
/// index is sum of three rolls of 3-sided die,
/// value is the number of combinations that generate that sum
static let threeRollCombinations: [Int] = {
@Westacular
Westacular / VarianceTestCasting.swift
Created December 11, 2017 18:04
Testing the covariance/contravariance behaviours of casting function types, both directly and in a generic context. There are several inconsistencies.
protocol Foo { }
protocol Bar { }
protocol Baz { }
struct FooStruct: Foo, Bar {}
/// Test covariance of return type
let block: () -> FooStruct = { FooStruct() }
@Westacular
Westacular / birthday_second_counter.py
Last active May 22, 2016 19:02
A tool to calculate your age in seconds, and show a countdown to a target age (e.g., 1 billion seconds)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import argparse
import traceback
import dateutil.parser
import dateutil.tz
from datetime import datetime, timedelta
@Westacular
Westacular / join_lines_alt.py
Created December 2, 2012 05:36
An alternative to Sublime Text 2's built-in join_lines command, which contains an annoying ideosyncracy of reaching outside the selected region when performing a join.
# -*- coding: UTF-8 -*-
import sublime_plugin
# An alternative to the builtin "join_lines" command,
# which contains an idiosyncracy that I consider a bug.
class JoinLinesAltCommand(sublime_plugin.TextCommand):
@Westacular
Westacular / gistcheck.py
Created November 25, 2012 21:42 — forked from michaeluhl/gistcheck.py
An all-purpose tool for pulling from, committing to, and downloading new gists in Pythonista.
# Source: https://gist.github.com/4145515
#
# All-purpose gist tool for Pythonista.
#
# When run directly, this script sets up four other scripts that call various
# functions within this file. Each of these sub-scripts are meant for use as
# action menu items. They are:
#
# Set Gist ID.py - Set the gist id that the current file should be
# associated with.
@Westacular
Westacular / WakeOnLan.py
Created November 11, 2012 23:52
Sends a wake-on-lan magic packet for the given MAC address to either the broadcast address of the local subnet, or, if provided, some other hostname. An alternate port can also be optionally provided.
# WakeOnLan
#
# Sends a wake-on-lan magic packet for the given MAC address
# to either the broadcast address of the local subnet, or,
# if provided, some other hostname.
# An alternate port can also be optionally provided.
import struct, socket
import sys
@Westacular
Westacular / markdown_reference_completions.py
Created September 4, 2012 18:50
Sublime autocomplete plugin for Markdown reference links
# -*- coding: UTF-8 -*-
import sublime, sublime_plugin
import re
# Utility function for shortening text.
# From: http://www.leancrew.com/all-this/2012/08/more-markdown-reference-links-in-bbedit/
def shorten(str, n):
'Truncate str to no more than n chars'
return str if len(str) <= n else str[:n-1] + u'…'