Skip to content

Instantly share code, notes, and snippets.

View stphung's full-sized avatar

Steven Phung stphung

  • Autodesk, Inc.
  • San Francisco, CA
View GitHub Profile
@stphung
stphung / PaintBucket.java
Created May 9, 2011 03:30
Implementation of paint bucket operation using bfs
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
public class PaintBucket {
public static void main(String[] args) {
int[][] colors = new int[][] {{2,2,0,1},
{1,0,0,0},
{0,0,0,1},
{1,1,1,1}};
@stphung
stphung / Magicka.scala
Created May 10, 2011 05:22
Google Code Jam 2011 Qualifier Round - Problem B, Magicka
object Magicka {
def invokeElements(spells: String, combines: List[String], oppositions: List[String]) = {
val oppositionSets = oppositions.map(_.toSet)
val combineMap = combines.map(c => (Set(c.charAt(0),c.charAt(1)), c.charAt(2))).toMap
spells.map(List(_)).foldLeft(List[Char]())((acc,spell) => {
acc.size match {
case 0 => spell
case _ => {
// Lazily compute the set which represents spells in the accumulator that clash with the current spell.
lazy val clashes = oppositionSets.filter(_.contains(spell.head)).map(_.diff(Set(spell.head))).flatten
@stphung
stphung / TheNumbersWithLuckyLastDigit.java
Created May 17, 2011 16:18
TopCoder SRM 505 Division 2 - 500 point problem
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class TheNumbersWithLuckyLastDigit {
private static int INF = 9999;
public int find(int n) {
int[][] edges = new int[10][10];
for (int i = 0; i < edges.length; i++)
@stphung
stphung / CubeAnts.java
Created May 29, 2011 21:35
TopCoder SRM 507 Division 2 - 250 point problem
import java.util.ArrayList;
import java.util.List;
public class CubeAnts {
public int getMinimumSteps(int[] pos) {
List<Integer> l = new ArrayList<Integer>();
for (int i : pos) l.add(i);
if (l.contains(6)) return 3;
else if (l.contains(2) || l.contains(5) || l.contains(7)) return 2;
else if (l.contains(3) || l.contains(1) || l.contains(4)) return 1;
@stphung
stphung / CubeStickers.java
Created May 29, 2011 21:35
TopCoder SRM 507 Division 2 - 500 point problem
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class CubeStickers {
public String isPossible(String[] sticker) {
Set<String> uniqueStickers = new HashSet<String>();
for (String s : sticker) uniqueStickers.add(s);
if (uniqueStickers.size() >= 5) return "YES";
@stphung
stphung / CandyShop.java
Created June 2, 2011 15:56
TopCoder SRM 508 Division 2 - 250 point problem
import java.util.HashSet;
import java.util.Set;
public class CandyShop {
public int countProbablePlaces(int[] X, int[] Y, int[] R) {
Set<Position> pos = new HashSet<Position>();
pos.add(new Position(X[0], Y[0]));
pos = locationsFor(R[0], pos);
for (int i = 1; i < X.length; i++) {
@stphung
stphung / gist:1255426
Created October 1, 2011 00:42
It's Over Nine Thousand Control Structure - The Crap Version
def itsOverNineThousand(n: Int)(f: () => Boolean) = {
if (n > 9000) {
f()
}
}
@stphung
stphung / gist:1255428
Created October 1, 2011 00:43
It's Over Nine Thousand Control Structure - Good Usage
itsOverNineThousand(15000) {
println("I think this is over nine thousand, yeah! So.. I guess I'll return true.")
true
}
@stphung
stphung / gist:1255432
Created October 1, 2011 00:44
It's Over Nine Thousand Control Structure - The Good Version
def itsOverNineThousand(n: Int)(body: => Boolean) = {
if (n > 9000) {
body
}
}
@stphung
stphung / gist:1255440
Created October 1, 2011 00:49
It's Over Nine Thousand Control Structure - Crap Usage
itsOverNineThousand(15000) { () =>
println("I think this is over nine thousand, yeah! So.. I guess I'll return true.")
true
}