From 60ba5e9ab3ab9456663bb4f07fedea31e7ce3a1d Mon Sep 17 00:00:00 2001 From: Jonathan George Date: Mon, 18 Feb 2019 13:13:31 +0000 Subject: [PATCH] Day 7 part 1 --- AoC2018.Runner/AoC2018.Runner.csproj | 3 + AoC2018.Runner/Input/day07.txt | 101 +++++++++++++++++++++++++++ AoC2018.Solutions/Day07/Part01.cs | 36 ++++++++++ AoC2018.Tests/AoCTestCases.cs | 1 + 4 files changed, 141 insertions(+) create mode 100644 AoC2018.Runner/Input/day07.txt create mode 100644 AoC2018.Solutions/Day07/Part01.cs diff --git a/AoC2018.Runner/AoC2018.Runner.csproj b/AoC2018.Runner/AoC2018.Runner.csproj index ccd34a8..4604660 100644 --- a/AoC2018.Runner/AoC2018.Runner.csproj +++ b/AoC2018.Runner/AoC2018.Runner.csproj @@ -28,6 +28,9 @@ PreserveNewest + + PreserveNewest + PreserveNewest diff --git a/AoC2018.Runner/Input/day07.txt b/AoC2018.Runner/Input/day07.txt new file mode 100644 index 0000000..4457263 --- /dev/null +++ b/AoC2018.Runner/Input/day07.txt @@ -0,0 +1,101 @@ +Step B must be finished before step G can begin. +Step G must be finished before step J can begin. +Step J must be finished before step F can begin. +Step U must be finished before step Z can begin. +Step C must be finished before step M can begin. +Step Y must be finished before step I can begin. +Step Q must be finished before step A can begin. +Step N must be finished before step L can begin. +Step O must be finished before step A can begin. +Step Z must be finished before step T can begin. +Step I must be finished before step H can begin. +Step L must be finished before step W can begin. +Step F must be finished before step W can begin. +Step T must be finished before step X can begin. +Step A must be finished before step X can begin. +Step K must be finished before step X can begin. +Step S must be finished before step P can begin. +Step M must be finished before step E can begin. +Step E must be finished before step W can begin. +Step D must be finished before step P can begin. +Step P must be finished before step W can begin. +Step X must be finished before step H can begin. +Step V must be finished before step W can begin. +Step R must be finished before step H can begin. +Step H must be finished before step W can begin. +Step N must be finished before step I can begin. +Step X must be finished before step R can begin. +Step D must be finished before step V can begin. +Step V must be finished before step R can begin. +Step F must be finished before step K can begin. +Step P must be finished before step R can begin. +Step P must be finished before step V can begin. +Step S must be finished before step X can begin. +Step I must be finished before step S can begin. +Step J must be finished before step N can begin. +Step T must be finished before step S can begin. +Step T must be finished before step R can begin. +Step K must be finished before step P can begin. +Step N must be finished before step R can begin. +Step G must be finished before step T can begin. +Step I must be finished before step V can begin. +Step G must be finished before step Q can begin. +Step D must be finished before step H can begin. +Step V must be finished before step H can begin. +Step T must be finished before step K can begin. +Step T must be finished before step W can begin. +Step E must be finished before step H can begin. +Step C must be finished before step R can begin. +Step L must be finished before step K can begin. +Step G must be finished before step Y can begin. +Step Y must be finished before step O can begin. +Step O must be finished before step E can begin. +Step U must be finished before step S can begin. +Step X must be finished before step W can begin. +Step C must be finished before step D can begin. +Step E must be finished before step P can begin. +Step B must be finished before step R can begin. +Step F must be finished before step R can begin. +Step A must be finished before step D can begin. +Step G must be finished before step M can begin. +Step B must be finished before step Q can begin. +Step Q must be finished before step V can begin. +Step B must be finished before step W can begin. +Step S must be finished before step H can begin. +Step P must be finished before step X can begin. +Step I must be finished before step M can begin. +Step A must be finished before step S can begin. +Step M must be finished before step X can begin. +Step L must be finished before step S can begin. +Step S must be finished before step W can begin. +Step L must be finished before step V can begin. +Step Z must be finished before step X can begin. +Step M must be finished before step R can begin. +Step T must be finished before step A can begin. +Step N must be finished before step V can begin. +Step M must be finished before step H can begin. +Step E must be finished before step D can begin. +Step F must be finished before step V can begin. +Step B must be finished before step O can begin. +Step G must be finished before step U can begin. +Step J must be finished before step C can begin. +Step G must be finished before step F can begin. +Step Y must be finished before step M can begin. +Step F must be finished before step D can begin. +Step M must be finished before step P can begin. +Step F must be finished before step T can begin. +Step G must be finished before step A can begin. +Step G must be finished before step Z can begin. +Step K must be finished before step V can begin. +Step J must be finished before step Z can begin. +Step O must be finished before step Z can begin. +Step B must be finished before step E can begin. +Step Z must be finished before step V can begin. +Step Q must be finished before step O can begin. +Step J must be finished before step D can begin. +Step Y must be finished before step E can begin. +Step D must be finished before step R can begin. +Step I must be finished before step F can begin. +Step M must be finished before step V can begin. +Step I must be finished before step D can begin. +Step O must be finished before step P can begin. \ No newline at end of file diff --git a/AoC2018.Solutions/Day07/Part01.cs b/AoC2018.Solutions/Day07/Part01.cs new file mode 100644 index 0000000..563ff31 --- /dev/null +++ b/AoC2018.Solutions/Day07/Part01.cs @@ -0,0 +1,36 @@ +namespace AoC2018.Solutions.Day07 +{ + using System; + using System.Collections.Generic; + using System.Linq; + + public class Part01 : ISolution + { + public string Solve(string input) + { + (string Step, string PreRequisite)[] statements = input.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).Select(this.ParseStatement).ToArray(); + string[] allSteps = statements.Select(x => x.Step).Union(statements.Select(x => x.PreRequisite)).Distinct().ToArray(); + var stepsWithDependencies = allSteps.ToDictionary(x => x, x => statements.Where(s => s.Step == x).Select(s => s.PreRequisite).ToArray()); + + var resolvedSteps = new List(); + + while (resolvedSteps.Count != allSteps.Length) + { + resolvedSteps.Add(this.GetNextAvailableStep(stepsWithDependencies, resolvedSteps)); + } + + return string.Concat(resolvedSteps); + } + + private string GetNextAvailableStep(Dictionary stepsWithDependencies, List resolvedSteps) + { + return stepsWithDependencies.Where(x => !resolvedSteps.Contains(x.Key)).Where(x => x.Value.All(y => resolvedSteps.Contains(y))).Select(x => x.Key).OrderBy(x => x).First(); + } + + private (string Step, string PreRequisite) ParseStatement(string statement) + { + string[] components = statement.Split(' '); + return (components[7], components[1]); + } + } +} diff --git a/AoC2018.Tests/AoCTestCases.cs b/AoC2018.Tests/AoCTestCases.cs index 27c562b..e855027 100644 --- a/AoC2018.Tests/AoCTestCases.cs +++ b/AoC2018.Tests/AoCTestCases.cs @@ -30,6 +30,7 @@ public class AoCTestCases [TestCase(5, 1, "dabAcCaCBAcCcaDA", "10")] [TestCase(5, 2, "dabAcCaCBAcCcaDA", "4")] [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")] public void Tests(int day, int part, string input, string expectedResult) { ISolution solution = SolutionFactory.GetSolution(day, part);