Skip to content

Instantly share code, notes, and snippets.

@pathikrit
pathikrit / sudoku-1.coffee
Created October 10, 2012 07:04
1-liner CoffeeScript Sudoku Solver
solve = (s, c = 0) -> if c is 81 then s else if s[x = c/9|0][y = c%9] isnt 0 then solve s, c+1 else (([1..9].filter (g) -> ![0...9].some (i) -> g in [s[x][i], s[i][y], s[3*(x/3|0) + i/3|0][3*(y/3|0) + i%3]]).some (g) -> s[x][y] = g; solve s, c+1) or s[x][y] = 0
import java.util.BitSet;
import java.util.NavigableSet;
import java.util.NoSuchElementException;
import java.util.concurrent.ConcurrentSkipListSet;
/**
* A concurrent sorted set with a fixed bound
*/
public class BoundedConcurrentSortedSet {
@pathikrit
pathikrit / MyAnt.java
Created November 20, 2012 21:00
Addepar Ants (solution to https://addepar.com/challenge.php)
import ants.Action;
import ants.Ant;
import ants.Direction;
import ants.Surroundings;
import ants.Tile;
import java.awt.Point;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
@pathikrit
pathikrit / bloomberg.js
Created December 4, 2012 04:38
Bloomberg scraper
var config = require('./config'), request = require('request'), fs = require('fs'), Zip = require('adm-zip'), path = require('path');
var tmpDir = 'tmp';
fs.mkdir(tmpDir);
request.post(config.bloomberg.bsym_url, function (err, res, body) {
if(err) throw err;
var record = eval('(' + body.substring(body.indexOf('{data'), body.lastIndexOf(';')));
record.data.forEach(function(data) {
var url = data.uri, name = path.join(tmpDir, url.substring(url.lastIndexOf('/') + 1));
import collection.mutable
import collection.JavaConversions._
import org.mapdb.DBMaker // Great disk based map - https://github.com/jankotek/MapDB
/**
* My attempt at http://www.azspcs.net/Contest/Factorials/
* Brief explanation:
* Brute force BFS (ignoring non-positive numbers and duplicates) upto length `fbf`
* Higher the `fbf`, the better quality result. I managed to get to `fbf`=8 before running out of memory
* Beyond `fbf`, try to multiply your way to n! Since the last few steps involve only multiplication,
@pathikrit
pathikrit / Calendar.java
Created March 28, 2013 08:29
Some crazy code
/*
* Works from dates after Sep 14, 1752 (New System of Gregorian Calendar (UK adoption))
* May crash or return junk value for negative or other invalid inputs.
*/
public static String calendar(int d, int m, int y) {return "Sun Mon Tues Wednes Thurs Fri Satur ".substring(d=((6-2*(y/100%4)+(m<3&&y%4==0&&(y%100!=0||y%400==0)?-1:0)+(y%=100)/12+5*(y%12)/4+"xbeehcfhdgbeg".charAt(m)+d)%7)*7,d+7).trim()+"day";}
@pathikrit
pathikrit / Trick.java
Last active December 15, 2015 23:58
You win a prize if you correctly guess the output
import java.util.*;
public class Trick {
public static void main(String args[]) {
System.out.println("/////////////////////////////////////////////////");
HashSet<Long> h = new HashSet();
Long l = (long)4;
h.add(l);
System.out.println(h.contains(4));
@pathikrit
pathikrit / IntervalMap.java
Created May 14, 2013 08:35
Interval Map in Java
package com.github.pathikrit.scalgos;
import java.util.Map.Entry;
import java.util.TreeMap;
/**
* Efficient data structure to map values to intervals
* e.g. set(5, 60000, "hello") would set all keys in [5, 60000) to be "hello"
*
* All operations are O(log n) (in practice much faster since n is usually number of segments)
@pathikrit
pathikrit / DXBALL.cpp
Created June 24, 2013 09:38
First "big" real code I ever wrote - for my 11th grade project at Sardar Patel Vidyalaya in 2003. It is a clone of Breakout written in Turbo C++ compiled using the Borland compiler. It ran (fast) on an old Pentium with 512kb of RAM. I have come a long way since then...
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <dos.h>
#include <time.h>
#include <ctype.h>
class player
@pathikrit
pathikrit / sync_gh_pages.sh
Created June 28, 2013 17:50
git: keep gh-pages in sync with master branch
git checkout gh-pages; git merge -s subtree master; git push origin master gh-pages; git checkout master;