Nära lösning dag 8
This commit is contained in:
101
2018_08/Program.cs
Normal file
101
2018_08/Program.cs
Normal file
@ -0,0 +1,101 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using static System.Console;
|
||||
|
||||
namespace _2018_08
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
//string[] resTab = File.ReadAllText(@"..\..\..\Data\Adventofcode_181208\2018_08_data.txt").Split(' ');
|
||||
string[] resTab = @"2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2".Split(' ');
|
||||
int[] numbers = new int[resTab.Length];
|
||||
for (int i = 0; i < resTab.Length; i++)
|
||||
{
|
||||
numbers[i] = int.Parse(resTab[i]);
|
||||
}
|
||||
WriteLine($"Antal värden = {resTab.Length}");
|
||||
ReadKey();
|
||||
|
||||
var pgm = new Program();
|
||||
int x = pgm.CalculateMetaData(numbers);
|
||||
|
||||
}
|
||||
|
||||
private int CalculateMetaData(int[] numbers)
|
||||
{
|
||||
int tempSum = 0;
|
||||
LinkedList<TreeNode> llT = new LinkedList<TreeNode>();
|
||||
TreeNode strtNode = new TreeNode(0, numbers);
|
||||
llT.AddLast(strtNode);
|
||||
int headEnd = 0;
|
||||
int firstAnt = strtNode.AntChilds;
|
||||
|
||||
TreeNode nxt = new TreeNode(strtNode.HeaderStart+2, numbers);
|
||||
|
||||
for (int i = 0; i < firstAnt; i++)
|
||||
{
|
||||
headEnd = this.SubProc(nxt, numbers, llT);
|
||||
if (i < firstAnt - 1)
|
||||
{
|
||||
nxt = new TreeNode(headEnd , numbers);
|
||||
llT.AddLast(nxt);
|
||||
}
|
||||
}
|
||||
|
||||
return tempSum;
|
||||
}
|
||||
|
||||
private int SubProc(TreeNode tn, int[] numbers, LinkedList<TreeNode> ll)
|
||||
{
|
||||
int nEnd = 0;
|
||||
|
||||
if (tn.AntChilds > 0)
|
||||
{
|
||||
var tnx = new TreeNode(tn.HeaderStart + 2, numbers);
|
||||
ll.AddLast(tnx);
|
||||
nEnd = SubProc(tnx, numbers, ll);
|
||||
|
||||
}
|
||||
|
||||
tn.PrepMetatab(nEnd);
|
||||
return tn.NodeEnd;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class TreeNode
|
||||
{
|
||||
public int HeaderStart { get; set; }
|
||||
public int AntChilds { get; set; }
|
||||
public int AntMetaEntries { get; set; }
|
||||
public int[] MetaTab { get; set; }
|
||||
public int NodeEnd { get; set; }
|
||||
public int[] NumberTab { get; set; }
|
||||
|
||||
public TreeNode(int treeNodeStart, int[] NumTab)
|
||||
{
|
||||
NumberTab = NumTab;
|
||||
HeaderStart = treeNodeStart;
|
||||
AntChilds = NumberTab[HeaderStart];
|
||||
AntMetaEntries = NumberTab[HeaderStart + 1];
|
||||
NodeEnd = HeaderStart + 2 + AntMetaEntries;
|
||||
}
|
||||
|
||||
public void PrepMetatab(int subLen)
|
||||
{
|
||||
NodeEnd += subLen;
|
||||
MetaTab = new int[AntMetaEntries];
|
||||
for (int i = 0, j = NodeEnd - AntMetaEntries; j < NodeEnd; j++, i++)
|
||||
{
|
||||
MetaTab[i] = NumberTab[j];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user