87 lines
2.3 KiB
C#
87 lines
2.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace _2017_01
|
|
{
|
|
class Program
|
|
{
|
|
static void Main(string[] args)
|
|
{
|
|
var Indata = File.ReadAllText(@"..\..\..\Data\Adventofcode_171201\data171201.txt");
|
|
//var Indata = "1212";
|
|
|
|
|
|
MethodOne(Indata);
|
|
MethodTwo(Indata);
|
|
|
|
}
|
|
|
|
private static void MethodOne(string Indata)
|
|
{
|
|
int value = 0;
|
|
char lastFig = ' ';
|
|
char firstFig = ' ';
|
|
foreach (var figure in Indata.ToCharArray())
|
|
{
|
|
if (lastFig == ' ')
|
|
{
|
|
firstFig = figure;
|
|
}
|
|
if (figure == lastFig)
|
|
{
|
|
value += int.Parse(figure.ToString());
|
|
}
|
|
lastFig = figure;
|
|
}
|
|
|
|
if (firstFig == lastFig)
|
|
{
|
|
value += int.Parse(lastFig.ToString());
|
|
}
|
|
|
|
System.Console.WriteLine("--Method 1--");
|
|
System.Console.WriteLine($"---{value}---");
|
|
System.Console.WriteLine("----------------");
|
|
}
|
|
|
|
private static void MethodTwo(string Indata)
|
|
{
|
|
int value = 0;
|
|
// char lastFig = ' ';
|
|
// char firstFig = ' ';
|
|
int locPos = 0;
|
|
char[] characters = Indata.ToCharArray();
|
|
foreach (var figure in characters)
|
|
{
|
|
if (figure == jmfChar(characters,locPos))
|
|
{
|
|
value += int.Parse(figure.ToString());
|
|
}
|
|
locPos++;
|
|
}
|
|
|
|
System.Console.WriteLine("--Method 2--");
|
|
System.Console.WriteLine($"---{value}---");
|
|
System.Console.WriteLine("----------------");
|
|
}
|
|
|
|
private static char jmfChar(char[] ind, int pos)
|
|
{
|
|
char returnChr = ' ';
|
|
int chars = ind.Length / 2;
|
|
if (pos + chars > ind.Length - 1)
|
|
{
|
|
returnChr = ind[pos + chars - ind.Length];
|
|
}
|
|
else returnChr = ind[pos + chars ];
|
|
|
|
return returnChr;
|
|
}
|
|
|
|
}
|
|
}
|