Skip to content
Permalink
Browse files

Implemented C# .NET solution for day 9

  • Loading branch information...
SirCAS committed Dec 17, 2016
1 parent a9c465f commit 396fcc43ef31d41bd3f67964c72f1a4c26a06756
@@ -0,0 +1,23 @@
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceRoot}/bin/Debug/netcoreapp1.0/DotNet.dll",
"args": [],
"cwd": "${workspaceRoot}",
"externalConsole": false,
"stopAtEntry": false,
"internalConsoleOptions": "openOnSessionStart"
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command.pickProcess}"
}
]
}
@@ -0,0 +1,16 @@
{
"version": "0.1.0",
"command": "dotnet",
"isShellCommand": true,
"args": [],
"tasks": [
{
"taskName": "build",
"args": [
"${workspaceRoot}/project.json"
],
"isBuildCommand": true,
"problemMatcher": "$msCompile"
}
]
}
@@ -0,0 +1,54 @@
using System.Text;
using System.Text.RegularExpressions;

namespace Day09
{
public class DecompressorVersion1
{
private string input;
private string output;
private int length;
public DecompressorVersion1(string input)
{
var markers = new Regex(@"(\(\d+x\d+\))+").Matches(input);

int markerPos = 0;
StringBuilder decoded = new StringBuilder();

foreach(Match m in markers)
{
if(m.Index < markerPos) continue;

var before = input.Substring(markerPos, m.Index - markerPos);
decoded.Append(before);
markerPos = m.Index;

var markerEnd = m.Value.IndexOf(')');
var splitPos = m.Value.IndexOf('x');

var len = int.Parse(m.Value.Substring(1, splitPos - 1));
var repeat = int.Parse(m.Value.Substring(splitPos + 1, markerEnd - splitPos - 1));

var after = input.Substring(markerPos + markerEnd + 1, len);

for(int x=0; x<repeat; ++x)
{
decoded.Append(after);
}

markerPos += len + markerEnd + 1;
}

var remaining = input.Substring(markerPos);
decoded.Append(remaining);

this.input = input;
this.output = decoded.ToString();
this.length = output.Length;
}

public string Input { get { return input; } }
public string Output { get { return output; } }
public int Length { get { return length; } }
}
}
@@ -0,0 +1,93 @@

using NUnit.Framework;

namespace Day09
{
[TestFixture]
public class DecompressorVersion1Tests
{
[Test]
public void NoMarkers_Test()
{
// 1. Arrange
const string input = "ADVENT";

// 2. Act
var decompressor = new DecompressorVersion1(input);

// 3. Assert
Assert.AreEqual("ADVENT", decompressor.Output);
Assert.AreEqual(6, decompressor.Length);
}

[Test]
public void SampleInput1_Test()
{
// 1. Arrange
const string input = "A(1x5)BC";

// 2. Act
var decompressor = new DecompressorVersion1(input);

// 3. Assert
Assert.AreEqual("ABBBBBC", decompressor.Output);
Assert.AreEqual(7, decompressor.Length);
}

[Test]
public void SampleInput2_Test()
{
// 1. Arrange
const string input = "(3x3)XYZ";

// 2. Act
var decompressor = new DecompressorVersion1(input);

// 3. Assert
Assert.AreEqual("XYZXYZXYZ", decompressor.Output);
Assert.AreEqual(9, decompressor.Length);
}

[Test]
public void SampleInput3_Test()
{
// 1. Arrange
const string input = "A(2x2)BCD(2x2)EFG";

// 2. Act
var decompressor = new DecompressorVersion1(input);

// 3. Assert
Assert.AreEqual("ABCBCDEFEFG", decompressor.Output);
Assert.AreEqual(11, decompressor.Length);
}

[Test]
public void SampleInput4_Test()
{
// 1. Arrange
const string input = "(6x1)(1x3)A";

// 2. Act
var decompressor = new DecompressorVersion1(input);

// 3. Assert
Assert.AreEqual("(1x3)A", decompressor.Output);
Assert.AreEqual(6, decompressor.Length);
}

[Test]
public void SampleInput5_Test()
{
// 1. Arrange
const string input = "X(8x2)(3x3)ABCY";

// 2. Act
var decompressor = new DecompressorVersion1(input);

// 3. Assert
Assert.AreEqual("X(3x3)ABC(3x3)ABCY", decompressor.Output);
Assert.AreEqual(18, decompressor.Length);
}
}
}
@@ -0,0 +1,54 @@
using System.Text.RegularExpressions;

