Skip to content

Instantly share code, notes, and snippets.

View fxn's full-sized avatar

Xavier Noria fxn

View GitHub Profile
records.each do |record|
# insert record
end
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)+"
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('*'):
# 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]
import intsets, strutils, strscans, sequtils
type
Opcode = enum
acc = "acc"
jmp = "jmp"
nop = "nop"
Instruction = tuple
opcode: Opcode
arg: int
import intsets, strutils, strscans, sequtils
type
Opcode = enum
acc = "acc"
jmp = "jmp"
nop = "nop"
Instruction = tuple
opcode: Opcode
arg: int
import re, strutils, sequtils, tables, sets
type
ContainedBag = tuple
number: int
color: string
Rule = tuple
color: string
contains: seq[ContainedBag]
@fxn
fxn / sol06.nim
Last active December 6, 2020 18:07
import strutils, sequtils, sets
var sum = 0
for group in readAll(stdin).strip.split("\n\n"):
var answers: seq[HashSet[char]]
for person in group.split("\n"):
answers.add(person.toHashSet())
sum += answers.foldl(a * b).card
echo sum
FROM ubuntu:latest
# See https://github.com/oracle/truffleruby/blob/master/tool/docker-configs.yaml.
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
make \
gcc \
libssl-dev \
libz-dev \
ca-certificates \
aria2
module ForkedSpecs
def forked_specs(*specs)
ActiveRecord::Base.clear_all_connections!
pids = []
specs.each do |spec|
pids << fork do
ActiveRecord::Base.establish_connection
spec.call
end