Skip to content

Instantly share code, notes, and snippets.

Avatar

Xavier Noria fxn

View GitHub Profile
View sidekiq.rb
Sidekiq.configure_server do |config|
config.on(:startup) do
BatchWriter.start_timer
at_exit do
BatchWriter.stop_timer
BatchWriter.flush
end
end
end
View batch_writer.rb
require "concurrent"
class BatchWriter
MAX_BATCH_SIZE = 100
TIMER_OPTS = { execution_interval: 1, timeout_interval: 1 }.freeze
cattr_accessor :buffer
self.buffer = []
cattr_accessor :mutex
View sidekiq_worker.rb
class LocationsJob < ApplicationJob
queue_as :high_priority
def perform(location)
BatchWriter.push(location)
end
end
View parallel_inserts.rb
queue = Queue.new
workers = Array.new(N) do
Thread.new do
while record = queue.pop
# insert record
end
end
end
View sequential_inserts.rb
records.each do |record|
# insert record
end
View day19_part2.nim
import strutils, tables, nre, strformat
proc parseRules(rules: seq[string]): Table[string, string] =
for rule in rules:
var parts = rule.split(":")
result[parts[0]] = parts[1].strip
proc buildRegex(idx: string, rules: Table[string, string]): string =
let rule = case idx:
of "8": "(?:42)+"
View day_18_part2.nim
import nre, strutils
proc eval(ex: string): int =
if ex.contains('('):
eval(ex.replace(re"\([^()]+\)", proc (match: string): string =
$eval(match[1 .. ^2])))
elif ex.contains('+'):
eval(ex.replace(re"(\d+) \+ (\d+)", proc (rm: RegexMatch): string =
$(parseInt(rm.captures[0]) + parseInt(rm.captures[1]))))
elif ex.contains('*'):
View aoc_2020_day_15.nim
# Second edition of the solution using Table[int, int]. This is possible
# because we only care about a repetition of the last spoken number, I
# misunderstood an example as more generic than what it is, the rules of
# the game are clear about this.
import strutils, sequtils, tables
var input = "8,11,0,19,1,2".split(',').map(parseInt)
var turns: Table[int, int]
View day08_part02.nim
import intsets, strutils, strscans, sequtils
type
Opcode = enum
acc = "acc"
jmp = "jmp"
nop = "nop"
Instruction = tuple
opcode: Opcode
arg: int
View aoc08-part2.nim
import intsets, strutils, strscans, sequtils
type
Opcode = enum
acc = "acc"
jmp = "jmp"
nop = "nop"
Instruction = tuple
opcode: Opcode
arg: int