Skip to content

Instantly share code, notes, and snippets.

View justinhj's full-sized avatar

Justin Heyes-Jones justinhj

View GitHub Profile
@justinhj
justinhj / efind
Created June 29, 2014 16:42
A bash script to easily search files for a regular expression
#!/bin/bash
# A simple script to recursively search files
if [ "$#" -ne 3 ]; then
echo "efind (easy find) usage: [path] [file pattern] [regex]"
echo 'efind ../c "*.c" "string"'
exit 1
fi
find "$1" -name "$2" -type f -print0 | xargs -0 egrep -iHn "$3"
// Determine if a value is the product of any two numbers in a vector
// Converts the array to a stream so it can be consumed lazily
// the helper function is recursive but subject to tail call optimization
def prod(in: Array[Int], v: Int): Boolean = {
def helper(in: Stream[Int], v: Int, found: Map[Int, Int]): Boolean = in match {
case x #:: xs => {
val r = v / x
if(r * x != v) helper(xs, v, found)
else if(found.contains(x)) true
@justinhj
justinhj / FileExt.scala
Last active August 29, 2015 14:20
Using unapply to match strings that are valid filenames with an extension
object FileExt {
def unapply(s: String): Option[String] = {
val parts: Array[String] = s.split('.')
if(parts.length < 2) None
else Some(parts.last)
}
@justinhj
justinhj / backupchangeslist.py
Last active August 29, 2015 14:22
backup your p$ change list files to a dated folder
# Copies a changelist (any files open for edit or add) to a directory you specify
# (C)2011 Justin Heyes-Jones
# TODO
# Allow user to pass in PORT setting
# Or at least check that it is set before doing perforce interactions
# P4 set P4PORT=servername:port
import subprocess, sys, marshal, string, os, time, datetime
@justinhj
justinhj / help.md
Created September 22, 2015 18:03
Water jug help for A*

State representation

In the 8Puzzle the state of each step in the solution is an array of tiles. In the water jug it can be an array of jug levels [0,0]

You can replace tiles with jugs TILE tiles[ BOARD_WIDTH*BOARD_HEIGHT ]; int jug_levels[ 2 ];

State transitions

@justinhj
justinhj / futureDemo.scala
Last active October 12, 2016 21:25
Demo of concurrency handling in for comprehension using Futures
// Demonstrating futures running concurrently and in sequence
import scala.concurrent.{ExecutionContext, Future}
import scala.language.postfixOps
// A simple execution context for us to use that will not shut down the JVM until the threads we start have all finished
// It does that by making the threads non-Daemon
// http://stackoverflow.com/questions/25236143/why-future-example-do-not-work
// http://stackoverflow.com/questions/2213340/what-is-daemon-thread-in-java
object Xmas1 {
object Move {
def fromString(arg: String): Option[Move] = arg match {
case s if arg.startsWith("R") || arg.startsWith("L") =>
try {
val turn = if(arg.charAt(0) == 'R') Right else Left
import scala.collection.{immutable, mutable}
object Xmas1 {
object Move {
def fromString(arg: String): Option[Move] = arg match {
case s if arg.startsWith("R") || arg.startsWith("L") =>
try {
// Solution to http://adventofcode.com/2016/day/2
object Xmas2 {
case class Transition(name: Char, target: Digit)
case class Digit(value: Int, var transitions: Vector[Transition] = Vector[Transition]()) {
def addTransitions(newTransitions: Vector[Transition]) = {
transitions = transitions ++ newTransitions
// Adapting part 1 to part 2 was a simple matter of changing the network of digits and transitions between them
object Xmas2 {
case class Transition(name: Char, target: Digit)
case class Digit(value: Int, var transitions: Vector[Transition] = Vector[Transition]()) {
def addTransitions(newTransitions: Vector[Transition]) = {
transitions = transitions ++ newTransitions