Last active
August 29, 2015 14:14
-
-
Save jmcd/f3afed6a7602ee05f6f0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using NUnit.Framework; | |
namespace DIrtyWars | |
{ | |
[TestFixture] | |
public class TestFixture | |
{ | |
[Test] | |
public void Test() | |
{ | |
const string input = @"5 5 | |
1 2 N | |
LMLMLMLMM | |
3 3 E | |
MMRMMRMRRM"; | |
var lines = input.Split(new[] {'\n', '\r'}, StringSplitOptions.RemoveEmptyEntries); | |
Action<Action<int>> x2 = a =>{ a(0); a(1); }; | |
Func<IEnumerable<string>, int[]> atois = ss => ss.Select(int.Parse).ToArray(); | |
var bs = new[] {'N', 0, 1, 'E', 1, 0, 'S', 0, -1, 'W', -1, 0}; | |
var ds = new[] {'M', 0, 'L', -1, 'R', 1}; | |
var mx = atois(lines[0].Split(' ')); | |
for (var i = 1; i < lines.Length; i += 2) | |
{ | |
var l0 = lines[i].Split(' '); | |
var p = atois(l0.Take(2)); | |
var b = Array.IndexOf(bs, l0[2][0]); | |
foreach (var di in lines[i + 1].Select(c => Array.IndexOf(ds, c))) | |
{ | |
x2(d => p[d] += bs[b + 1 + d]*(di == 0 ? 1 : 0)); | |
b += ds[di + 1]*3; | |
b = b < 0 ? bs.Length - 3 : b == bs.Length ? 0 : b; | |
x2(d => p[d] = Math.Max(0, Math.Min(mx[d], p[d]))); | |
} | |
Console.WriteLine("{0} {1} {2}", p[0], p[1], (char) bs[b]); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment