Skip to content

Instantly share code, notes, and snippets.

View benzittlau's full-sized avatar

Ben Zittlau benzittlau

View GitHub Profile
@benzittlau
benzittlau / injection_pattern.ts
Last active January 24, 2025 22:56
The challenge here is to try and implement TypeScript that can handle a set of different operations defined in objects that are internally consistent in their type requirements. By this I mean that each object has a consistent interface except that the types are different object to object. The interface is of a consistent type, but has constrain…
// The challenge here is to try and implement TypeScript that can handle a set of different operations
// defined in objects that are internally consistent in their type requirements. By this I mean that
// each object has a consistent interface except that the types are different object to object. The
// interface is of a consistent type, but has constraints, such as the return type of one function
// being the argument for another function. I then want to be able to have generic code that is
// able to "execute" each of these type specific objects while being type safe and not throwing
// type errors.
//
// The primary issue is that TypeScript infers the types to be a union type, for example String | Number,
// and consequently throws a type error. The structure doesn't allow it to understand that in each
/**
* @param {NS} ns
*/
export async function main(ns) {
ns.print("Running hacking script...");
var target; securityTreshold; moneyTreshold
// Identify the server to attempt to hack
var target = ns.args[0];
@benzittlau
benzittlau / 4_by_4_tooling.js
Created February 12, 2019 22:31
4 By 4 Skyscrapers Tooling
function solvePuzzle (clues) {
// The hint says the value
// that must *at least* be in the adjacent cell.
// 1 -> 4
// 2 -> 3, etc.
// Calculate the possible permutations for each score in advance
// Rotate around in the order of hints, check if there is only
// one permutation that matches the hint and populated values, if
// so then populate it.
@benzittlau
benzittlau / player.rb
Created March 11, 2017 06:53
Ruby warrior intermediate player implementation (Level 4)
class Player
DIRECTIONS = [:left, :forward, :right, :backward]
def play_turn(warrior)
@warrior = warrior
bind_enemies! ||
attack_enemies! ||
heal! ||
@benzittlau
benzittlau / merge_defaults.rb
Created February 10, 2017 20:16
Merging defaults with options hash in a ruby function
# Related area of style guide: https://github.com/GetJobber/ruby-style-guide#hash-fetch-defaults
# Pros: Is succinct and intuitive
# Cons: Doesn't work for false values
def default_one(options)
options[:foo] ||= 'foo'
options[:bar] ||= 'bar'
end
# Pros: Handles false without issue, is succient
@benzittlau
benzittlau / benchmarking.rb
Last active March 26, 2016 18:22
Simple benchmarking util
# Usage:
# $bm = Benchmarking.new
# VisitGeneratorWorker.new.perform(work_order.id)
# $bm.finish
# $bm.results
#
# Inside what you're benchmarking:
# $bm.mark("This is a data point")
#
@benzittlau
benzittlau / Add_line_at_matching_line.rb
Created November 27, 2015 19:10
Searches for a certain set of files, and injects the same line into each line at a matching place
def inject_model_fixtures(filepath)
content = File.read(filepath).split("\n")
block_start = content.find_index{|l| l =~ /^RSpec.describe/}
return false if block_start.nil?
content.insert(block_start + 1, " include_context 'model_fixtures'")
File.open(filepath, 'wb') { |file| file.write(content.join("\n")) }
module Net
class HTTP
def request_with_monitor(request, body = nil, &block)
# Continue with the original request
response = request_without_monitor(request, body, &block)
begin
::NetNanny::HttpNetMonitor.output_request_debug_info(self, request)
if ::NetNanny::Registry.instance.registered_address?(self.address)
-- HUMAN RESOURCE MACHINE PROGRAM --
COMMENT 0
a:
COPYFROM 24
COPYTO 20
b:
INBOX
JUMPZ c
COPYTO [20]
@benzittlau
benzittlau / hard_click.js
Created May 29, 2015 21:33
Programatically Accept Suggestions in Google Docs
function hardClick(node) {
triggerMouseEvent (node, "mouseover");
triggerMouseEvent (node, "mousedown");
triggerMouseEvent (node, "mouseup");
triggerMouseEvent (node, "click");
}
function triggerMouseEvent (node, eventType) {
var clickEvent = document.createEvent ('MouseEvents');
clickEvent.initEvent (eventType, true, true);