[2022] Day10 added

This commit is contained in:
Tiago Garcia 2023-06-09 23:29:32 +01:00
parent 67a1245371
commit cff59d5939
No known key found for this signature in database
GPG Key ID: F423E37A2415E160
2 changed files with 95 additions and 0 deletions

94
Year2022/Day10.cs Normal file
View File

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

View File

@ -13,5 +13,6 @@ public static class Loader2022
new Day7(); new Day7();
// new Day8(); -- Commented because it's a slow solution // new Day8(); -- Commented because it's a slow solution
new Day9(); new Day9();
new Day10();
} }
} }