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
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 / 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 {
@BradB132
BradB132 / AutoLayoutController.swift
Created May 28, 2015 22:20
AutoLayoutRecipesPt1-1
@IBOutlet weak var constraint1: NSLayoutConstraint!
@IBOutlet weak var constraint2: NSLayoutConstraint!
@IBAction func animateViews(sender: AnyObject) {
constraint1.constant = CGFloat(arc4random_uniform(200))
constraint2.constant = CGFloat(arc4random_uniform(200))
UIView.animateWithDuration(0.5, animations: {
self.view.setNeedsUpdateConstraints()
self.view.layoutIfNeeded()
BOOL isCurrentlyDaytimeInNYC()
{
static CGFloat const latitudeNYC = 40.7831;
static CGFloat const longitudeNYC = 73.9712;
return isCurrentlyDaytime(latitudeNYC,
longitudeNYC);
}
@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 / TTPushAnimator.m
Created June 10, 2015 04:51
ViewControllerTransitionsPart1-5
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{
UIViewController* toController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIViewController* fromController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIView* container = [transitionContext containerView];
//get rects that represent the top and bottom halves of the screen
CGSize viewSize = fromController.view.bounds.size;
CGRect topFrame = CGRectMake(0, 0, viewSize.width, viewSize.height/2);
CGRect bottomFrame = CGRectMake(0, viewSize.height/2, viewSize.width, viewSize.height/2);