namespace Day09
{
public class DecompressorVersion2
{
private string input;
private ulong length;
public DecompressorVersion2(string input)
{
this.input = input;
this.length = DecompressChunck(input);
}

private ulong DecompressChunck(string input)
{
var markers = new Regex(@"(\(\d+x\d+\))+").Matches(input);

int markerPos = 0;
ulong result = 0;

foreach(Match m in markers)
{
if(m.Index < markerPos) continue;

var before = input.Substring(markerPos, m.Index - markerPos);
result += (ulong) before.Length;
markerPos = m.Index;

var markerEnd = m.Value.IndexOf(')');
var splitPos = m.Value.IndexOf('x');

var len = int.Parse(m.Value.Substring(1, splitPos - 1));
var repeat = int.Parse(m.Value.Substring(splitPos + 1, markerEnd - splitPos - 1));

var after = input.Substring(markerPos + markerEnd + 1, len);
var test = DecompressChunck(after);

for(int x=0; x<repeat; ++x)
{
result += test;
}

markerPos += len + markerEnd + 1;
}

result += (ulong) input.Substring(markerPos).Length;

return result;
}

public ulong Length { get { return length; } }
}
}
@@ -0,0 +1,74 @@

using NUnit.Framework;

namespace Day09
{
[TestFixture]
public class DecompressorVersion2Tests
{
[Test]
public void NoMarkers_Test()
{
// 1. Arrange
const string input = "ADVENT";

// 2. Act
var decompressor = new DecompressorVersion2(input);

// 3. Assert
Assert.AreEqual(6, decompressor.Length);
}

[Test]
public void SampleInput1_Test()
{
// 1. Arrange
const string input = "(3x3)XYZ";

// 2. Act
var decompressor = new DecompressorVersion2(input);

// 3. Assert
Assert.AreEqual(9, decompressor.Length);
}

[Test]
public void SampleInput2_Test()
{
// 1. Arrange
const string input = "X(8x2)(3x3)ABCY";

// 2. Act
var decompressor = new DecompressorVersion2(input);

// 3. Assert
Assert.AreEqual(20, decompressor.Length);
}

[Test]
public void SampleInput3_Test()
{
// 1. Arrange
const string input = "(27x12)(20x12)(13x14)(7x10)(1x12)A";

// 2. Act
var decompressor = new DecompressorVersion2(input);

// 3. Assert
Assert.AreEqual(241920, decompressor.Length);
}

[Test]
public void SampleInput4_Test()
{
// 1. Arrange
const string input = "(25x3)(3x3)ABC(2x3)XY(5x2)PQRSTX(18x9)(3x2)TWO(5x7)SEVEN";

// 2. Act
var decompressor = new DecompressorVersion2(input);

// 3. Assert
Assert.AreEqual(445, decompressor.Length);
}
}
}
@@ -0,0 +1,28 @@
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace Day09
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("+-------------------------+");
Console.WriteLine("| Advent of Code - Day 09 |");
Console.WriteLine("+-------------------------+");

var input = File.ReadAllText("input.txt");

var decompressor1 = new DecompressorVersion1(input);
var decompressor2 = new DecompressorVersion2(input);

Console.WriteLine($"Length of decompressed v1 string is {decompressor1.Length}");
Console.WriteLine($"Length of decompressed v2 string is {decompressor2.Length}");

Console.WriteLine($" -Glædelig jul!");
}
}
}

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -0,0 +1,25 @@
{
"version": "1.0.0-*",

"buildOptions": {
"debugType": "portable",
"emitEntryPoint": true
},
"dependencies": {
"Newtonsoft.Json": "9.0.1",
"NUnit": "3.4.1",
"dotnet-test-nunit": "3.4.0-beta-2"
},
"testRunner": "nunit",
"frameworks": {
"netcoreapp1.0": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
}
},
"imports": "dnxcore50"
}
}
}

0 comments on commit 396fcc4

Please sign in to comment.
You can’t perform that action at this time.