namespace AdventOfCodeLibrary._2022 { using AdventOfCodeLibrary.Shared; 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; } } }