Skip to content

Instantly share code, notes, and snippets.

View sofoklis's full-sized avatar

Sofoklis Papasofokli sofoklis

View GitHub Profile
@sofoklis
sofoklis / heuristic.scala
Created February 21, 2013 22:13
switching heuristic in Eight puzzle
// Creating a different solution with exactly the same code but a different
// ordering in my queue, so one can easily add solutions with better
// heuristic functions
def bestSolution(ordering: Ordering[(State, List[Move])]) =
aStarSearch(PriorityQueue((initialState, List[Move]()))(ordering), Set())
lazy val bestSolutionManhatan: Option[List[Move]] =
bestSolution(manhatanOrdering)
lazy val bestSolutionMisplaced: Option[List[Move]] =
@sofoklis
sofoklis / 8puzzle.scala
Created February 21, 2013 22:32
breathFirstSearch
@tailrec
final def breathFirstSearch(queue: List[(State, List[Move])], explored: Set[State], sol: List[(State, List[Move])]): List[(State, List[Move])] =
queue match {
case Nil => sol
case (state, history) :: xs =>
if (!explored(state)) {
breathFirstSearch(xs ++ Move.availableStates(state, moves, history, explored), explored + state, (state, history) :: sol)
} else {
breathFirstSearch(xs, explored, sol)
}
@sofoklis
sofoklis / topological2.cpp
Created March 19, 2013 18:32
topological search
#include <iostream>
#include <bitset>
using namespace std;
bool fastHasIncomingArrows(bool graph[20][20], int size, bitset<100> usedNodes, int node)
{
bool found = false;
for(int j = 0; !found && j < size; j++)
{
if(!usedNodes.test(j))
val s = Right(1)
val l = Left("Error")
val seq: Seq[(Int, Either[String, Int])] = Seq((2, s), (3, l))
for {
(id, Right(res)) <- seq
} yield (id, res)
for {