Skip to content

Instantly share code, notes, and snippets.

@carlynorama
carlynorama / vm_setup.sh
Last active July 24, 2023 20:49
Linux-VM History (Labtanza)
sudo apt update
sudo apt upgrade
sudo apt install clang libicu-dev build-essential pkg-config
apt install git gh
apt install libpng-dev
## NOTE THIS WAS DELETED LATER
apt install cmake
mkdir ~/swift
@carlynorama
carlynorama / StringNode.swift
Last active July 17, 2023 17:49
Generic StringNode tree for constructing nested text in Swift, with Result Builder
//
// StringNode.swift
//
//
// Created by Carlyn Maw on 7/16/23.
// MIT License
// https://www.whynotestflight.com/excuses/hello-usd-part-11-gotta-make-it-easier-to-write-file-builders/
public struct Indent {
let count:Int
@carlynorama
carlynorama / rainbow_mesh.usd
Created June 28, 2023 23:08
Rainbow Cube USD examples
#usda 1.0
def Xform "Rainbow"
{
def Mesh "cubeMesh"
{
float3[] extent = [(-2, -2, -2), (2, 2, 2)]
int[] faceVertexCounts = [4, 4, 4, 4, 4, 4]
int[] faceVertexIndices = [0, 1, 2, 3, 4, 5, 6, 7, 0, 6, 5, 1, 4, 7, 3, 2, 0, 3, 7, 6, 4, 2, 1, 5]
protocol Vegetable {
var crunchy:Bool { get }
var volume:Int { get } //in mL
}
struct Carrot:Vegetable {
var volume: Int = 300
var crunchy = true
}
@carlynorama
carlynorama / FixedWidthInteger+Data.swift
Created March 18, 2023 23:57
Converting a fixed width integer to Data in Swift. e.g. UInt32/Int32, etc.
extension FixedWidthInteger {
var dataBigEndian: Data {
var int = self.bigEndian
return Data(bytes: &int, count: MemoryLayout<Self>.size)
}
var dataLittleEndian: Data {
var int = self.littleEndian
import UIKit
import PlaygroundSupport
import Foundation
PlaygroundPage.current.needsIndefiniteExecution = true
//URLSessionTaskDelegate
//You use this protocol in one of two ways, depending on how you use a URLSession:
//If you create tasks with Swift’s async-await syntax, using methods like bytes(for:delegate:) and data(for:delegate:), you pass a delegate argument of this type. The delegate receives callbacks for things like task progress, while the call point awaits the completion of the task. A delegate that receives life cycle and authentication challenge callbacks as the transfer progresses.
//If you add tasks to the session with methods like dataTask(with:) and downloadTask(with:), then you implement this protocol’s methods in a delegate you set on the session. This session delegate may also implement other protocols as appropriate, like URLSessionDownloadDelegate and URLSessionDataDelegate. You can also assign a delegate of this type directly to the task to intercept callbacks before the task deliver
@carlynorama
carlynorama / json_tests.js
Created February 4, 2023 01:34
Working With JSON in JavaScript
import { readFile, writeFile } from 'fs/promises';
import { readFileSync, writeFileSync } from 'fs';
function writeToJson(path, data, rewrite = false) {
let old_data = readFileSync(path);
if (old_data.length == 0 || rewrite == true) {
writeFileSync(path, JSON.stringify(data, null, 4));
return;
}
let json_obj = [JSON.parse(old_data)]; // without brackets it reverts an error
json_obj.push(data);
@carlynorama
carlynorama / AOC2022_day15.swift
Last active December 16, 2022 03:04
My answer to Day15 of Advent of Code. Uses ranges. Searches what I thought might be more likely rows first. "AOC2022_day15.swift" is the playground file. "Range+Smush.swift" is for the "Sources" folder and "day15_testinput.txt" for "Resources."
//
// Day15.swift
// AdventOfCode2022
//
// Created by carlynorama on 12/15/22.
import Foundation
let testData = "day15_testinput"
@carlynorama
carlynorama / drawPoints.swift
Created December 9, 2022 18:53
Function that takes an array of Points (a Type with (x,y) ) and creates an ASCII representation.
func drawPoints(array:[P], width:Int, height:Int) -> String {
var outterArray:[[String]] = []
for _ in (0..<width) {
outterArray.append((Array(repeating: ".", count:height)))
}
for point in array {
outterArray[point.y][point.x] = "#"
}
//[Y][X]
@carlynorama
carlynorama / regex_indexParser.swift
Last active December 6, 2022 16:56
Use a regular expressions to get indices from one string. Then use those indices to get the content from another. #AdventOfCode2022 #Day5 https://adventofcode.com/2022/day/5
import UIKit
let stringA = " 1 2 3 4 5 6 7 8 9"
let stringB = "[F] [R] [C] [F] [L] [Q] [F] [D] [P]"
let matches = stringA.matches(of: /\b[0-9]+\b/)
for match in matches {