AdventOfCode/AdventOfCode.Solutions/2022/Day 10/Day10Part2.cs
Rob 3846b42b7e Massive code base change
partial completion of day 3
2023-12-03 19:09:26 +01:00

67 lines
1.9 KiB
C#

namespace AdventOfCode.Solutions._2022
{
public class Day10Part2 : Answerable
{
public override int Year { get; set; } = 2022;
public override int Day { get; set; } = 10;
public override int Part { get; set; } = 2;
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++)
{
char render = '.';
int scanLocation = Cycles % 40;
if (X - 1 == scanLocation || X == scanLocation || X + 1 == scanLocation)
{
render = '#';
}
Console.Write(render);
Cycles++;
if (Cycles % 40 == 0)
{
Console.Write('\n');
}
if (measureCycles.Contains(Cycles))
{
measureResult += Cycles * X;
}
}
X += valueToAdd;
}
}
}