Skip to content

Instantly share code, notes, and snippets.

View d12's full-sized avatar
🐱

Nathaniel Woodthorpe d12

🐱
View GitHub Profile
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCr6VkBkvGW2RrDt0X+6HpEnhpWvKq1nqfczVxb0FXm00l5Fu68+kAmK+QOCHnRBMP9LZU/tsHWxjxiN6tKD1sORn4e72QMvXk53q4VJhOSJLA9yyPsrzCAF1wIHA7yQvm3lxQ23e+Vtzwnl/bjyrxUeRO8H1z2v62pJ6KpX3kdVV0KTZouT3DFiuQmfT+CasCLKCUX4FyUxoT08MwapX5MFVetiRJ8CLKlL6DUBx5HuYUwi9cb8oZIv11ffG3QJDCBhJWrkDDa4RihC01MWztWxiDffpCCOiwwlmOW08PaLxuP922ZSBJwMj34Iu3f8X0HP/qByUdq9DQtFGyp+wC1eG3OAqnZF+oN0BKurzwfYk8DHiUoPernxeoADfJwvsn1q7cQ9nQxVtg9NFP4bq4ZRkWu5aU9GRFVDvDw1NDLTRAbvrHv2L/Jfj0dLlBjhvJww68qKeClKiAKEGSoizsm59tbHBVP8h76k11JoP7o0VSML1FzxuJkoAh8MjrIcz92tHVgX+MI+BYmZLwBxwJOoB0aydpiL1GTU9HBNIY5rPKdoMOC6iWGS737CqPXHL+MACeDBMMbAz3gcGmFynUd8VHOfmX7puR0oOD3XwuymmbwIChYe/UL314Yp9JWtTzAaWQMeLldi3LXcirjp+x00vPGU+gdlYnhC08n5KKUpQ== d12@Nathaniels-MacBook-Pro.local
f_lines = File.read("input.txt").lines.map(&:chomp)
result_lines = f_lines.map do |line|
# Extract all angle brackets, and sort them.
# We use this as a "stack" of angle brackets that we can pull from
# when we need an angle bracket in the final result
angle_brackets = line.scan(/[<>]/).sort
line_chars = line.chars.map do |c|
c == " " ? " " : angle_brackets.shift
end
@d12
d12 / 11.rb
Created January 2, 2022 16:23
# Brute force
# Times out
# O(n^2) time, O(1) space
def max_area(heights)
max = 0
current_left_index = nil
current_right_index = nil
heights.each_with_index do |num1, index1|
next if num1 == 0
# @param {Integer[][]} image
# @param {Integer} sr
# @param {Integer} sc
# @param {Integer} new_color
# @return {Integer[][]}
def flood_fill(image, sr, sc, new_color)
x_max = image.length - 1
y_max = image[0].length - 1
stack = [[sr, sc]]
@d12
d12 / 9.rb
Created October 21, 2021 02:05
Advent 2015 day 9
require "json"
require "byebug"
mapping = {}
list = File.read("9_input.txt").lines.map(&:chomp)
list.each do |l|
a, _, b, _, number = l.split(" ")
mapping[[a, b]] = number.to_i
input = File.read("7_input.txt").gsub("OR", "|").gsub("AND", "&").gsub("LSHIFT", "<<").gsub("RSHIFT", ">>").gsub("NOT", "~").lines.map(&:chomp)
@map = {}
input.each do |expression|
left_side, right_side = expression.split("->")
@map[right_side.strip] = left_side
end
@memo = {}
def evaluate(label)
# Strategy:
# 1. Find location of each number
# 2. For each combination of 2 numbers, find the distance between them in the graph. Use BFS because I'm lazy
# 3. Brute force the shortest path that goes through each of the 7 numbers.
# - For each permutation of 7 numbers that begins with 0, sum up the distances for each hop.
# - E.g. [0, 2, 5, ...] == distance(0, 2) + distance(2, 5) + ...
# - Find min distance among all permutations
require "byebug"
input = File.read("input.txt").lines.map(&:strip).map(&:chars)
@d12
d12 / TakeScreenshot.cs
Created July 19, 2021 18:52
Takes a super high res screenshot in Unity 2019.4 when you press G.
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public static class TakeScreenshot
{
// See http://docs.unity3d.com/ScriptReference/MenuItem.html for docs on [MenuItem] hotkeys
[MenuItem("Helpers/Screenshot _g")]
public static void Screenshot()
@d12
d12 / RenderQueueSorter.cs
Last active April 10, 2021 02:02
When using GPU instancing on a small number of unique meshes, it helps to have all objects with a specific mesh on the same render queue so that GPU instancing can properly batch all objects together. Otherwise, you end up with a semi-shuffled render queue and the GPU instancing algorithm won't batch draws that aren't back-to-back. RenderQueueSo…
public static class RenderQueueSorter
{
private static int nextRenderQueue = 5; // Don't go over 2500! Or we leak into transparent queues
private static Dictionary<Mesh, Material> _meshToMatMap = new Dictionary<Mesh, Material>();
public static Material SortedMaterialFromMesh(Mesh mesh, Material sharedMaterial)
{
if (_meshToMatMap.ContainsKey(mesh)) return _meshToMatMap[mesh];
return GenerateMaterialForMesh(mesh, sharedMaterial);
@d12
d12 / ExceptionManager.cs
Created March 29, 2021 01:26
A simple Unity exception handler that reports errors to a server. Handy for tracking down issues hit by end users.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ExceptionManager : MonoBehaviour
{
private const int ExceptionsPerCooldown = 5;
private const float CooldownTime = 5f;
private int _exceptionsLeftBeforeTimeout = ExceptionsPerCooldown;