Add day 10
This commit is contained in:
281
Days/Day10.cs
Normal file
281
Days/Day10.cs
Normal file
@@ -0,0 +1,281 @@
|
||||
using Spectre.Console;
|
||||
|
||||
namespace AdventOfCode.Days;
|
||||
|
||||
public class Day10 : Day
|
||||
{
|
||||
public override int Number => 10;
|
||||
public override string Name => "Cathode-Ray Tube";
|
||||
public override void RunPart1()
|
||||
{
|
||||
int cycle = 1;
|
||||
int x = 1;
|
||||
long sum = 0;
|
||||
|
||||
// Increment sum when needed
|
||||
void IncrementCycle()
|
||||
{
|
||||
cycle++;
|
||||
|
||||
// Check for signal stength at the start of a cycle
|
||||
if ((cycle + 20) % 40 == 0)
|
||||
{
|
||||
sum += x * cycle;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var line in Input.ReadAllLines())
|
||||
{
|
||||
// noop
|
||||
if (line == "noop")
|
||||
{
|
||||
// noop finished, go to next cycle
|
||||
IncrementCycle();
|
||||
}
|
||||
// addx
|
||||
else
|
||||
{
|
||||
var split = line.Split(' ');
|
||||
var v = int.Parse(split[1]);
|
||||
|
||||
// Prepare addx
|
||||
IncrementCycle();
|
||||
|
||||
// addx finished, add to x and then go to next cycle
|
||||
x += v;
|
||||
IncrementCycle();
|
||||
}
|
||||
}
|
||||
|
||||
AnsiConsole.MarkupLine($"[green]Sum of signal strengths: [yellow]{sum}[/][/]");
|
||||
}
|
||||
|
||||
public override void RunPart2()
|
||||
{
|
||||
// Setup the CRT
|
||||
const char litPixel = '#';
|
||||
const char darkPixel = '.';
|
||||
|
||||
const int rowCount = 6;
|
||||
const int columnCount = 40;
|
||||
var crt = new char[rowCount, columnCount];
|
||||
var crtCanvas = new Canvas(columnCount, rowCount);
|
||||
|
||||
// Now cycle is the position where the CRT is drawing (need to offset by 1 in the grid)
|
||||
int cycle = 1;
|
||||
// x is the position of the sprite
|
||||
int x = 1;
|
||||
|
||||
// Draw first pixel
|
||||
crt[0, 0] = litPixel;
|
||||
crtCanvas.SetPixel(0, 0, Color.Red);
|
||||
|
||||
// Increment sum when needed
|
||||
void IncrementCycle()
|
||||
{
|
||||
cycle++;
|
||||
|
||||
// We're done drawing the crt
|
||||
if (cycle > 240)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Draw a white or black pixel
|
||||
int row = (cycle - 1) / columnCount;
|
||||
int column = (cycle - 1) % columnCount;
|
||||
|
||||
// The CRT is drawing on the sprite
|
||||
if (column == x - 1 || column == x || column == x + 1)
|
||||
{
|
||||
crt[row, column] = litPixel;
|
||||
crtCanvas.SetPixel(column, row, (cycle + row) % 2 == 0 ? Color.Green : Color.Red);
|
||||
}
|
||||
else
|
||||
{
|
||||
crt[row, column] = darkPixel;
|
||||
crtCanvas.SetPixel(column, row, Color.Black);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var line in Input.ReadAllLines())
|
||||
{
|
||||
// noop
|
||||
if (line == "noop")
|
||||
{
|
||||
// noop finished, go to next cycle
|
||||
IncrementCycle();
|
||||
}
|
||||
// addx
|
||||
else
|
||||
{
|
||||
var split = line.Split(' ');
|
||||
var v = int.Parse(split[1]);
|
||||
|
||||
// Prepare addx
|
||||
IncrementCycle();
|
||||
|
||||
// addx finished, add to x and then go to next cycle
|
||||
x += v;
|
||||
IncrementCycle();
|
||||
}
|
||||
}
|
||||
|
||||
// Print the CRT
|
||||
AnsiConsole.Write(crtCanvas);
|
||||
}
|
||||
|
||||
#region Input
|
||||
|
||||
public const string Input =
|
||||
"""
|
||||
noop
|
||||
noop
|
||||
addx 15
|
||||
addx -10
|
||||
noop
|
||||
noop
|
||||
addx 3
|
||||
noop
|
||||
noop
|
||||
addx 7
|
||||
addx 1
|
||||
addx 4
|
||||
addx -1
|
||||
addx 1
|
||||
addx 5
|
||||
addx 1
|
||||
noop
|
||||
noop
|
||||
addx 5
|
||||
addx -1
|
||||
noop
|
||||
addx 3
|
||||
noop
|
||||
addx 3
|
||||
addx -38
|
||||
noop
|
||||
addx 3
|
||||
addx 2
|
||||
addx 5
|
||||
addx 2
|
||||
addx 26
|
||||
addx -21
|
||||
addx -2
|
||||
addx 5
|
||||
addx 2
|
||||
addx -14
|
||||
addx 15
|
||||
noop
|
||||
addx 7
|
||||
noop
|
||||
addx 2
|
||||
addx -22
|
||||
addx 23
|
||||
addx 2
|
||||
addx 5
|
||||
addx -40
|
||||
noop
|
||||
noop
|
||||
addx 3
|
||||
addx 2
|
||||
noop
|
||||
addx 24
|
||||
addx -19
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 5
|
||||
addx 5
|
||||
addx 2
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 7
|
||||
noop
|
||||
addx 3
|
||||
noop
|
||||
addx 3
|
||||
addx -2
|
||||
addx 2
|
||||
addx 5
|
||||
addx -38
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 5
|
||||
addx 2
|
||||
addx -1
|
||||
addx 2
|
||||
addx 30
|
||||
addx -23
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 3
|
||||
addx 5
|
||||
addx -11
|
||||
addx 12
|
||||
noop
|
||||
addx 6
|
||||
addx 1
|
||||
noop
|
||||
addx 4
|
||||
addx 3
|
||||
noop
|
||||
addx -40
|
||||
addx 4
|
||||
addx 28
|
||||
addx -27
|
||||
addx 5
|
||||
addx 2
|
||||
addx 5
|
||||
noop
|
||||
noop
|
||||
addx -2
|
||||
addx 2
|
||||
addx 5
|
||||
addx 3
|
||||
noop
|
||||
addx 2
|
||||
addx -25
|
||||
addx 30
|
||||
noop
|
||||
addx 3
|
||||
addx -2
|
||||
addx 2
|
||||
addx 5
|
||||
addx -39
|
||||
addx 29
|
||||
addx -27
|
||||
addx 5
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 4
|
||||
noop
|
||||
addx 1
|
||||
addx 2
|
||||
addx 5
|
||||
addx 2
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 5
|
||||
addx 1
|
||||
noop
|
||||
addx 2
|
||||
addx 5
|
||||
addx -32
|
||||
addx 34
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
""";
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -9,7 +9,7 @@ var days = Assembly.GetAssembly(typeof(Day))!.GetTypes()
|
||||
|
||||
var select = new SelectionPrompt<Day>()
|
||||
.Title("[cyan]Select a [yellow]day[/] to run:[/]")
|
||||
.AddChoices(days.Reverse());
|
||||
.AddChoices(days.OrderByDescending(d => d.Number));
|
||||
|
||||
var selectedDay = AnsiConsole.Prompt(select);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user