/
Day14.cs
145 lines (113 loc) · 4.34 KB
/
Day14.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace AdventOfCode2019
{
public class Day14 : IDay
{
private static List<ChemicalReaction> ReadFile()
{
var reactions = new List<ChemicalReaction>();
var fileReader = new StreamReader("./Input/Day14.txt");
string line;
while ((line = fileReader.ReadLine()) != null)
{
reactions.Add(ChemicalReaction.Parse(line));
}
return reactions;
}
public bool IsImplemented => true;
public bool IsPart1Complete => true;
public void DoAction1()
{
var refinery = new Refinery(ReadFile());
refinery.Calculate("FUEL", 1);
Console.WriteLine($"Total ORE needed is {refinery.OreNeeded}");
}
public bool IsPart2Complete => true;
public void DoAction2()
{
var totalOre = 1000000000000;
var refinery = new Refinery(ReadFile());
var counter = 7659700;
while (true)
{
refinery.Calculate("FUEL", counter);
Console.WriteLine($"For {counter} FUEL you need {refinery.OreNeeded}");
if (refinery.OreNeeded > totalOre)
{
break;
}
counter++;
}
Console.WriteLine();
Console.WriteLine($"The maximum number of fuel with 1 trillion ore is {counter-1}");
}
}
internal class Refinery
{
private readonly List<ChemicalReaction> _reactions;
internal long OreNeeded { get; private set; }
internal Refinery(List<ChemicalReaction> reactions)
{
_reactions = reactions;
}
internal void Calculate(string name, int amount)
{
OreNeeded = 0;
var componentsNeeded = new Dictionary<string, long> { { name, amount } };
while (componentsNeeded.Any(component => component.Key != "ORE" && component.Value > 0))
{
var fill = componentsNeeded.First(component => component.Key != "ORE" && component.Value > 0);
var reaction = _reactions.First(x => x.OutputComponent.Name == fill.Key);
var multiplier = Convert.ToInt64(Math.Ceiling(componentsNeeded[fill.Key] / (reaction.OutputComponent.Amount * 1.0)));
componentsNeeded[fill.Key] -= reaction.OutputComponent.Amount * multiplier;
foreach (var input in reaction.InputComponents)
{
if (componentsNeeded.ContainsKey(input.Name))
{
componentsNeeded[input.Name] += input.Amount * multiplier;
}
else
{
componentsNeeded.Add(input.Name, input.Amount * multiplier);
}
}
}
OreNeeded = componentsNeeded["ORE"];
}
}
internal class ChemicalReaction
{
public List<ReactionComponent> InputComponents { get; set; }
public ReactionComponent OutputComponent { get; set; }
public static ChemicalReaction Parse(string reactionInput)
{
var returnValue = new ChemicalReaction {
InputComponents = new List<ReactionComponent>()
};
var inOut = reactionInput.Split(new[] { "=>" }, StringSplitOptions.RemoveEmptyEntries);
var inputs = inOut[0].Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var input in inputs)
{
returnValue.InputComponents.Add(ReactionComponent.Parse(input.Trim()));
}
returnValue.OutputComponent = ReactionComponent.Parse(inOut[1].Trim());
return returnValue;
}
}
internal struct ReactionComponent
{
public int Amount { get; set; }
public string Name { get; set; }
public static ReactionComponent Parse(string input)
{
var parsedArr = input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
return new ReactionComponent {
Amount = int.Parse(parsedArr[0].Trim()),
Name = parsedArr[1].Trim()
};
}
}
}