mirror of
https://github.com/ckaczor/Advent2019.git
synced 2026-01-13 17:22:15 -05:00
Day 14 - Part 1
This commit is contained in:
@@ -19,6 +19,12 @@
|
|||||||
<None Update="Day13\input.txt">
|
<None Update="Day13\input.txt">
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
</None>
|
</None>
|
||||||
|
<None Update="Day14\input.txt">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
|
<None Update="Day14\test-input.txt">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
<None Update="Day1\input.txt">
|
<None Update="Day1\input.txt">
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
</None>
|
</None>
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=day11/@EntryIndexedValue">True</s:Boolean>
|
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=day11/@EntryIndexedValue">True</s:Boolean>
|
||||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=day12/@EntryIndexedValue">True</s:Boolean>
|
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=day12/@EntryIndexedValue">True</s:Boolean>
|
||||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=day13/@EntryIndexedValue">True</s:Boolean>
|
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=day13/@EntryIndexedValue">True</s:Boolean>
|
||||||
|
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=day14/@EntryIndexedValue">True</s:Boolean>
|
||||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=day2/@EntryIndexedValue">True</s:Boolean>
|
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=day2/@EntryIndexedValue">True</s:Boolean>
|
||||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=day3/@EntryIndexedValue">True</s:Boolean>
|
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=day3/@EntryIndexedValue">True</s:Boolean>
|
||||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=day4/@EntryIndexedValue">True</s:Boolean>
|
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=day4/@EntryIndexedValue">True</s:Boolean>
|
||||||
|
|||||||
93
Day14/Day14.cs
Normal file
93
Day14/Day14.cs
Normal file
@@ -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<Reagent> Components { get; } = new List<Reagent>();
|
||||||
|
|
||||||
|
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<string, Reaction> Reactions = new Dictionary<string, Reaction>();
|
||||||
|
private static readonly Dictionary<string, int> Inventory = new Dictionary<string, int>();
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
61
Day14/input.txt
Normal file
61
Day14/input.txt
Normal file
@@ -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
|
||||||
17
Day14/test-input.txt
Normal file
17
Day14/test-input.txt
Normal file
@@ -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
|
||||||
@@ -16,7 +16,8 @@
|
|||||||
//Day10.Execute();
|
//Day10.Execute();
|
||||||
//Day11.Execute();
|
//Day11.Execute();
|
||||||
//Day12.Execute();
|
//Day12.Execute();
|
||||||
Day13.Execute();
|
//Day13.Execute();
|
||||||
|
Day14.Execute();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user