Skip to content

Instantly share code, notes, and snippets.

@aglee
aglee / alert-build-succeeded.swift
Created June 23, 2024 23:09
Swift program to display an alert
import Cocoa
func imageFromEmoji(emoji: String, size: CGSize) -> NSImage? {
let image = NSImage(size: size)
image.lockFocus()
let attributes: [NSAttributedString.Key: Any] = [
.font: NSFont.systemFont(ofSize: size.height) // Adjust font size based on image size
]
let attributedString = NSAttributedString(string: emoji, attributes: attributes)
@aglee
aglee / AoC2018Day07.swift
Created December 7, 2018 21:09
Advent of Code Day 07
import Foundation
let USING_TEST_INPUT = false
let commandURL = URL(fileURLWithPath: CommandLine.arguments[0])
let inputFileName = USING_TEST_INPUT ? "test.txt" : "input.txt"
let inputFileURL = commandURL.deletingLastPathComponent().appendingPathComponent(inputFileName)
let inputText = try! String(contentsOf: inputFileURL, encoding: .utf8).trimmingCharacters(in: .whitespacesAndNewlines)
let inputLines = inputText.split(separator: "\n")
@aglee
aglee / main.swift
Last active December 6, 2018 16:36
Advent of Code 2018 day 06
import Foundation
let commandURL = URL(fileURLWithPath: CommandLine.arguments[0])
let inputFileURL = commandURL.deletingLastPathComponent().appendingPathComponent("input.txt")
let inputText = try! String(contentsOf: inputFileURL, encoding: .utf8).trimmingCharacters(in: .whitespacesAndNewlines)
let inputLines = inputText.split(separator: "\n")
// I will call the input coordinates "pins", as in pins on a map (is clearer to my mind than "coordinates").
// I will call the index of a pin in allPins its "pinID".
#import <Foundation/Foundation.h>
// Two things to know about NSSet:
//
// - NSSet assumes its elements will never mutate. It breaks if they do.
// - Unlike NSDictionary, NSSet does not copy its unique objects. It stores
// the original pointers.
//
// Bottom line: DO NOT MUTATE OBJECTS THAT ARE ELEMENTS OF AN NSSET. The
// code below demonstrates the kind of bugs you could run into if you
@aglee
aglee / ij.py
Last active October 15, 2016 11:23
# Expects i and j to be 1-based row and column indexes, respectively.
def f(i, j):
return (((i+j-1) * (i+j))/2) - (i if ((i+j-1) & 1 == 0) else j) + 1
# Test: use f() to generate matrix and visually inspect it
# to see that the numbers are correct.
for i in range(10):
line = ""
for j in range(10):
line += "%5d" % (f(i+1, j+1))
@aglee
aglee / ij.swift
Last active October 15, 2016 11:24
import Foundation
// Expects i and j to be 1-based row and column indexes, respectively.
func f(_ i: Int, _ j: Int) -> Int {
return ((i+j-1) * (i+j))/2 - (((i+j-1) & 1 == 0) ? i : j) + 1
}
// Test: use f() to generate matrix and visually inspect it
// to see that the numbers are correct.
for i in 1...10 {
#!/usr/bin/python
"""
https://www.hackerrank.com/challenges/cut-the-tree
"""
class Node(object):
def __init__(self, weight):
self.weight = weight
self.adjacent_nodes = []
@aglee
aglee / TreeSearch.swift
Last active June 22, 2016 16:52
A Swift exercise
func ==(lhs: Node, rhs: Node) -> Bool {
return lhs === rhs
}
class Node: Equatable {
var value: String
var children: [Node] = []
init(_ value: String) {
self.value = value
//
// RVNReceiptValidation.m
//
// Created by Satoshi Numata on 12/06/30.
// Copyright (c) 2012 Sazameki and Satoshi Numata, Ph.D. All rights reserved.
//
// This sample shows how to write the Mac App Store receipt validation code.
// Replace kRVNBundleID and kRVNBundleVersion with your own ones.
//
// This sample is provided because the coding sample found in "Validating Mac App Store Receipts"
@aglee
aglee / NSApplication+MarkdownCredits.h
Created December 22, 2013 18:53
Category on NSApplication that makes it easy to use a Markdown-formatted Credits file, or to use your README as the Credits file.
//
// NSApplication+MarkdownCredits.h
// ALUtilities
//
// Copyright (c) 2013 Andy Lee
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software
// and associated documentation files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge, publish, distribute,
// sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is