-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay07Solver.cs
53 lines (46 loc) · 1.33 KB
/
Day07Solver.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
using System.Collections.Generic;
using System.Linq;
using AdventOfCode.Abstractions;
namespace AdventOfCode.Year2019.Day07;
public class Day07Solver : DaySolver
{
private const int START_POWER = 0;
private const int NOF_AMPLIFIERS = 5;
private readonly int[] _input;
private readonly ThrusterPowerSystem _thrusterPowerSystem;
public Day07Solver(string inputFilePath) : base(inputFilePath)
{
_input = Input.Split(',').Select(int.Parse).ToArray();
_thrusterPowerSystem = new(_input, NOF_AMPLIFIERS, START_POWER);
}
private IEnumerable<IReadOnlyList<T>> GetPermutations<T>(IReadOnlyList<T> list)
{
if (list.Count <= 1)
{
yield return list;
yield break;
}
for (int i = 0; i < list.Count; i++)
{
T el = list[i];
foreach (IReadOnlyList<T> perm in GetPermutations(list.Take(i).Concat(list.Skip(i + 1)).ToList()))
{
yield return perm.Prepend(el).ToList();
}
}
}
public override string SolvePart1()
{
_thrusterPowerSystem.Reset();
int part1 = GetPermutations(Enumerable.Range(0, 5).ToList())
.Max(_thrusterPowerSystem.GetThrusterPower);
return part1.ToString();
}
public override string SolvePart2()
{
_thrusterPowerSystem.Reset();
int part2 = GetPermutations(Enumerable.Range(5, 5).ToList())
.Max(perm => _thrusterPowerSystem.GetRecurrentThrusterPower(perm, true));
return part2.ToString();
}
}