mirror of
https://github.com/ckaczor/Advent2019.git
synced 2026-01-14 01:25:36 -05:00
52 lines
2.7 KiB
C#
52 lines
2.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace Advent
|
|
{
|
|
public static class Day7
|
|
{
|
|
public static void Execute()
|
|
{
|
|
var program = "3,8,1001,8,10,8,105,1,0,0,21,34,43,64,85,98,179,260,341,422,99999,3,9,1001,9,3,9,102,3,9,9,4,9,99,3,9,102,5,9,9,4,9,99,3,9,1001,9,2,9,1002,9,4,9,1001,9,3,9,1002,9,4,9,4,9,99,3,9,1001,9,3,9,102,3,9,9,101,4,9,9,102,3,9,9,4,9,99,3,9,101,2,9,9,1002,9,3,9,4,9,99,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,99,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,99,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,99,3,9,101,1,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,99,3,9,101,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,99";
|
|
|
|
var phaseList = GetPhaseList();
|
|
|
|
int? max = 0;
|
|
|
|
var amp = new IntcodeComputer();
|
|
|
|
foreach (var phases in phaseList)
|
|
{
|
|
var output = amp.Execute(new[] { phases[0], 0 }, program);
|
|
output = amp.Execute(new[] { phases[1], output.GetValueOrDefault(0) }, program);
|
|
output = amp.Execute(new[] { phases[2], output.GetValueOrDefault(0) }, program);
|
|
output = amp.Execute(new[] { phases[3], output.GetValueOrDefault(0) }, program);
|
|
output = amp.Execute(new[] { phases[4], output.GetValueOrDefault(0) }, program);
|
|
|
|
if (output > max)
|
|
max = output;
|
|
}
|
|
|
|
Console.WriteLine(max);
|
|
}
|
|
|
|
private static IEnumerable<int[]> GetPhaseList()
|
|
{
|
|
// This is a stupid brute force way to do it but I'm lazy and it is fast so good enough
|
|
|
|
var phaseList = new List<int[]>();
|
|
|
|
for (var i = 0; i <= 99999; i++)
|
|
{
|
|
var phases = i.ToString("00000").ToCharArray().Select(c => int.Parse(c.ToString())).ToArray();
|
|
|
|
if (phases.All(p => p <= 4) && phases.Distinct().Count() == 5)
|
|
phaseList.Add(phases);
|
|
}
|
|
|
|
return phaseList;
|
|
}
|
|
}
|
|
}
|