Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
448 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp2.1</TargetFramework> | ||
<RootNamespace>Day2._2.csharp.Tests</RootNamespace> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" /> | ||
<PackageReference Include="MSTest.TestAdapter" Version="1.3.2" /> | ||
<PackageReference Include="MSTest.TestFramework" Version="1.3.2" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Day2.2.csharp\Day2.2.csharp.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System.Collections.Generic; | ||
|
||
namespace Day2._2.csharp.Tests | ||
{ | ||
[TestClass] | ||
public class UnitTest1 | ||
{ | ||
private readonly string[] _testSet = new string[] | ||
{ | ||
"abcde", | ||
"fghij", | ||
"klmno", | ||
"pqrst", | ||
"fguij", | ||
"axcye", | ||
"wvxyz" | ||
}; | ||
|
||
[TestMethod] | ||
public void MainTest() | ||
{ | ||
var result = Day2._2.csharp.Program.Calculate(_testSet); | ||
|
||
Assert.AreEqual("fgij", result); | ||
} | ||
|
||
[TestMethod] | ||
public void TestHelperFunction() | ||
{ | ||
var testString1 = "abcdef"; | ||
var testString2 = "abcqwe"; | ||
var testString3 = "zxcasd"; | ||
|
||
Assert.AreEqual(3, Program.GetStringDistance(testString1, testString2)); | ||
Assert.AreEqual(5, Program.GetStringDistance(testString1, testString3)); | ||
Assert.AreEqual(0, Program.GetStringDistance(testString3, testString3)); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp2.1</TargetFramework> | ||
<RootNamespace>Day2._2.csharp</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Remove="input.txt" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Content Include="input.txt"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</Content> | ||
</ItemGroup> | ||
|
||
</Project> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace Day2._2.csharp | ||
{ | ||
public class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
var input = System.IO.File.ReadAllLines("input.txt"); | ||
|
||
var result = Calculate(input); | ||
|
||
Console.WriteLine(result); | ||
} | ||
|
||
public static string Calculate(string[] inputLines) | ||
{ | ||
(string, string)? searchDistanceResult = null; | ||
|
||
for (var i = 0; i < inputLines.Count(); i++) | ||
{ | ||
searchDistanceResult = GetStringDistance(inputLines[i], inputLines.Skip(i + 1)); | ||
if (searchDistanceResult != null) | ||
{ | ||
break; | ||
} | ||
} | ||
|
||
var resultSb = new StringBuilder(); | ||
|
||
for(var i = 0; i < searchDistanceResult.Value.Item1.Length; i++) | ||
{ | ||
if (searchDistanceResult.Value.Item1[i] == searchDistanceResult.Value.Item2[i]) | ||
{ | ||
resultSb.Append(searchDistanceResult.Value.Item1[i]); | ||
} | ||
} | ||
|
||
return resultSb.ToString(); | ||
} | ||
|
||
private static (string, string)? GetStringDistance(string s1, IEnumerable<string> inputs) | ||
{ | ||
foreach (var input in inputs) | ||
{ | ||
if (GetStringDistance(s1, input) == 1) | ||
{ | ||
return (s1, input); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public static int GetStringDistance(string s1, string s2) | ||
{ | ||
if (s1.Length != s2.Length) | ||
{ | ||
throw new ArgumentException(); | ||
} | ||
|
||
var result = 0; | ||
|
||
for (var i = 0; i < s1.Length; i++) | ||
{ | ||
if (s1[i] != s2[i]) result++; | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
} |
Oops, something went wrong.