Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/swift
/*
* Command line script that draws an evenly spaced grid over an image and labels each grid square.
*/
import Foundation
// Function to run a shell command and capture the output
func runShellCommand(_ command: String, printCommand: Bool) -> Int32 {
@BradB132
BradB132 / Cavegen_comparison.txt
Last active March 30, 2023 21:00
CaveGen performance comparison (Godot)
// BEGIN GDSCRIPT - Takes >9000ms.
extends TextureRect
const map_dimension: int = 1024
enum MapType { AIR = 0, ROCK = 1 }
var map_array: PackedInt32Array
var temp_array: PackedInt32Array
@BradB132
BradB132 / WordleSolver.swift
Last active May 11, 2022 15:26
Wordle solver (inspired by 3 Blue 1 Brown)
#!/usr/bin/swift
// Best starters according to 3 Blue 1 Brown are "Trace" and "Crate".
// https://www.youtube.com/watch?v=fRed0Xmc2Wg
// This script, given the entire Wordle word list, chooses the following:
// TARES - 6.196031220185254
// LARES - 6.151215692838039
// RALES - 6.116011613563718
// RATES - 6.098037836330005
import Foundation
// Get the Unix list of all words.
let words = try! String(contentsOfFile: "/usr/share/dict/words")
.split(whereSeparator: \.isNewline).map({ $0.uppercased() })
// Calculate frequencies of each letter in the above corpus of words.
var wordFrequencies:[String.Element: Int] = [:]
for word in words {
// Only look at 5-letter words.
if word.count != 5 {
BOOL isCurrentlyDaytimeInNYC()
{
static CGFloat const latitudeNYC = 40.7831;
static CGFloat const longitudeNYC = 73.9712;
return isCurrentlyDaytime(latitudeNYC,
longitudeNYC);
}
BOOL isCurrentlyDaytime(CGFloat latitude, CGFloat longitude)
{
// http://users.electromagnetic.net/bu/astro/iyf-calc.php
// http://aa.quae.nl/en/reken/zonpositie.html
static NSUInteger const secondsPerDay = 60*60*24;
static NSTimeInterval const julianLeap = 0.0009;
static double const degreeToRadian = M_PI/180.0;
static double const radianToDegree = 180.0/M_PI;
@BradB132
BradB132 / ViewController.swift
Last active January 18, 2016 17:01
Focus_in_tvOS
import UIKit
class ViewController: UIViewController {
@IBOutlet var buttonA:UIButton!
@IBOutlet var buttonB:UIButton!
override func viewDidLoad() {
super.viewDidLoad()
@BradB132
BradB132 / TraitViewController.swift
Last active November 28, 2015 19:44
SizeClassConcepts
class TraitViewController: UIViewController {
//this function comes from the UITraitEnvironment protocol
override func traitCollectionDidChange(previousTraitCollection: UITraitCollection?) {
let horizontalSize = traitCollection.horizontalSizeClass
let verticalSize = traitCollection.verticalSizeClass
let previousHorizontalSize = previousTraitCollection.horizontalSizeClass
let previousVerticalSize = previousTraitCollection.verticalSizeClass
@BradB132
BradB132 / ViewController.swift
Created July 4, 2015 19:46
InterfaceBuilderTricks-2
let testView = view.viewWithStringTag("testView")
println("TEST VIEW: \(testView)")
@BradB132
BradB132 / UIVIew+stringTag.swift
Created July 3, 2015 16:13
InterfaceBuilderTricks-1
private var stringTagHandle: UInt8 = 0
extension UIView {
//use Objective C Associated Object API to add this property to UIView
@IBInspectable public var stringTag:String? {
get {
if let object = objc_getAssociatedObject(self, &stringTagHandle) as? String {
return object
}