Compare commits

...

12 Commits

8 changed files with 553 additions and 48 deletions

View File

@ -35,6 +35,8 @@
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Numerics" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />

View File

@ -35,6 +35,8 @@
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Numerics" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />

View File

@ -1,9 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace _2018_08
@ -12,89 +10,225 @@ namespace _2018_08
{
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(' ');
bool own = false;
int y = 0;
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 2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2".Split(' ');
//string[] resTab = @"2 3 2 1 2 3 1 1 0 2 1 2 3 0 3 4 5 6 7 8 9 0 4 10 11 12 13 14 2 2 0 3 15 16 17 0 2 18 19 20 21 22 23 24".Split(' '); own = true;
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}");
if (own)
{
for (int z = 1; z < 25; z++)
{
y += z;
}
}
Write($"Antal värden = {resTab.Length}");
if (own)
{
WriteLine($" svar = {y}");
}
else WriteLine();
ReadKey();
var pgm = new Program();
int x = pgm.CalculateMetaData(numbers);
int x = pgm.CalcMetaData(numbers);
WriteLine();
WriteLine($"summa = {x}");
ReadKey();
}
private int CalculateMetaData(int[] numbers)
private int CalcMetaData(int[] nums)
{
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++)
int metaSum = 0;
TreeNode tnStart = new TreeNode(nums, null);
TreeNode tnTmp = tnStart;
TreeNode tnAkt = tnStart;
while (true)
{
headEnd = this.SubProc(nxt, numbers, llT);
if (i < firstAnt - 1)
{
nxt = new TreeNode(headEnd, numbers);
llT.AddLast(nxt);
}
tnAkt = new TreeNode(nums, tnTmp);
tnTmp = tnAkt;
if (tnAkt.LC.Level < 0)
break;
}
return tempSum;
}
ReadKey();
private int SubProc(TreeNode tn, int[] numbers, LinkedList<TreeNode> ll)
{
int nEnd = 0;
if (tn.AntChilds > 0)
tnTmp = tnStart;
while (tnTmp!=null)
{
int nAnt = tn.AntChilds;
for (int i = 0; i < nAnt; i++)
{
var tnx = new TreeNode(tn.HeaderStart + 2, numbers);
ll.AddLast(tnx);
nEnd = SubProc(tnx, numbers, ll);
}
//nEnd -= (tnx.HeaderStart + 2);
tnTmp.PrepMetatab(tnTmp.NodeEnd);
WriteLine(tnTmp.ToString());
metaSum += tnTmp.MetaValueSum();
tnTmp = tnTmp.Next;
}
tn.PrepMetatab(nEnd);
return tn.NodeEnd;
ReadKey();
return metaSum;
}
}
public class TreeNode
{
public int ObjNr { get; set; }
public int HeaderStart { get; set; }
public int AntChilds { get; set; }
public int ChildNo { get; set; }
public int AntMetaEntries { get; set; }
public int[] MetaTab { get; set; }
public int NodeEnd { get; set; }
public int[] NumberTab { get; set; }
public TreeNode Previous { get; set; }
public TreeNode Next { get; set; }
public LevelCount LC { get; set; }
static int nr;
int treeNodeStart = 0;
public TreeNode(int treeNodeStart, int[] NumTab)
public TreeNode(int[] NumTab, TreeNode tnPrev)
{
TreeNode.nr++;
ObjNr = TreeNode.nr;
NumberTab = NumTab;
HeaderStart = treeNodeStart;
AntChilds = NumberTab[HeaderStart];
AntMetaEntries = NumberTab[HeaderStart + 1];
NodeEnd = HeaderStart + 2 + AntMetaEntries;
LC = new LevelCount();
Previous = tnPrev;
if (Previous != null)
{
Previous.Next = this;
}
else
{
LC.Level = 0;
treeNodeStart = 0;
}
Next = null;
SetLevel();
if (LC.Level > -1)
{
HeaderStart = treeNodeStart;
AntChilds = NumberTab[HeaderStart];
AntMetaEntries = NumberTab[HeaderStart + 1];
NodeEnd = HeaderStart + 2 + AntMetaEntries;
WriteLine(this.ToString());
}
}
private void SetLevel()
{
TreeNode wrkTmp = this;
if (Previous != null)
{
if (Previous.AntChilds > 0)
{
LC.Level = Previous.LC.Level + 1;
LC.Nodes = Previous.AntChilds;
ChildNo = 1;
treeNodeStart = Previous.HeaderStart + 2;
}
else
{
if (Previous.ChildNo < Previous.LC.Nodes)
{
LC.Level = Previous.LC.Level;
LC.Nodes = Previous.LC.Nodes;
ChildNo = Previous.ChildNo + 1;
treeNodeStart = Previous.NodeEnd;
}
else
{
LC.Level = Previous.LC.Level - 1;
if (FindStartOfObject(wrkTmp).ChildNo < FindStartOfObject(wrkTmp).LC.Nodes)
{
FindStartOfObject(wrkTmp).PrepMetatab(wrkTmp.Previous.NodeEnd + FindStartOfObject(wrkTmp).AntMetaEntries);
treeNodeStart = FindStartOfObject(wrkTmp).NodeEnd;
wrkTmp.ChildNo = FindStartOfObject(wrkTmp).ChildNo + 1;
wrkTmp.LC.Nodes = FindHeadOfStartOfObject(wrkTmp).AntChilds;
}
else
{
int chld = FindStartOfObject(wrkTmp).ChildNo;
int ndss = FindStartOfObject(wrkTmp).LC.Nodes;
treeNodeStart = wrkTmp.Previous.NodeEnd;
while (FindStartOfObject(wrkTmp).ChildNo == FindStartOfObject(wrkTmp).LC.Nodes && LC.Level > -1)
{
FindStartOfObject(wrkTmp).PrepMetatab(treeNodeStart + FindStartOfObject(wrkTmp).AntMetaEntries);
treeNodeStart = FindStartOfObject(wrkTmp).NodeEnd;
LC.Level = LC.Level - 1;
chld = FindStartOfObject(wrkTmp).ChildNo;
ndss = FindStartOfObject(wrkTmp).LC.Nodes;
}
if (LC.Level <= 0)
{
if (treeNodeStart >= NumberTab.Length)
{
Previous.Next = null;
}
else
{
LC.Level = 0;
}
}
}
}
}
}
}
private TreeNode FindStartOfObject(TreeNode xTree)
{
TreeNode jmfTmp = this;
TreeNode srchTmp = Previous;
if (xTree != null)
{
jmfTmp = xTree;
srchTmp = xTree.Previous;
}
while (srchTmp.LC.Level > jmfTmp.LC.Level && srchTmp.Previous != null)
{
srchTmp = srchTmp.Previous;
}
return srchTmp;
}
private TreeNode FindHeadOfStartOfObject(TreeNode xTree)
{
TreeNode jmfTmp = this;
TreeNode srchTmp = Previous;
if (xTree != null)
{
jmfTmp = xTree;
srchTmp = xTree.Previous;
}
while (srchTmp.LC.Level >= this.LC.Level && srchTmp.Previous != null)
{
srchTmp = srchTmp.Previous;
}
return srchTmp;
}
public void PrepMetatab(int subLen)
{
NodeEnd += subLen;
NodeEnd = subLen;
MetaTab = new int[AntMetaEntries];
for (int i = 0, j = NodeEnd - AntMetaEntries; j < NodeEnd; j++, i++)
{
@ -102,5 +236,43 @@ namespace _2018_08
}
}
public int MetaValueSum()
{
int tmpMetas = 0;
if (MetaTab != null)
{
for (int y = 0; y < AntMetaEntries; y++)
{
tmpMetas += MetaTab[y];
}
}
else tmpMetas = -1;
return tmpMetas;
}
public override string ToString()
{
string retStr = $"Nr: {ObjNr}, Level: {LC.Level}, Child: {ChildNo} Start: {HeaderStart}, Subobj: {AntChilds}, Metafält: {AntMetaEntries}, Nodend: {NodeEnd}";
if (MetaTab != null)
{
for (int x = 0; x < AntMetaEntries; x++)
{
retStr += $" meta[{x}]: {MetaTab[x]}";
}
}
return retStr;
}
}
public class LevelCount
{
public int Level { get; set; }
public int Nodes { get; set; }
public LevelCount()
{
Level = 0;
Nodes = 0;
}
}
}

