Skip to content

Instantly share code, notes, and snippets.

@bsidhom
bsidhom / extract-gemcraft-levels.js
Last active January 7, 2024 00:34
Extract Gemcraft: Chasing Shadows level metadata
// Extract level metadata from https://gemcraft.fandom.com/wiki/GemCraft_Chapter_2:_Chasing_Shadow_Fields
let extractLevels = () => {
const extractGemTypes = (cell) => {
const anchors = [...cell.querySelectorAll("a")];
return anchors.map((a) => a.title);
};
const extractText = (cell) => {
const s = cell.innerText.trim();
if (s.length == 0) {
@bsidhom
bsidhom / partitions.py
Last active May 20, 2024 23:04
Generate integer partitions and compositions
#!/usr/bin/env python3
def main():
print("partitions of 5:")
for p in partitions(5):
print(p)
print()
print("compositions of 5:")
for c in compositions(5):
@bsidhom
bsidhom / babynames.py
Created November 4, 2023 06:50
Merge SSA baby names dataset into a flat CSV
#!/usr/bin/env python3
# Baby names zip can be downloaded from https://www.ssa.gov/oact/babynames/names.zip
import argparse
import csv
import io
import re
import sys
import zipfile
@bsidhom
bsidhom / stolen_rubies.py
Created October 29, 2023 04:32
Stolen Rubies riddle
#!/usr/bin/env python3
# Solves the stolen rubies riddle: https://www.youtube.com/watch?v=2QJ2L2ip32w
import itertools
def main():
configs = unique_configs(gen_chests())
# For each "reasonable" strategy (possibly assigning a different number to
@bsidhom
bsidhom / banana_camel_puzzle.py
Created October 28, 2023 00:43
Banana Camel puzzle
#!/usr/bin/env python3
# Approximately solves the camel/bananas problem from
# https://www.youtube.com/watch?v=pDoar4zh5tI.
# NOTE: I really enjoyed going back to this from a fresh perspective as
# I first saw it many years ago as a problem in a math class.
# The approach here is to _assume_ you are moving bananas in segments of a fixed
# length, d, and then ask how many bananas you can move in total with segments
@bsidhom
bsidhom / one_hundred_switches_one_hundred_lights.py
Created October 28, 2023 00:30
100 switches/100 lights puzzle
#!/usr/bin/env python3
# Solves https://www.youtube.com/watch?v=ltsx5389iEs
# The idea is to identify all lights in ⌈log2(n)⌉ moves by assigning each bulb
# an integer and using the unique bit representation of each as the sequence of
# light switches for which it was powered on (1 means it was on for a given
# trial, 0 means it was off).
# NOTE: The bit representation + transpose operation makes the logic most clear,
@bsidhom
bsidhom / giant_cat_army.py
Created October 28, 2023 00:21
Giant Cat Army riddle
#!/usr/bin/env python3
# Enumerate all solutions to the TED-Ed riddle
# https://www.youtube.com/watch?v=YeMVoJKn1Tg
def main():
target = [0, 2, 10, 14]
count = 0
unique_code_sets = set()
@bsidhom
bsidhom / penniless_pilgrim.py
Created October 28, 2023 00:18
Penniless Pilgrim riddle
#!/usr/bin/env python3
# Solves the riddle from https://www.youtube.com/watch?v=6sBB-gRhfjE
from __future__ import annotations
from dataclasses import dataclass
from functools import total_ordering
# "Cheating royal riddle" from https://www.youtube.com/watch?v=hk9c7sJ08Bg
# Die designations.
x <- c(2, 7, 7, 12, 12, 17)
y <- c(3, 8, 8, 13, 13, 18)
# Tabulate the outcomes to get counts per face per die. Note that tabulate
# discards non-positive values, but this is fine since all of the values of
# interest are positive.
tx <- tabulate(x)
@bsidhom
bsidhom / Package.swift
Last active June 25, 2023 06:51
Capture a photo every second and turn the input into a 30 fps h.264 video.
// swift-tools-version: 5.8
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "WebcamCapture",
platforms: [
.macOS(.v10_15)
],