generated from sindrekjr/AdventOfCodeBase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.cs
60 lines (52 loc) · 1.63 KB
/
Solution.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
using System.Linq;
using System.Collections.Generic;
namespace AdventOfCode.Solutions.Year2019.Day07;
class Solution : SolutionBase
{
IntcodeComputer Amplifier;
public Solution() : base(7, 2019, "Amplification Circuit")
{
Amplifier = new IntcodeComputer(Input.ToIntArray(","));
}
protected override string SolvePartOne()
{
int highest = 0;
foreach(var signal in Enumerable.Range(0, 5).Permutations())
{
int output = 0;
foreach(int i in signal.ToArray())
{
output = (int)Amplifier.Initialize().WriteInput(i, output).Run().Output.First();
}
if(output > highest) highest = output;
}
return highest.ToString();
}
protected override string SolvePartTwo()
{
int highest = 0;
foreach(var signal in Enumerable.Range(5, 5).Permutations())
{
var Amplifiers = new Queue<IntcodeComputer<int>>();
for(int i = 0; i < 5; i++)
{
Amplifiers.Enqueue(
new IntcodeComputer(Input.ToIntArray(",")).WriteInput(signal.ToArray()[i])
);
}
int output = 0;
while(Amplifiers.Count > 0)
{
var Amp = Amplifiers.Dequeue();
Amp.Input.Enqueue(output);
if(Amp.Run().Paused)
{
Amplifiers.Enqueue(Amp);
}
output = (int)Amp.Output.Dequeue();
}
if(output > highest) highest = output;
}
return highest.ToString();
}
}