Skip to content

Instantly share code, notes, and snippets.

View CaffeinatedDave's full-sized avatar

Dave Chaston CaffeinatedDave

View GitHub Profile
require 'yaml'
require 'twitter'
yaml = YAML.load_file('twitter.yaml')
$client = Twitter::REST::Client.new do |config|
config.consumer_key = yaml["consumer_key"]
config.consumer_secret = yaml["consumer_secret"]
config.access_token = yaml["token"]
config.access_token_secret = yaml["secret"]
require "serialport"
require "net/http"
require "uri"
port_str = "/dev/tty.usbmodem1421"
baud_rate = 9600
data_bits = 8
stop_bits = 1
parity = SerialPort::NONE
object scratch {
type SentenceAnagram = Set[String]
type SolutionSpace = Set[SentenceAnagram]
type CharacterCounts = Map[Char, Int]
def frequencyCount(word: String): CharacterCounts = {
val grouped: Map[Char, String] = word.toLowerCase.replaceAll("[^a-z]", "").groupBy(c => c)
grouped.map((pair: Tuple2[Char, String]) => (pair._1, pair._2.length))
}
@CaffeinatedDave
CaffeinatedDave / shuffle.js
Created February 2, 2015 19:16
Simple card shuffler
function Card(face, suit) {
this.face = face;
this.suit = suit;
// Cheap to_string function
this.to_s = function() {
return "The " + face + " of " + suit;
}
}
@CaffeinatedDave
CaffeinatedDave / Langton.scala
Created September 19, 2014 07:38
Implementation of Langton's Ant - WLHN Sept '14
object Langton extends App {
// TODO - make infinite vectors...
type Grid = Vector[Vector[Boolean]]
case class Ant(pos: (Int, Int), direction: Symbol) {
// If current position is white (false), move right 1 square
// If position is black (true), move left
def move(grid: Grid): Ant = {
@CaffeinatedDave
CaffeinatedDave / tsp.scala
Created August 9, 2014 09:23
West London Hack Night - Genetic Algorithm
import java.awt.{Color, Dimension, Graphics2D}
import javax.swing.JPanel
import scala.annotation.tailrec
import scala.collection.mutable.ListBuffer
import scala.swing.MainFrame
import scala.util.Random
class City (val x : Double, val y : Double, val name : String) {
def distanceTo (c : City) : Double = {
Math.sqrt((this.x - c.x) * (this.x - c.x) + (this.y - c.y) * (this.y - c.y))
@CaffeinatedDave
CaffeinatedDave / AliceInMarkovChains.scala
Created August 9, 2014 09:21
West London Hack Night - Markov Chains
import scala.io.Source
import java.io.File
import java.util.Arrays
import scala.collection.mutable.ArrayBuffer
import scala.collection.immutable.HashMap
import scala.annotation.tailrec
/**
* Alice in Markov Chains for West London Hack Night
*
@CaffeinatedDave
CaffeinatedDave / Brainfuck.scala
Last active August 29, 2015 14:05
West London Hack Night - Brainfuck interpreter
object Compiler extends Application {
val program = "++++++++++" +
"[" +
">+++++++" +
">++++++++++" +
">+++" +
">+" +
"<<<<-" +
"]" +
@CaffeinatedDave
CaffeinatedDave / Sudoku.scala
Created August 3, 2014 10:47
Sudoku Solver
object Solver extends App{
case class Puzzle (values: Vector[Vector[Option[Int]]]) {
def printState {
for (row <- values) {
println((for (cell <- row) yield {
cell match {
case None => "_"
case Some(i) => i
}
@CaffeinatedDave
CaffeinatedDave / Voronoi.scala
Created July 23, 2014 11:32
West London Hack Night - Voronoi
object Voronoi extends App {
case class Point(x: Int, y: Int)
val pubs: List[Point] = List(Point(1,4), Point(2,1), Point(4,3))
def findDist(start: Point, pub: Point): Int = {
Math.abs(start.x - pub.x) + Math.abs(start.y - pub.y)
}