Fungerar med exemplet + ett egenkonstruerat exempel

This commit is contained in:
2019-01-03 21:13:11 +01:00
parent 66d0641376
commit c2fe917110
5 changed files with 523 additions and 144 deletions

View File

@ -1,4 +1,7 @@
using System.Collections.Generic; using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using static System.Console; using static System.Console;
namespace _2018_08 namespace _2018_08
@ -9,9 +12,9 @@ namespace _2018_08
{ {
bool own = false; bool own = false;
int y = 0; int y = 0;
//string[] resTab = File.ReadAllText(@"..\..\..\Data\Adventofcode_181208\2018_08_data.txt").Split(' '); 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 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; //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]; int[] numbers = new int[resTab.Length];
for (int i = 0; i < resTab.Length; i++) for (int i = 0; i < resTab.Length; i++)
{ {
@ -35,7 +38,7 @@ namespace _2018_08
ReadKey(); ReadKey();
var pgm = new Program(); var pgm = new Program();
//int x = pgm.CalculateMetaData(numbers);
int x = pgm.CalcMetaData(numbers); int x = pgm.CalcMetaData(numbers);
WriteLine(); WriteLine();
@ -43,147 +46,36 @@ namespace _2018_08
ReadKey(); ReadKey();
} }
#region oldCode
//private int CalculateMetaData(int[] numbers)
//{
// int tempSum = 0;
// int omgStart = 0;
// LinkedList<TreeNode> llT = new LinkedList<TreeNode>();
// while (omgStart < numbers.Length)
// {
// TreeNode strtNode = new TreeNode(omgStart, 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);
// }
// }
// Write($"start: {strtNode.HeaderStart}, init. end: {strtNode.NodeEnd}, ant. Metas: {strtNode.AntMetaEntries}, ");
// strtNode.PrepMetatab(headEnd - strtNode.NodeEnd + strtNode.AntMetaEntries);
// WriteLine($" fakt. slut {strtNode.NodeEnd}");
// omgStart = strtNode.NodeEnd;
// if (omgStart < numbers.Length)
// {
// for (int i = omgStart; i < omgStart + 5; i++)
// {
// Write($"numbers[{i}]:{numbers[i]}, ");
// }
// WriteLine();
// Write(" ");
// }
// }
// WriteLine();
// foreach (var ll in llT)
// {
// Write($"Obj Nr: {ll.ObjNr} ({ll.HeaderStart},{ll.NodeEnd});");
// foreach (int x in ll.MetaTab)
// {
// Write($"{x}, ");
// tempSum += x;
// }
// WriteLine();
// }
// return tempSum;
//}
//private int SubProc(TreeNode tn, int[] numbers, LinkedList<TreeNode> ll)
//{
// ll.AddLast(tn);
// int nEnd = 0;
// int scndAnt = tn.AntChilds;
// if (scndAnt > 0)
// {
// var tnx = new TreeNode(tn.HeaderStart + 2, numbers);
// for (int k = 0; k < scndAnt; k++)
// {
// var xs = SubProc(tnx, numbers, ll);
// WriteLine($"-- tillbaka tnx obj: {tnx.ObjNr} nEnd: {nEnd} svnAnt: {scndAnt} tn obj: {tn.ObjNr}");
// nEnd = xs - tnx.HeaderStart;
// if (k < scndAnt - 1)
// {
// //tnx = new TreeNode(nEnd, numbers);
// tnx = new TreeNode(tnx.NodeEnd, numbers);
// }
// else
// {
// if (scndAnt > 1) {
// nEnd = xs;
// }
// //else
// //{
// // nEnd = xs - tn.NodeEnd;
// //}
// }
// }
// }
// tn.PrepMetatab(nEnd);
// return tn.NodeEnd;
//}
#endregion
private int CalcMetaData(int[] nums) private int CalcMetaData(int[] nums)
{ {
int metaSum = 0; int metaSum = 0;
int last_pos = 0; TreeNode tnStart = new TreeNode(nums, null);
LinkedList<TreeNode> ll = new LinkedList<TreeNode>(); TreeNode tnTmp = tnStart;
TreeNode tnStart = new TreeNode(0, nums, ll); TreeNode tnAkt = tnStart;
while (true)
{
tnAkt = new TreeNode(nums, tnTmp);
tnTmp = tnAkt;
if (tnAkt.LC.Level < 0)
break;
}
last_pos = diverge(tnStart, ll); ReadKey();
tnTmp = tnStart;
while (tnTmp!=null)
{
tnTmp.PrepMetatab(tnTmp.NodeEnd);
WriteLine(tnTmp.ToString());
metaSum += tnTmp.MetaValueSum();
tnTmp = tnTmp.Next;
}
WriteLine($" last_pos= {last_pos}");
ReadKey(); ReadKey();
return metaSum; return metaSum;
} }
private int diverge(TreeNode tn, LinkedList<TreeNode> lnk)
{
int last_pos = 0;
int[] workTable = tn.NumberTab;
int level_amount = tn.AntChilds;
int level_akt_ch = 0;
if (level_amount > 0)
{
var tnx = new TreeNode(tn.HeaderStart + 2, workTable, lnk);
last_pos = tnx.NodeEnd;
last_pos += diverge(tnx, lnk);
tnx.PrepMetatab( last_pos);
}
if (level_akt_ch < level_amount)
{
level_akt_ch++;
var tny = new TreeNode(last_pos, workTable, lnk);
last_pos = tny.NodeEnd;
last_pos += diverge(tny, lnk);
tny.PrepMetatab(tny.NodeEnd + last_pos);
}
tn.PrepMetatab(tn.AntMetaEntries + last_pos);
last_pos = tn.NodeEnd;
return last_pos;
}
} }
public class TreeNode public class TreeNode
@ -191,24 +83,149 @@ namespace _2018_08
public int ObjNr { get; set; } public int ObjNr { get; set; }
public int HeaderStart { get; set; } public int HeaderStart { get; set; }
public int AntChilds { get; set; } public int AntChilds { get; set; }
public int ChildNo { get; set; }
public int AntMetaEntries { get; set; } public int AntMetaEntries { get; set; }
public int[] MetaTab { get; set; } public int[] MetaTab { get; set; }
public int NodeEnd { get; set; } public int NodeEnd { get; set; }
public int[] NumberTab { 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; static int nr;
public TreeNode(int treeNodeStart, int[] NumTab, LinkedList<TreeNode> links) int treeNodeStart = 0;
public TreeNode(int[] NumTab, TreeNode tnPrev)
{ {
TreeNode.nr++; TreeNode.nr++;
ObjNr = TreeNode.nr; ObjNr = TreeNode.nr;
NumberTab = NumTab; NumberTab = NumTab;
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; HeaderStart = treeNodeStart;
AntChilds = NumberTab[HeaderStart]; AntChilds = NumberTab[HeaderStart];
AntMetaEntries = NumberTab[HeaderStart + 1]; AntMetaEntries = NumberTab[HeaderStart + 1];
NodeEnd = HeaderStart + 2 + AntMetaEntries; NodeEnd = HeaderStart + 2 + AntMetaEntries;
links.AddLast(this);
WriteLine(this.ToString()); 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) public void PrepMetatab(int subLen)
{ {
NodeEnd = subLen; NodeEnd = subLen;
@ -219,10 +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() public override string ToString()
{ {
string retStr = $"Nr: {ObjNr}, Start: {HeaderStart}, Subobj: {AntChilds}, Metafält: {AntMetaEntries}, Nodend: {NodeEnd}"; 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; return retStr;
} }
}
public class LevelCount
{
public int Level { get; set; }
public int Nodes { get; set; }
public LevelCount()
{
Level = 0;
Nodes = 0;
}
} }
} }

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>