Day08 part 1

This commit is contained in:
2018-12-11 21:02:22 +01:00
parent ef84354e66
commit e00c496a95
7 changed files with 184 additions and 2 deletions

View File

@@ -56,6 +56,7 @@
<Otherwise />
</Choose>
<ItemGroup>
<Compile Include="ChronoLicenceNode_Tests.cs" />
<Compile Include="ChronoPoint_Tests.cs" />
<Compile Include="Claim_Tests.cs" />
<Compile Include="Day01_Tests.cs" />
@@ -65,6 +66,7 @@
<Compile Include="Day05_Tests.cs" />
<Compile Include="Day06_Tests.cs" />
<Compile Include="Day07_Tests.cs" />
<Compile Include="Day08_Tests.cs" />
<Compile Include="GuardEvent_Tests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>

View File

@@ -0,0 +1,37 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace AdventOfCode2018.Tests
{
[TestClass()]
public class ChronoLicenceNode_Tests
{
[TestMethod()]
public void BuildFromIntStream__Test()
{
Day08 day = new Day08();
IntStream values = new IntStream("2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2");
ChronoLicenceNode result = ChronoLicenceNode.BuildFromIntStream(values);
Assert.AreEqual(2, result.Childs.Count);
Assert.AreEqual(3, result.Metadata.Count);
Assert.AreEqual(1, result.Metadata[0]);
Assert.AreEqual(1, result.Metadata[1]);
Assert.AreEqual(2, result.Metadata[2]);
Assert.AreEqual(0, result.Childs[0].Childs.Count);
Assert.AreEqual(3, result.Childs[0].Metadata.Count);
Assert.AreEqual(10, result.Childs[0].Metadata[0]);
Assert.AreEqual(11, result.Childs[0].Metadata[1]);
Assert.AreEqual(12, result.Childs[0].Metadata[2]);
Assert.AreEqual(1, result.Childs[1].Childs.Count);
Assert.AreEqual(1, result.Childs[1].Metadata.Count);
Assert.AreEqual(2, result.Childs[1].Metadata[0]);
Assert.AreEqual(0, result.Childs[1].Childs[0].Childs.Count);
Assert.AreEqual(1, result.Childs[1].Childs[0].Metadata.Count);
Assert.AreEqual(99, result.Childs[1].Childs[0].Metadata[0]);
}
}
}

View File

@@ -0,0 +1,18 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace AdventOfCode2018.Tests
{
[TestClass()]
public class Day08_Tests
{
[TestMethod()]
public void ResolvePart1__Test()
{
Day08 day = new Day08();
string result = day.ResolvePart1(new string[] { "2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2", });
Assert.AreEqual("138", result);
}
}
}

View File

@@ -50,6 +50,7 @@
<Compile Include="Day05.cs" />
<Compile Include="Day06.cs" />
<Compile Include="Day07.cs" />
<Compile Include="Day08.cs" />
<Compile Include="IDay.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
@@ -76,6 +77,9 @@
<Content Include="inputs\Day07.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="inputs\Day08.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

119
AdventOfCode2018/Day08.cs Normal file
View File

@@ -0,0 +1,119 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace AdventOfCode2018
{
/*
--- Day 8: Memory Maneuver ---
The sleigh is much easier to pull than you'd expect for something its weight. Unfortunately, neither you nor the Elves know which way the North Pole is from here.
You check your wrist device for anything that might help. It seems to have some kind of navigation system! Activating the navigation system produces more bad news: "Failed to start navigation system. Could not read software license file."
The navigation system's license file consists of a list of numbers (your puzzle input). The numbers define a data structure which, when processed, produces some kind of tree that can be used to calculate the license number.
The tree is made up of nodes; a single, outermost node forms the tree's root, and it contains all other nodes in the tree (or contains nodes that contain nodes, and so on).
Specifically, a node consists of:
A header, which is always exactly two numbers:
The quantity of child nodes.
The quantity of metadata entries.
Zero or more child nodes (as specified in the header).
One or more metadata entries (as specified in the header).
Each child node is itself a node that has its own header, child nodes, and metadata. For example:
2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2
A----------------------------------
B----------- C-----------
D-----
In this example, each node of the tree is also marked with an underline starting with a letter for easier identification. In it, there are four nodes:
A, which has 2 child nodes (B, C) and 3 metadata entries (1, 1, 2).
B, which has 0 child nodes and 3 metadata entries (10, 11, 12).
C, which has 1 child node (D) and 1 metadata entry (2).
D, which has 0 child nodes and 1 metadata entry (99).
The first check done on the license file is to simply add up all of the metadata entries. In this example, that sum is 1+1+2+10+11+12+2+99=138.
What is the sum of all metadata entries?
*/
public class Day08 : IDay
{
public string ResolvePart1(string[] inputs)
{
IntStream values = new IntStream(inputs[0]);
ChronoLicenceNode licenceTree = ChronoLicenceNode.BuildFromIntStream(values);
int result = licenceTree.GetChecksum();
return result.ToString();
}
public string ResolvePart2(string[] inputs)
{
return null;
}
}
public class IntStream
{
private int[] values;
private int index;
public IntStream(string strValues)
{
values = strValues
.Split(new string[] { " ", "" }, StringSplitOptions.RemoveEmptyEntries)
.Select(strVal => Convert.ToInt32(strVal))
.ToArray();
index = 0;
}
public int Get()
{
int value = values[index];
index++;
return value;
}
}
public class ChronoLicenceNode
{
public List<ChronoLicenceNode> Childs { get; } = new List<ChronoLicenceNode>();
public List<int> Metadata { get; } = new List<int>();
public static ChronoLicenceNode BuildFromIntStream(IntStream stream)
{
ChronoLicenceNode node = new ChronoLicenceNode();
int numChilds = stream.Get();
int numMetadata = stream.Get();
for (int i = 0; i < numChilds; i++)
{
ChronoLicenceNode childNode = BuildFromIntStream(stream);
node.Childs.Add(childNode);
}
for (int i = 0; i < numMetadata; i++)
{
node.Metadata.Add(stream.Get());
}
return node;
}
public int GetChecksum()
{
int checksum = Metadata.Sum();
foreach (ChronoLicenceNode child in Childs)
{
checksum += child.GetChecksum();
}
return checksum;
}
}
}

View File

@@ -7,7 +7,7 @@ namespace AdventOfCode2018
{
private static void Main(string[] args)
{
int currentDayNumber = 7;
int currentDayNumber = 8;
IDay currentDay = null;
switch (currentDayNumber)
@@ -19,6 +19,7 @@ namespace AdventOfCode2018
case 5: currentDay = new Day05(); break;
case 6: currentDay = new Day06(); break;
case 7: currentDay = new Day07(); break;
case 8: currentDay = new Day08(); break;
}
string[] linesDay = File.ReadAllLines(string.Format("inputs/Day{0:00}.txt", currentDayNumber));

File diff suppressed because one or more lines are too long