Skip to content

Commit

Permalink
Day 8 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
jongeorge1 committed Feb 18, 2019
1 parent 284c3c0 commit 1ffba41
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 25 deletions.
24 changes: 0 additions & 24 deletions AoC2018.Solutions/Day08/LicenceFileExtensions.cs

This file was deleted.

29 changes: 29 additions & 0 deletions AoC2018.Solutions/Day08/NodeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace AoC2018.Solutions.Day08
{
using System;
using System.Collections.Generic;
using System.Linq;

public static class NodeExtensions
{
public static int MetadataChecksum(this Node[] nodes)
{
return nodes.Sum(x => x.MetadataChecksum());
}

public static int MetadataChecksum(this Node node)
{
return node.Metadata.Sum() + node.Children.MetadataChecksum();
}

public static int Value(this Node node)
{
if (node.Children.Length == 0)
{
return node.Metadata.Sum();
}

return node.Metadata.Sum(x => (x > 0 && x <= node.Children.Length) ? node.Children[x - 1].Value() : 0);
}
}
}
2 changes: 1 addition & 1 deletion AoC2018.Solutions/Day08/Part01.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public string Solve(string input)
{
var licenceFile = LicenceFile.Parse(input);

return licenceFile.MetadataChecksum().ToString();
return licenceFile.RootNodes.MetadataChecksum().ToString();
}
}
}
16 changes: 16 additions & 0 deletions AoC2018.Solutions/Day08/Part02.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace AoC2018.Solutions.Day08
{
using System;
using System.Collections.Generic;
using System.Linq;

public class Part02 : ISolution
{
public string Solve(string input)
{
var licenceFile = LicenceFile.Parse(input);

return licenceFile.RootNodes[0].Value().ToString();
}
}
}
1 change: 1 addition & 0 deletions AoC2018.Tests/AoCTestCases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class AoCTestCases
[TestCase(6, 1, "1, 1\r\n1, 6\r\n8, 3\r\n3, 4\r\n5, 5\r\n8, 9", "17")]
[TestCase(7, 1, "Step C must be finished before step A can begin.\r\nStep C must be finished before step F can begin.\r\nStep A must be finished before step B can begin.\r\nStep A must be finished before step D can begin.\r\nStep B must be finished before step E can begin.\r\nStep D must be finished before step E can begin.\r\nStep F must be finished before step E can begin.", "CABDFE")]
[TestCase(8, 1, "2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2", "138")]
[TestCase(8, 2, "2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2", "66")]
public void Tests(int day, int part, string input, string expectedResult)
{
ISolution solution = SolutionFactory.GetSolution(day, part);
Expand Down

0 comments on commit 1ffba41

Please sign in to comment.