Skip to content

Instantly share code, notes, and snippets.

@henrik-ch
Created May 30, 2012 06:04
Show Gist options
  • Save henrik-ch/2834038 to your computer and use it in GitHub Desktop.
Save henrik-ch/2834038 to your computer and use it in GitHub Desktop.
solution to the heathrow route planning problem from learnyousomeerlang.com
using System;
using System.Collections.Generic;
using System.Linq;
namespace CSHeathrow
{
class Program
{
static void Main(string[] args)
{
var init = new RoutePair(0, new List<string>(), 0, new List<string>() );
Functions.CalcNewRoutePair(init, 50, 10, 30);
var moveSteps = new List<MoveStep>
{
new MoveStep(50, 10, 30),
new MoveStep(5, 90, 20),
new MoveStep(40, 2, 25),
new MoveStep(10, 8, 0)
};
var myResult = moveSteps.Aggregate(init,
(curState, curEntry) =>
Functions.CalcNewRoutePair(curState, curEntry.AFwd, curEntry.BFwd, curEntry.XCross));
Console.WriteLine("A Value: {0}", myResult.AVal);
Console.WriteLine("A Path: ");
foreach (var str in myResult.AMoves)
{
Console.WriteLine(str);
}
Console.WriteLine();
Console.WriteLine("B Value: {0}", myResult.BVal);
Console.WriteLine("B Path: ");
foreach (var str in myResult.BMoves)
{
Console.WriteLine(str);
}
Console.WriteLine();
}
}
class MoveStep
{
public int AFwd { get; private set; }
public int BFwd { get; private set; }
public int XCross { get; private set; }
public MoveStep(int aFwd, int bFwd, int xCross)
{
this.AFwd = aFwd;
this.BFwd = bFwd;
this.XCross = xCross;
}
}
class RoutePair
{
public int AVal { get; private set ; }
public List<string> AMoves { get; private set; }
public int BVal { get; private set; }
public List<string> BMoves { get; private set; }
public RoutePair(int aVal, List<string> aMoves, int bVal, List<string> bMoves)
{
this.AVal = aVal;
this.AMoves = aMoves;
this.BVal = bVal;
this.BMoves = bMoves;
}
}
class Functions
{
public static RoutePair CalcNewRoutePair(RoutePair currentPair, int aVal, int bVal, int xCrossVal)
{
var valToAStraightFromA = currentPair.AVal + aVal;
var valToACrossFromB = currentPair.BVal + bVal + xCrossVal;
var newAVal = Math.Min(valToAStraightFromA, valToACrossFromB);
List<string> newAMoves;
if(valToAStraightFromA < valToACrossFromB)
{
newAMoves = new List<string>(currentPair.AMoves);
newAMoves.Add("AFwd");
}
else
{
newAMoves = new List<string>(currentPair.BMoves);
newAMoves.Add("BFwd");
newAMoves.Add("XCross");
}
//----------------------------------------------
var valToBStraightFromB = currentPair.BVal + bVal;
var valToBCrossFromA = currentPair.AVal + aVal + xCrossVal;
var newBVal = Math.Min(valToBStraightFromB, valToBCrossFromA);
List<string> newBMoves;
if (valToBStraightFromB < valToBCrossFromA)
{
newBMoves = new List<string>(currentPair.BMoves);
newBMoves.Add("BFwd");
}
else
{
newBMoves = new List<string>(currentPair.AMoves);
newBMoves.Add("AFwd");
newBMoves.Add("XCross");
}
return new RoutePair(newAVal, newAMoves, newBVal, newBMoves);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment