2023-06-09 22:29:32 +00:00
|
|
|
namespace AdventOfCode.Year2022;
|
|
|
|
|
|
|
|
public class Day10
|
|
|
|
{
|
|
|
|
private static readonly List<string> CommandsList = new();
|
2023-06-24 21:26:39 +00:00
|
|
|
|
2023-06-09 22:29:32 +00:00
|
|
|
public Day10()
|
|
|
|
{
|
|
|
|
File.ReadAllLines("inputs/day10.txt").ToList().ForEach(line => CommandsList.Add(line));
|
2023-06-24 21:26:39 +00:00
|
|
|
Console.WriteLine($@"
|
|
|
|
Day10 Solution
|
|
|
|
Part1 Result: {Part1()}
|
|
|
|
Part2 Result: {Part2()}
|
|
|
|
=============================");
|
2023-06-09 22:29:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static int Part1()
|
|
|
|
{
|
2023-06-24 21:26:39 +00:00
|
|
|
var sum = 0;
|
|
|
|
var currentPower = 1;
|
|
|
|
|
|
|
|
var listIndex = 0;
|
|
|
|
var addPower = 0;
|
|
|
|
var skipCycles = 0;
|
2023-06-09 22:29:32 +00:00
|
|
|
|
2023-06-24 21:26:39 +00:00
|
|
|
for (var cycle = 1; cycle <= 220; cycle++)
|
2023-06-09 22:29:32 +00:00
|
|
|
{
|
|
|
|
if (skipCycles == 0)
|
|
|
|
{
|
|
|
|
currentPower += addPower;
|
2023-06-24 21:26:39 +00:00
|
|
|
var line = CommandsList[listIndex];
|
|
|
|
var cmd = line.Substring(0, 4);
|
2023-06-09 22:29:32 +00:00
|
|
|
skipCycles = cmd == "noop" ? 0 : 1;
|
|
|
|
addPower = cmd == "noop" ? 0 : Convert.ToInt32(line.Split(" ")[1]);
|
|
|
|
listIndex++;
|
|
|
|
}
|
|
|
|
else
|
2023-06-24 21:26:39 +00:00
|
|
|
{
|
2023-06-09 22:29:32 +00:00
|
|
|
skipCycles--;
|
2023-06-24 21:26:39 +00:00
|
|
|
}
|
|
|
|
|
2023-06-09 22:29:32 +00:00
|
|
|
if ((cycle - 20) % 40 == 0)
|
|
|
|
sum += currentPower * cycle;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static string Part2()
|
|
|
|
{
|
2023-06-24 21:26:39 +00:00
|
|
|
var crt = new char[40 * 6];
|
|
|
|
var registerPos = 0;
|
|
|
|
|
|
|
|
var listIndex = 0;
|
|
|
|
var movePos = 0;
|
|
|
|
var skipCycles = 0;
|
2023-06-09 22:29:32 +00:00
|
|
|
|
2023-06-24 21:26:39 +00:00
|
|
|
for (var cycle = 0; cycle < 240; cycle++)
|
2023-06-09 22:29:32 +00:00
|
|
|
{
|
|
|
|
if (skipCycles == 0)
|
|
|
|
{
|
|
|
|
registerPos += movePos;
|
2023-06-24 21:26:39 +00:00
|
|
|
var line = CommandsList[listIndex];
|
|
|
|
var cmd = line[..4];
|
2023-06-09 22:29:32 +00:00
|
|
|
skipCycles = cmd == "noop" ? 0 : 1;
|
|
|
|
movePos = cmd == "noop" ? 0 : Convert.ToInt32(line.Split(" ")[1]);
|
|
|
|
listIndex++;
|
|
|
|
}
|
|
|
|
else
|
2023-06-24 21:26:39 +00:00
|
|
|
{
|
2023-06-09 22:29:32 +00:00
|
|
|
skipCycles--;
|
2023-06-24 21:26:39 +00:00
|
|
|
}
|
2023-06-09 22:29:32 +00:00
|
|
|
|
2023-06-24 21:26:39 +00:00
|
|
|
var sprite = CreateSprite(registerPos);
|
|
|
|
crt[cycle] = sprite[cycle % 40];
|
2023-06-09 22:29:32 +00:00
|
|
|
}
|
|
|
|
|
2023-06-24 21:26:39 +00:00
|
|
|
var result = "\n";
|
|
|
|
var charI = 0;
|
2023-06-09 22:29:32 +00:00
|
|
|
crt.ToList().ForEach(c =>
|
|
|
|
{
|
|
|
|
result += c.ToString();
|
2023-06-24 21:26:39 +00:00
|
|
|
if ((charI + 1) % 40 == 0)
|
2023-06-09 22:29:32 +00:00
|
|
|
result += "\n";
|
|
|
|
charI++;
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static char[] CreateSprite(int registerPos)
|
|
|
|
{
|
2023-06-24 21:26:39 +00:00
|
|
|
var result = new char[40];
|
|
|
|
for (var i = 0; i < 40; i++)
|
|
|
|
result[i] = i > registerPos - 1 && i < registerPos + 3 ? '#' : ' ';
|
2023-06-09 22:29:32 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|