Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/python
# vim: set expandtab:ts=2:sw=2
import argparse
import bluetooth
import struct
import sys
def checksum(msg):
checksum = 0
#!/usr/bin/python
# Invoke n instances of sudo in parallel. This works fine in all machines I've
# tried this on, except Windows Azure VMs, where occasionally invocations to
# sudo take ~5s to complete when n > 1.
# Tested on a Standard_A1 instance w/Ubuntu Utopic, kernel 3.16.0-34-generic.
import subprocess
import sys
import time
@lhchavez
lhchavez / deproyector.py
Created July 21, 2011 17:15
removes a perspective projection from a region of an image.
#!/usr/bin/python
import Image
import math
# calculates simple two-line intersection. be warned: fails miserably with
# vertical lines and parallelograms.
def intersection(p0, p1, p2, p3):
m0 = (p1[1] - p0[1]) / float(p1[0] - p0[0])
m2 = (p3[1] - p2[1]) / float(p3[0] - p2[0])
@lhchavez
lhchavez / batman.txt
Created July 30, 2011 07:59
The "Batman" polynomial. A pair of parametric equations that, when plotted with ~200 bits of precision, draws a Batman logo.
t ∈ [0, 1];
x(t) = 0.0000000000000000055471625557159340725376176154529062181733718807506255520255*t^302 - 0.000000000000000073364101000888831703733953581037511007054939579810582331399*t^301 + 0.00000000000000019993660043110753520337148440192171752144851216958770618608*t^300 + 0.0000000000000025431459190353075519117623598827064762116187905095073821053*t^299 - 0.000000000000025925946645421675754267336846692479715572284533512884725878*t^298 + 0.000000000000033895461820820261658723914655701363831301259561299803770339*t^297 + 0.00000000000098435998558754374882208672660807298710459239071613885704591*t^296 - 0.0000000000068611167133899914841924408007856853142977551403882403283457*t^295 - 0.0000000000048908713957691280882664471217742259158249732413864474770153*t^294 + 0.00000000029798626331047729269718080398515232860371117604820388543549*t^293 - 0.0000000012579473080466912761750295031070320627008549897730597637288*t^292 - 0.0000000050712723859962030166333419681481068633434285144559824963219*t^291 + 0.0000000672479633
#!/usr/bin/python
import argparse
import random
import subprocess
import sys
import time
PREFIX = '/sbin/ip netns exec'
@lhchavez
lhchavez / comesolo.txt
Last active December 22, 2015 21:29
solución comesolo
Este problema es especial porque es el primero en omegaUp de solo salida! Usualmente lo que debes esperar cuando te enfrentes con uno de esos problemas es que sea un problema NP que no tiene una solución rápida, y usualmente te pedirán que te aproximes lo más posible a la solución óptima. Esto significa que te vas a tener que valer de técnicas ad-hoc y heurísticas para sacar puntos.
La solución del problema es bastante sencilla de explicar: haz una búsqueda en profundidad intentando todos los posibles movimientos por fuerza bruta hasta que te salga una solución aceptable e imprímela. El problema es que esta estrategia es $O(n!)$, y como $n$ puede valer hasta 30x30, puedes esperar que el programa corria varios milenios antes de encontrar la solución óptima. Hay tres trucos (en orden de importancia) para obtener una solución decente en un tiempo razonable:
* No repetir estados.
* No "clavarse" con soluciones que parece que son muy buenas, pero en realidad llevan a callejones sin salida.
* Encontrar una maner
@lhchavez
lhchavez / sumas.java
Created November 29, 2013 19:21
Código para resolver Sumas.
import java.io.*;
import java.util.*;
// Todos los problemas en Java _deben_ usar Main como nombre de clase.
class Main {
public static void main(String[] args) throws IOException {
// Declaración del scanner para poder leer por tokens.
Scanner in = new Scanner(System.in);
int a, b, c;
@lhchavez
lhchavez / sumas.java
Created November 29, 2013 19:23
Sumas 64-bit
import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
long a, b, c;
a = in.nextInt();
b = in.nextInt();
@lhchavez
lhchavez / quimicos-edmonds.cpp
Last active January 3, 2016 23:29
Solución de quimicos, algoritmo de Edmonds (Blossom)
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/max_cardinality_matching.hpp>
using namespace std;
// Un grafo no-dirigido.
@lhchavez
lhchavez / karelobscuro.cpp
Created January 21, 2014 07:26
Solución de karel-obscuro-vs-karel
#include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
#define IOI 2
struct Arco {