From c2fe917110609487bebd31675d3c75e8cadbd055 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommy=20=C3=96man?= Date: Thu, 3 Jan 2019 21:13:11 +0100 Subject: [PATCH] Fungerar med exemplet + ett egenkonstruerat exempel --- 2018_08/Program.cs | 338 ++++++++++++++----------- TestProgram/App.config | 6 + TestProgram/Program.cs | 234 +++++++++++++++++ TestProgram/Properties/AssemblyInfo.cs | 36 +++ TestProgram/TestProgram.csproj | 53 ++++ 5 files changed, 523 insertions(+), 144 deletions(-) create mode 100644 TestProgram/App.config create mode 100644 TestProgram/Program.cs create mode 100644 TestProgram/Properties/AssemblyInfo.cs create mode 100644 TestProgram/TestProgram.csproj diff --git a/2018_08/Program.cs b/2018_08/Program.cs index 4be4a0c..3012967 100644 --- a/2018_08/Program.cs +++ b/2018_08/Program.cs @@ -1,4 +1,7 @@ -using System.Collections.Generic; +using System; +using System.Collections; +using System.Collections.Generic; +using System.IO; using static System.Console; namespace _2018_08 @@ -9,9 +12,9 @@ namespace _2018_08 { bool own = false; 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 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]; for (int i = 0; i < resTab.Length; i++) { @@ -35,7 +38,7 @@ namespace _2018_08 ReadKey(); var pgm = new Program(); - //int x = pgm.CalculateMetaData(numbers); + int x = pgm.CalcMetaData(numbers); WriteLine(); @@ -43,147 +46,36 @@ namespace _2018_08 ReadKey(); } - #region oldCode - //private int CalculateMetaData(int[] numbers) - //{ - // int tempSum = 0; - // int omgStart = 0; - // LinkedList llT = new LinkedList(); - // 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 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 last_pos = 0; - LinkedList ll = new LinkedList(); - TreeNode tnStart = new TreeNode(0, nums, ll); + TreeNode tnStart = new TreeNode(nums, null); + TreeNode tnTmp = tnStart; + 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(); return metaSum; } - private int diverge(TreeNode tn, LinkedList 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 @@ -191,22 +83,147 @@ namespace _2018_08 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; - public TreeNode(int treeNodeStart, int[] NumTab, LinkedList links) + int treeNodeStart = 0; + + 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; - links.AddLast(this); - WriteLine(this.ToString()); + 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) @@ -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() { - 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; } + + } + + public class LevelCount + { + public int Level { get; set; } + public int Nodes { get; set; } + public LevelCount() + { + Level = 0; + Nodes = 0; + } } } diff --git a/TestProgram/App.config b/TestProgram/App.config new file mode 100644 index 0000000..731f6de --- /dev/null +++ b/TestProgram/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/TestProgram/Program.cs b/TestProgram/Program.cs new file mode 100644 index 0000000..8060f75 --- /dev/null +++ b/TestProgram/Program.cs @@ -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 sentence = new LinkedList(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 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 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 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 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 words, string test) + { + Console.WriteLine(test); + foreach (string word in words) + { + Console.Write(word + " "); + } + Console.WriteLine(); + Console.WriteLine(); + } + + private static void IndicateNode(LinkedListNode 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 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 + // + +} diff --git a/TestProgram/Properties/AssemblyInfo.cs b/TestProgram/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..b68b9d3 --- /dev/null +++ b/TestProgram/Properties/AssemblyInfo.cs @@ -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")] diff --git a/TestProgram/TestProgram.csproj b/TestProgram/TestProgram.csproj new file mode 100644 index 0000000..c451139 --- /dev/null +++ b/TestProgram/TestProgram.csproj @@ -0,0 +1,53 @@ + + + + + Debug + AnyCPU + {3C619B27-1F67-4151-BFD2-AF0DEC322297} + Exe + TestProgram + TestProgram + v4.6.1 + 512 + true + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + \ No newline at end of file