Skip to content

Instantly share code, notes, and snippets.

@manlon
manlon / 7.rb
Created December 7, 2017 09:43
class AoC7
attr_reader :weights, :child_map, :root_name
PATTERN = /^([a-z]+) \(([0-9]+)\)( -> ([a-z ,]+))?$/
def initialize
lines = IO.read("./input7.txt").strip.split("\n")
@weights = {}
@child_map = {}
lines.each do |line|
@manlon
manlon / 4.py
Created December 4, 2017 08:33
def inputs():
for line in open("./input4.txt"):
yield line.split()
def sort_string(s):
l = list(s)
l.sort()
return "".join(l)
def has_dupes(words):
@manlon
manlon / 3.py
Last active December 4, 2017 02:20
from itertools import cycle, islice
def coords():
dirs = cycle([(1,0), (0,1), (-1,0), (0, -1)])
(dx, dy) = next(dirs)
yield (0,0)
minx = miny = maxx = maxy = 0
x = y = 0
while True:
(x,y) = (x + dx, y + dy)
defmodule AoC3 do
def coord_stream do
directions = [{1,0}, {0,1}, {-1,0}, {0, -1}]
Stream.unfold({{0,0}, {0,0,0,0}, directions},
fn {{x,y}, {minx, miny, maxx, maxy}, dirs = [{dx,dy} | rest]} ->
{x_,y_} = {x+dx, y+dy}
{minx, miny, maxx, maxy, dirs} =
if (x_ > maxx) || (x_< minx) || (y_ > maxy) || (y_ < miny) do
{min(minx, x_), min(miny, y_), max(maxx, x_), max(maxy, y_), rest ++ [{dx,dy}]}
else
@manlon
manlon / .vimrc
Last active September 19, 2017 00:16
filetype on
filetype off
execute pathogen#infect()
syntax on
filetype plugin indent on
" ruby-friendly tab defaults
set tabstop=2
set smarttab
set shiftwidth=2
var fs = require("fs");
var express = require("express");
var jsdom = require("jsdom");
var serializeDocument = require("jsdom").serializeDocument;
var elmSrc = fs.readFileSync("./elm.js"); // Elm program with Main module and signalInit port
var html = `
<!DOCTYPE html>
<html>
module BatchesRandomStart
# `find_in_batches`, but starting from a random position in the set of
# records, then continuing from the beginning of the set. Give up early
# if `time_limit` elapses.
def find_in_batches_random_start(batch_size: 10, time_limit: nil)
relation = scoped
c = relation.count
return if c == 0
@manlon
manlon / secondary_db_join_table_test.rb
Created August 3, 2015 22:59
repro case for issue with join tables in non-default db
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
filetype on
filetype off
execute pathogen#infect()
syntax on
filetype plugin indent on
" ruby-friendly tab defaults
set tabstop=2
set smarttab
set shiftwidth=2
if has("gui_macvim")
macmenu &File.New\ Tab key=<nop>
map <D-t> :CtrlP<CR>
endif