From fd61ce8dc32e4dd3fcc395fc01facc2badd28d99 Mon Sep 17 00:00:00 2001 From: Chris Kaczor Date: Sun, 15 Dec 2019 08:09:21 -0500 Subject: [PATCH] Day 14 - Part 1 --- Advent.csproj | 6 +++ Advent.csproj.DotSettings | 1 + Day14/Day14.cs | 93 +++++++++++++++++++++++++++++++++++++++ Day14/input.txt | 61 +++++++++++++++++++++++++ Day14/test-input.txt | 17 +++++++ Program.cs | 3 +- 6 files changed, 180 insertions(+), 1 deletion(-) create mode 100644 Day14/Day14.cs create mode 100644 Day14/input.txt create mode 100644 Day14/test-input.txt diff --git a/Advent.csproj b/Advent.csproj index bccac4f..6dd64f4 100644 --- a/Advent.csproj +++ b/Advent.csproj @@ -19,6 +19,12 @@ PreserveNewest + + PreserveNewest + + + PreserveNewest + PreserveNewest diff --git a/Advent.csproj.DotSettings b/Advent.csproj.DotSettings index bfac362..24c9df5 100644 --- a/Advent.csproj.DotSettings +++ b/Advent.csproj.DotSettings @@ -4,6 +4,7 @@ True True True + True True True True diff --git a/Day14/Day14.cs b/Day14/Day14.cs new file mode 100644 index 0000000..fb5db5c --- /dev/null +++ b/Day14/Day14.cs @@ -0,0 +1,93 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; + +namespace Advent +{ + public static class Day14 + { + private class Reagent + { + public string Name { get; } + public int Count { get; } + + public Reagent(string s) + { + var parts = s.Split(' '); + + Name = parts[1]; + Count = int.Parse(parts[0]); + } + } + + private class Reaction + { + public Reagent Result { get; } + public List Components { get; } = new List(); + + public Reaction(string s) + { + var reactionParts = s.Split("=>").Select(s2 => s2.Trim()).ToArray(); + + Result = new Reagent(reactionParts[1]); + + var reagentParts = reactionParts[0].Split(',').Select(s2 => s2.Trim()).ToArray(); + + foreach (var reagentPart in reagentParts) + { + Components.Add(new Reagent(reagentPart)); + } + } + } + + private static readonly Dictionary Reactions = new Dictionary(); + private static readonly Dictionary Inventory = new Dictionary(); + + public static void Execute() + { + var lines = File.ReadAllLines(@".\Day14\input.txt"); + + foreach (var line in lines) + { + var reaction = new Reaction(line); + + Reactions[reaction.Result.Name] = reaction; + + Inventory[reaction.Result.Name] = 0; + } + + var oreRequired = GetRequiredOre("FUEL", 1); + + Console.WriteLine($"Ore: {oreRequired}"); + } + + private static int GetRequiredOre(string reagentName, int amountRequired) + { + // Get the reaction to produce this reagent + var reaction = Reactions[reagentName]; + + // Get what we have already + var inInventory = Inventory[reagentName]; + + // Figure out how many reactions are needed + var reactionsRequired = (int)Math.Ceiling((decimal)Math.Max(amountRequired - inInventory, 0) / reaction.Result.Count); + + // Set what we have in inventory after + Inventory[reagentName] = (reaction.Result.Count * reactionsRequired) - (amountRequired - inInventory); + + var oreRequired = 0; + + // Loop over each reagent + foreach (var reagent in reaction.Components) + { + if (reagent.Name == "ORE") + oreRequired += reactionsRequired * reagent.Count; + else + oreRequired += GetRequiredOre(reagent.Name, reagent.Count * reactionsRequired); + } + + return oreRequired; + } + } +} diff --git a/Day14/input.txt b/Day14/input.txt new file mode 100644 index 0000000..c0393e4 --- /dev/null +++ b/Day14/input.txt @@ -0,0 +1,61 @@ +118 ORE => 7 GTPZ +6 RNQJN, 4 NQKVW => 4 DTQRC +2 GBXJL => 3 XHBR +4 BPZM => 9 LVDRH +131 ORE => 3 RHBL +2 LFZS => 2 FPRJW +6 GTPZ => 4 VTBTK +8 GPMP, 2 BPNFJ, 3 LFZS => 2 SFGCR +3 GPMP => 4 SPRCM +16 XCDZP, 1 NQKSL => 4 NQKVW +2 BXGD, 3 VJHSV, 1 MGNCW => 8 MGLH +1 XLNTJ => 1 KXBGP +9 PJQWR, 19 NQKVW, 10 GJHWN => 7 ZBGDF +3 VTBTK => 6 CJNQ +12 PJQWR => 1 JNHBR +16 BPZM => 9 MVCH +1 KWPXQ, 1 LVDRH => 6 LFZS +6 VTBTK => 6 XCDZP +1 PZFG, 2 LFZS, 2 CJNQ, 2 FPRJW, 17 MVCH, 7 MGNCW, 26 KXBGP => 6 TBTL +2 DTQRC, 7 NBNLC => 8 BPZM +102 ORE => 3 WNTQ +1 WNTQ => 9 NQKSL +5 XZMH, 1 LPLMR, 13 BXGD => 8 JPFL +1 NQKSL, 6 XCDZP, 2 FCDVQ => 9 GJHWN +6 XZMH => 4 GLDL +23 ZTWR, 4 BPZM => 2 MGNCW +11 GPMP, 19 ZBGDF => 2 XZMH +2 MGNCW, 4 XCDZP, 17 KQLT => 4 VJHSV +1 CJNQ => 7 QHPH +1 RHBL => 8 GBXJL +2 MVCH, 3 KDNT, 6 NBNLC, 26 QHPH, 2 KRKB, 1 MCPDH, 4 XZMH, 6 XHBR => 1 HZMWJ +9 XDLZ => 1 QSXKS +4 GLDL => 6 WJNP +5 MVCH => 3 MCPDH +14 TKGM => 5 LPLMR +1 WVQN => 2 PJQWR +4 KWPXQ => 6 FCDVQ +10 DTQRC, 27 TBTL, 9 HZMWJ, 41 XVGP, 2 TPZFL, 54 WNTQ, 85 RHBL, 5 WCZK, 2 QVSB, 28 SPRCM => 1 FUEL +15 RNQJN, 1 PJQWR, 2 NBNLC => 4 TKGM +126 ORE => 5 WVQN +10 NBNLC => 3 BWMD +2 SFGCR, 1 NQKSL, 1 KRKB => 1 WGQTF +2 MLWN => 5 ZTWR +12 DTQRC, 3 NQKVW, 9 NBNLC => 8 BPNFJ +10 SFGCR, 1 PZFG, 2 ZVFVH, 12 WJNP, 14 WGQTF, 1 JNHBR, 8 FPRJW => 3 QVSB +2 MCPDH => 8 XVGP +19 JPFL => 4 TPZFL +5 GBXJL => 6 MLWN +9 TKGM => 5 KDNT +1 NQKVW, 15 PJQWR => 9 XDLZ +2 QHPH, 2 JNHBR => 1 ZVFVH +189 ORE => 6 KWPXQ +5 KRKB, 3 MGLH => 6 WCZK +3 NBNLC, 8 BWMD => 7 KRKB +1 ZBGDF, 6 XDLZ => 4 GPMP +11 XDLZ, 1 QSXKS => 2 BXGD +2 KRKB, 1 GJHWN => 1 XLNTJ +3 ZTWR => 4 RNQJN +15 FCDVQ, 3 MLWN => 4 NBNLC +1 KDNT, 1 XZMH, 8 BXGD => 1 KQLT +2 WJNP => 3 PZFG diff --git a/Day14/test-input.txt b/Day14/test-input.txt new file mode 100644 index 0000000..753fa4e --- /dev/null +++ b/Day14/test-input.txt @@ -0,0 +1,17 @@ +171 ORE => 8 CNZTR +7 ZLQW, 3 BMBT, 9 XCVML, 26 XMNCP, 1 WPTQ, 2 MZWV, 1 RJRHP => 4 PLWSL +114 ORE => 4 BHXH +14 VRPVC => 6 BMBT +6 BHXH, 18 KTJDG, 12 WPTQ, 7 PLWSL, 31 FHTLT, 37 ZDVW => 1 FUEL +6 WPTQ, 2 BMBT, 8 ZLQW, 18 KTJDG, 1 XMNCP, 6 MZWV, 1 RJRHP => 6 FHTLT +15 XDBXC, 2 LTCX, 1 VRPVC => 6 ZLQW +13 WPTQ, 10 LTCX, 3 RJRHP, 14 XMNCP, 2 MZWV, 1 ZLQW => 1 ZDVW +5 BMBT => 4 WPTQ +189 ORE => 9 KTJDG +1 MZWV, 17 XDBXC, 3 XCVML => 2 XMNCP +12 VRPVC, 27 CNZTR => 2 XDBXC +15 KTJDG, 12 BHXH => 5 XCVML +3 BHXH, 2 VRPVC => 7 MZWV +121 ORE => 7 VRPVC +7 XCVML => 6 RJRHP +5 BHXH, 4 VRPVC => 5 LTCX \ No newline at end of file diff --git a/Program.cs b/Program.cs index 4aeab66..52d8bdc 100644 --- a/Program.cs +++ b/Program.cs @@ -16,7 +16,8 @@ //Day10.Execute(); //Day11.Execute(); //Day12.Execute(); - Day13.Execute(); + //Day13.Execute(); + Day14.Execute(); } } }