Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View pkulak's full-sized avatar

Phil Kulak pkulak

View GitHub Profile
@pkulak
pkulak / sanitizer.rs
Created April 6, 2023 02:13
Remove Segments From an Iterator
struct Sanitizer<I: Iterator> {
iter: PeekMoreIterator<I>,
needle: Vec<I::Item>,
}
impl<I: Iterator> Sanitizer<I>
where
I::Item: PartialEq,
{
fn new(haystack: I, needle: Vec<I::Item>) -> Self {
@pkulak
pkulak / 100-prisoners-riddle.kt
Created July 1, 2022 21:31
100 Prisoners Riddle
fun main() {
var allUnder = 0
var anyOver = 0
repeat(500_000) {
val boxes = (0 until 100).toList().shuffled()
if (allUnderFifty(boxes)) {
allUnder++
} else {
sensor:
- platform: template
sensors:
front_door_status:
value_template: >-
{%- if is_state("sensor.front_door_lock_type", "19") -%}
{%- if is_state("sensor.front_door_lock_level", "1") -%}
Matt
{%- elif is_state("sensor.front_door_lock_level", "2") -%}
Mark
fun main() {
val nonBlockingTime = measureTime { runBlocking { nonBlocking() } }
println(nonBlockingTime)
val pool = Executors.newFixedThreadPool(1000)
val blockingTime = measureTime { blocking(pool) }
println(blockingTime)
}
suspend fun nonBlocking(total: Int = 1000) = coroutineScope {
@pkulak
pkulak / encrypted_btrfs_snapper_notes.sh
Last active December 12, 2021 22:38
My Arch Encrypted, BTRFS with Snapper Install Notes
# This is mostly here for my reference. If someone on the internet finds this, I hope it's
# helpful, but don't trust it! I'm pretty new at this, which is why I have to keep exhaustive
# notes like this. The corollary then, is that if you're a pro at this, and notice something
# dumb I'm doing, please let me know.
#
# The idea here is to end up with an ecrypted BTRFS filesystem running Snapper in a way that
# makes rollbacks super easy. I tried to keep things simple, so the swap is a file, not a
# partition (that would have to be encryped separately). The subvolume layout is totally flat,
# to make rollbacks a simple matter of moving the volumes around. When this is all done, a
# simple ls of either .snapshots directory will show you your live filesystem right alongside
@pkulak
pkulak / Async.kt
Last active April 7, 2019 22:09
Map an iterable asynchronously using limited concurrency.
/**
* A very simple, non-blocking semaphore.
*/
class Semaphore(capacity: Int) {
private val channel = Channel<Boolean>(capacity)
suspend fun acquire() {
channel.send(true)
}
@pkulak
pkulak / validateBrackets.js
Created December 14, 2016 00:41
Answer to an interview question
let opening = ['{', '[', '('];
let closing = ['}', ']', ')'];
let all = opening.concat(closing)
function validate(s) {
if (s.length == 0) return true;
// ignore unknown chars at the head
while (all.indexOf(s.charAt(0)) == -1) {
s = s.slice(1);
@pkulak
pkulak / climate.rb
Last active January 21, 2020 23:57
A quick script to turn on your Audi A3 E Tron's climate control remotely.
#!/usr/bin/env ruby
require 'unirest'
# Set these by going to etron.audiusa.com/web/aucwp/login once by hand and figuring them out
EMAIL = "me@example.com"
PASSWORD = "password"
ACCOUNT = 9999
PIN = 9999
@pkulak
pkulak / camera_dl.rb
Last active March 9, 2017 16:04
A simple script to get the files off your Olympus camera.
#!/usr/bin/env ruby
require 'open-uri'
require 'time'
require 'json'
class Runner
def initialize
@base = "http://192.168.0.10/DCIM/100OLYMP"
@pkulak
pkulak / gist:61de475a74d824a9d875
Created February 10, 2015 23:41
Roku BIF File JavaScript Parser
// Takes an ArrayBuffer; returns an array of these objects:
// {
// seconds: *number of seconds into the video of this image*,
// bytes: *the bytes of this image, as an ArrayBuffer*
// }
var parseBif = function(buffer) {
var data = new Uint8Array(buffer);
// Make sure this really is a BIF.
var magicNumber = [0x89, 0x42, 0x49, 0x46, 0x0d, 0x0a, 0x1a, 0x0a];