Binary file not shown.

6
TestProgram/App.config Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>

234
TestProgram/Program.cs Normal file
View File

@ -0,0 +1,234 @@
using System;
using System.Text;
using System.Collections.Generic;
namespace TestProgram
{
public class Example
{
public static void Main()
{
// Create the link list.
string[] words =
{ "the", "fox", "jumps", "over", "the", "dog" };
LinkedList<string> sentence = new LinkedList<string>(words);
Display(sentence, "The linked list values:");
Console.WriteLine("sentence.Contains(\"jumps\") = {0}",
sentence.Contains("jumps"));
// Add the word 'today' to the beginning of the linked list.
sentence.AddFirst("today");
Display(sentence, "Test 1: Add 'today' to beginning of the list:");
// Move the first node to be the last node.
LinkedListNode<string> mark1 = sentence.First;
sentence.RemoveFirst();
sentence.AddLast(mark1);
Display(sentence, "Test 2: Move first node to be last node:");
// Change the last node to 'yesterday'.
sentence.RemoveLast();
sentence.AddLast("yesterday");
Display(sentence, "Test 3: Change the last node to 'yesterday':");
// Move the last node to be the first node.
mark1 = sentence.Last;
sentence.RemoveLast();
sentence.AddFirst(mark1);
Display(sentence, "Test 4: Move last node to be first node:");
// Indicate the last occurence of 'the'.
sentence.RemoveFirst();
LinkedListNode<string> current = sentence.FindLast("the");
IndicateNode(current, "Test 5: Indicate last occurence of 'the':");
// Add 'lazy' and 'old' after 'the' (the LinkedListNode named current).
sentence.AddAfter(current, "old");
sentence.AddAfter(current, "lazy");
IndicateNode(current, "Test 6: Add 'lazy' and 'old' after 'the':");
// Indicate 'fox' node.
current = sentence.Find("fox");
IndicateNode(current, "Test 7: Indicate the 'fox' node:");
// Add 'quick' and 'brown' before 'fox':
sentence.AddBefore(current, "quick");
sentence.AddBefore(current, "brown");
IndicateNode(current, "Test 8: Add 'quick' and 'brown' before 'fox':");
// Keep a reference to the current node, 'fox',
// and to the previous node in the list. Indicate the 'dog' node.
mark1 = current;
LinkedListNode<string> mark2 = current.Previous;
current = sentence.Find("dog");
IndicateNode(current, "Test 9: Indicate the 'dog' node:");
// The AddBefore method throws an InvalidOperationException
// if you try to add a node that already belongs to a list.
Console.WriteLine("Test 10: Throw exception by adding node (fox) already in the list:");
try
{
sentence.AddBefore(current, mark1);
}
catch (InvalidOperationException ex)
{
Console.WriteLine("Exception message: {0}", ex.Message);
}
Console.WriteLine();
// Remove the node referred to by mark1, and then add it
// before the node referred to by current.
// Indicate the node referred to by current.
sentence.Remove(mark1);
sentence.AddBefore(current, mark1);
IndicateNode(current, "Test 11: Move a referenced node (fox) before the current node (dog):");
// Remove the node referred to by current.
sentence.Remove(current);
IndicateNode(current, "Test 12: Remove current node (dog) and attempt to indicate it:");
// Add the node after the node referred to by mark2.
sentence.AddAfter(mark2, current);
IndicateNode(current, "Test 13: Add node removed in test 11 after a referenced node (brown):");
// The Remove method finds and removes the
// first node that that has the specified value.
sentence.Remove("old");
Display(sentence, "Test 14: Remove node that has the value 'old':");
// When the linked list is cast to ICollection(Of String),
// the Add method adds a node to the end of the list.
sentence.RemoveLast();
ICollection<string> icoll = sentence;
icoll.Add("rhinoceros");
Display(sentence, "Test 15: Remove last node, cast to ICollection, and add 'rhinoceros':");
Console.WriteLine("Test 16: Copy the list to an array:");
// Create an array with the same number of
// elements as the inked list.
string[] sArray = new string[sentence.Count];
sentence.CopyTo(sArray, 0);
foreach (string s in sArray)
{
Console.WriteLine(s);
}
// Release all the nodes.
sentence.Clear();
Console.WriteLine();
Console.WriteLine("Test 17: Clear linked list. Contains 'jumps' = {0}",
sentence.Contains("jumps"));
Console.ReadLine();
}
private static void Display(LinkedList<string> words, string test)
{
Console.WriteLine(test);
foreach (string word in words)
{
Console.Write(word + " ");
}
Console.WriteLine();
Console.WriteLine();
}
private static void IndicateNode(LinkedListNode<string> node, string test)
{
Console.WriteLine(test);
if (node.List == null)
{
Console.WriteLine("Node '{0}' is not in the list.\n",
node.Value);
return;
}
StringBuilder result = new StringBuilder("(" + node.Value + ")");
LinkedListNode<string> nodeP = node.Previous;
while (nodeP != null)
{
result.Insert(0, nodeP.Value + " ");
nodeP = nodeP.Previous;
}
node = node.Next;
while (node != null)
{
result.Append(" " + node.Value);
node = node.Next;
}
Console.WriteLine(result);
Console.WriteLine();
}
}
//This code example produces the following output:
//
//The linked list values:
//the fox jumps over the dog
//Test 1: Add 'today' to beginning of the list:
//today the fox jumps over the dog
//Test 2: Move first node to be last node:
//the fox jumps over the dog today
//Test 3: Change the last node to 'yesterday':
//the fox jumps over the dog yesterday
//Test 4: Move last node to be first node:
//yesterday the fox jumps over the dog
//Test 5: Indicate last occurence of 'the':
//the fox jumps over (the) dog
//Test 6: Add 'lazy' and 'old' after 'the':
//the fox jumps over (the) lazy old dog
//Test 7: Indicate the 'fox' node:
//the (fox) jumps over the lazy old dog
//Test 8: Add 'quick' and 'brown' before 'fox':
//the quick brown (fox) jumps over the lazy old dog
//Test 9: Indicate the 'dog' node:
//the quick brown fox jumps over the lazy old (dog)
//Test 10: Throw exception by adding node (fox) already in the list:
//Exception message: The LinkedList node belongs a LinkedList.
//Test 11: Move a referenced node (fox) before the current node (dog):
//the quick brown jumps over the lazy old fox (dog)
//Test 12: Remove current node (dog) and attempt to indicate it:
//Node 'dog' is not in the list.
//Test 13: Add node removed in test 11 after a referenced node (brown):
//the quick brown (dog) jumps over the lazy old fox
//Test 14: Remove node that has the value 'old':
//the quick brown dog jumps over the lazy fox
//Test 15: Remove last node, cast to ICollection, and add 'rhinoceros':
//the quick brown dog jumps over the lazy rhinoceros
//Test 16: Copy the list to an array:
//the
//quick
//brown
//dog
//jumps
//over
//the
//lazy
//rhinoceros
//Test 17: Clear linked list. Contains 'jumps' = False
//
}

View File

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("TestProgram")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("TestProgram")]
[assembly: AssemblyCopyright("Copyright © 2019")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("3c619b27-1f67-4151-bfd2-af0dec322297")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -0,0 +1,53 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{3C619B27-1F67-4151-BFD2-AF0DEC322297}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>TestProgram</RootNamespace>
<AssemblyName>TestProgram</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>