53 lines
1.5 KiB
C#
53 lines
1.5 KiB
C#
namespace AdventOfCodeLibrary._2022
|
|
{
|
|
using AdventOfCodeLibrary.Shared;
|
|
|
|
public class Day10Part1 : Answerable
|
|
{
|
|
public override int Year { get; set; } = 2022;
|
|
public override int Day { get; set; } = 10;
|
|
public override int Part { get; set; } = 1;
|
|
|
|
private int Cycles = 0;
|
|
private int X = 1;
|
|
private readonly int[] measureCycles = { 20, 60, 100, 140, 180, 220 };
|
|
|
|
private int measureResult = 0;
|
|
|
|
public override string GetAnswer(byte[] data)
|
|
{
|
|
string[] commands = GetAsStringArray(data);
|
|
|
|
for (int commandIndex = 0; commandIndex < commands.Length; commandIndex++)
|
|
{
|
|
if (commands[commandIndex].Equals("noop"))
|
|
{
|
|
ProcessCycles(1, 0);
|
|
}
|
|
|
|
// command should start with addx
|
|
string[] commandValues = commands[commandIndex].Split(' ');
|
|
if (commandValues[0].Equals("addx"))
|
|
{
|
|
ProcessCycles(2, Convert.ToInt32(commandValues[1]));
|
|
}
|
|
}
|
|
return measureResult.ToString();
|
|
}
|
|
|
|
internal void ProcessCycles(int cycles, int valueToAdd)
|
|
{
|
|
for (int i = 0; i < cycles; i++)
|
|
{
|
|
Cycles++;
|
|
if (measureCycles.Contains(Cycles))
|
|
{
|
|
measureResult += Cycles * X;
|
|
}
|
|
}
|
|
|
|
X += valueToAdd;
|
|
}
|
|
}
|
|
}
|