Day 10
This commit is contained in:
@@ -22,6 +22,10 @@
|
||||
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove="Problems\AOC2022\Day10\input.txt" />
|
||||
<None Remove="Problems\AOC2022\Day10\sara.txt" />
|
||||
<None Remove="Problems\AOC2022\Day10\simple.txt" />
|
||||
<None Remove="Problems\AOC2022\Day10\test.txt" />
|
||||
<None Remove="Problems\AOC2022\Day3\input.txt" />
|
||||
<None Remove="Problems\AOC2022\Day3\test.txt" />
|
||||
<None Remove="Problems\AOC2022\Day4\input.txt" />
|
||||
@@ -32,6 +36,9 @@
|
||||
<None Remove="Problems\AOC2022\Day6\test.txt" />
|
||||
<None Remove="Problems\AOC2022\Day8\input.txt" />
|
||||
<None Remove="Problems\AOC2022\Day8\test.txt" />
|
||||
<None Remove="Problems\AOC2022\Day9\input.txt" />
|
||||
<None Remove="Problems\AOC2022\Day9\test.txt" />
|
||||
<None Remove="Problems\AOC2022\Day9\test2.txt" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
|
||||
74
AdventOfCode/Problems/AOC2022/Day10/CathodeCPU.cs
Normal file
74
AdventOfCode/Problems/AOC2022/Day10/CathodeCPU.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AdventOfCode.Problems.AOC2022.Day10;
|
||||
internal class CathodeCPU
|
||||
{
|
||||
public enum Instruction
|
||||
{
|
||||
NoOp,
|
||||
AddX
|
||||
}
|
||||
|
||||
public int X { get; private set; } = 1;
|
||||
private int _cycleNumber = 1;
|
||||
private int _programCounter;
|
||||
private int _pending = -1;
|
||||
|
||||
public int[] ExecuteCode((Instruction ins, int value)[] code, int[] outputCycles, Func<int, int, int>? processor = null)
|
||||
{
|
||||
var result = new int[outputCycles.Length];
|
||||
|
||||
var ridx = 0;
|
||||
|
||||
if (processor == null)
|
||||
processor = (c, x) => c * x;
|
||||
|
||||
ExecuteCode(code, (c, x) =>
|
||||
{
|
||||
if (ridx < outputCycles.Length && c == outputCycles[ridx])
|
||||
{
|
||||
result[ridx] = processor(c, x);
|
||||
ridx++;
|
||||
}
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
public void ExecuteCode((Instruction ins, int value)[] code, Action<int, int> processor)
|
||||
{
|
||||
|
||||
while (_programCounter < code.Length)
|
||||
{
|
||||
var (ins, value) = code[_programCounter];
|
||||
|
||||
processor(_cycleNumber, X);
|
||||
|
||||
switch ((ins, _pending))
|
||||
{
|
||||
case { ins: Instruction.NoOp }:
|
||||
_programCounter++;
|
||||
break;
|
||||
case { ins: Instruction.AddX, _pending : -1 }:
|
||||
_pending = 1;
|
||||
break;
|
||||
case { ins: Instruction.AddX, _pending: 0 }:
|
||||
X += value;
|
||||
_programCounter++;
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
|
||||
if(_pending >= 0)
|
||||
_pending--;
|
||||
_cycleNumber++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
56
AdventOfCode/Problems/AOC2022/Day10/CathodeRayTube.cs
Normal file
56
AdventOfCode/Problems/AOC2022/Day10/CathodeRayTube.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using AdventOfCode.Runner.Attributes;
|
||||
|
||||
namespace AdventOfCode.Problems.AOC2022.Day10;
|
||||
|
||||
[ProblemInfo(2022, 10, "Cathode-Ray Tube")]
|
||||
internal class CathodeRayTube : Problem<int, string>
|
||||
{
|
||||
private (CathodeCPU.Instruction ins, int value)[] _code = Array.Empty<(CathodeCPU.Instruction ins, int value)>();
|
||||
|
||||
public override void CalculatePart1()
|
||||
{
|
||||
var cpu = new CathodeCPU();
|
||||
var result = cpu.ExecuteCode(_code, new[] { 20, 60, 100, 140, 180, 220 });
|
||||
Part1 = result.Sum();
|
||||
}
|
||||
|
||||
public override void CalculatePart2()
|
||||
{
|
||||
var output = Enumerable.Repeat(' ', 6 * 40).ToArray();
|
||||
|
||||
var cpu = new CathodeCPU();
|
||||
cpu.ExecuteCode(_code, (cycle, signal) =>
|
||||
{
|
||||
cycle -= 1;
|
||||
if (cycle > output.Length)
|
||||
return;
|
||||
|
||||
|
||||
var pos = signal % 40;
|
||||
var head = (cycle % 40);
|
||||
var sprite = Math.Abs(pos - head);
|
||||
if(sprite <= 1)
|
||||
output[cycle] = '█';
|
||||
});
|
||||
|
||||
var lines = output.Chunk(40).Select(r => new string(r));
|
||||
Part2 = $"\n{string.Join("\n", lines)}";
|
||||
}
|
||||
|
||||
public override void LoadInput()
|
||||
{
|
||||
var lines = ReadInputLines("input.txt");
|
||||
_code = new (CathodeCPU.Instruction ins, int value)[lines.Length];
|
||||
for (int i = 0; i < lines.Length; i++)
|
||||
{
|
||||
var ln = lines[i];
|
||||
if (ln == "noop")
|
||||
_code[i] = (CathodeCPU.Instruction.NoOp, 0);
|
||||
else
|
||||
{
|
||||
var instruction = ln.Split(' ');
|
||||
_code[i] = (Enum.Parse<CathodeCPU.Instruction>(instruction[0], true), int.Parse(instruction[1]));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
144
AdventOfCode/Problems/AOC2022/Day10/input.txt
Normal file
144
AdventOfCode/Problems/AOC2022/Day10/input.txt
Normal file
@@ -0,0 +1,144 @@
|
||||
addx 1
|
||||
noop
|
||||
addx 29
|
||||
addx -24
|
||||
addx 4
|
||||
addx 3
|
||||
addx -2
|
||||
addx 3
|
||||
addx 1
|
||||
addx 5
|
||||
addx 3
|
||||
addx -2
|
||||
addx 2
|
||||
noop
|
||||
noop
|
||||
addx 7
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 5
|
||||
addx 1
|
||||
noop
|
||||
addx -38
|
||||
addx 21
|
||||
addx 8
|
||||
noop
|
||||
addx -19
|
||||
addx -2
|
||||
addx 2
|
||||
addx 5
|
||||
addx 2
|
||||
addx -12
|
||||
addx 13
|
||||
addx 2
|
||||
addx 5
|
||||
addx 2
|
||||
addx -18
|
||||
addx 23
|
||||
noop
|
||||
addx -15
|
||||
addx 16
|
||||
addx 7
|
||||
noop
|
||||
noop
|
||||
addx -38
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 8
|
||||
addx 2
|
||||
addx 3
|
||||
addx -2
|
||||
addx 4
|
||||
noop
|
||||
noop
|
||||
addx 5
|
||||
addx 3
|
||||
noop
|
||||
addx 2
|
||||
addx 5
|
||||
noop
|
||||
noop
|
||||
addx -2
|
||||
noop
|
||||
addx 3
|
||||
addx 6
|
||||
noop
|
||||
addx -38
|
||||
addx -1
|
||||
addx 35
|
||||
addx -6
|
||||
addx -19
|
||||
addx -2
|
||||
addx 2
|
||||
addx 5
|
||||
addx 2
|
||||
addx 3
|
||||
noop
|
||||
addx 2
|
||||
addx 3
|
||||
addx -2
|
||||
addx 2
|
||||
noop
|
||||
addx -9
|
||||
addx 16
|
||||
noop
|
||||
addx 9
|
||||
addx -3
|
||||
addx -36
|
||||
addx -2
|
||||
addx 11
|
||||
addx 22
|
||||
addx -28
|
||||
noop
|
||||
addx 3
|
||||
addx 2
|
||||
addx 5
|
||||
addx 2
|
||||
addx 3
|
||||
addx -2
|
||||
addx 2
|
||||
noop
|
||||
addx 3
|
||||
addx 2
|
||||
noop
|
||||
addx -11
|
||||
addx 16
|
||||
addx 2
|
||||
addx 5
|
||||
addx -31
|
||||
noop
|
||||
addx -6
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 7
|
||||
addx 30
|
||||
addx -24
|
||||
addx -1
|
||||
addx 5
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 5
|
||||
noop
|
||||
addx 5
|
||||
noop
|
||||
addx 1
|
||||
noop
|
||||
addx 2
|
||||
addx 5
|
||||
addx 2
|
||||
addx 1
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
139
AdventOfCode/Problems/AOC2022/Day10/sara.txt
Normal file
139
AdventOfCode/Problems/AOC2022/Day10/sara.txt
Normal file
@@ -0,0 +1,139 @@
|
||||
noop
|
||||
noop
|
||||
addx 5
|
||||
addx 31
|
||||
addx -30
|
||||
addx 2
|
||||
addx 7
|
||||
noop
|
||||
noop
|
||||
addx -4
|
||||
addx 5
|
||||
addx 6
|
||||
noop
|
||||
addx -1
|
||||
addx 5
|
||||
addx -1
|
||||
addx 5
|
||||
addx 1
|
||||
noop
|
||||
addx 5
|
||||
noop
|
||||
addx -1
|
||||
addx -35
|
||||
addx 3
|
||||
noop
|
||||
addx 2
|
||||
addx 3
|
||||
addx -2
|
||||
addx 2
|
||||
noop
|
||||
addx 8
|
||||
addx -3
|
||||
addx 5
|
||||
addx -17
|
||||
addx 22
|
||||
addx -2
|
||||
addx 2
|
||||
addx 5
|
||||
addx -2
|
||||
addx -26
|
||||
addx 31
|
||||
addx 2
|
||||
addx 5
|
||||
addx -40
|
||||
addx 30
|
||||
addx -27
|
||||
addx 4
|
||||
addx 2
|
||||
addx 3
|
||||
addx -3
|
||||
addx 8
|
||||
noop
|
||||
noop
|
||||
addx 2
|
||||
addx 21
|
||||
addx -15
|
||||
addx -2
|
||||
addx 2
|
||||
noop
|
||||
addx 15
|
||||
addx -16
|
||||
addx 8
|
||||
noop
|
||||
addx 3
|
||||
addx 5
|
||||
addx -38
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 5
|
||||
addx -5
|
||||
addx 6
|
||||
addx 2
|
||||
addx 7
|
||||
noop
|
||||
noop
|
||||
addx 4
|
||||
addx -3
|
||||
noop
|
||||
noop
|
||||
addx 7
|
||||
addx 2
|
||||
addx 2
|
||||
addx -1
|
||||
noop
|
||||
addx 3
|
||||
addx 6
|
||||
noop
|
||||
addx 1
|
||||
noop
|
||||
noop
|
||||
addx -38
|
||||
noop
|
||||
noop
|
||||
addx 7
|
||||
addx 3
|
||||
noop
|
||||
addx 2
|
||||
addx -2
|
||||
addx 7
|
||||
addx -2
|
||||
addx 5
|
||||
addx 2
|
||||
addx 5
|
||||
addx -4
|
||||
addx 2
|
||||
addx 5
|
||||
addx 2
|
||||
addx -21
|
||||
addx 9
|
||||
addx 15
|
||||
noop
|
||||
addx 3
|
||||
addx -38
|
||||
addx 7
|
||||
noop
|
||||
noop
|
||||
addx 18
|
||||
addx -17
|
||||
addx 4
|
||||
noop
|
||||
addx 1
|
||||
addx 2
|
||||
addx 5
|
||||
addx 3
|
||||
noop
|
||||
noop
|
||||
addx 14
|
||||
addx -9
|
||||
noop
|
||||
noop
|
||||
addx 4
|
||||
addx 1
|
||||
noop
|
||||
addx 4
|
||||
addx 3
|
||||
noop
|
||||
addx -8
|
||||
noop
|
||||
3
AdventOfCode/Problems/AOC2022/Day10/simple.txt
Normal file
3
AdventOfCode/Problems/AOC2022/Day10/simple.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
noop
|
||||
addx 3
|
||||
addx -5
|
||||
146
AdventOfCode/Problems/AOC2022/Day10/test.txt
Normal file
146
AdventOfCode/Problems/AOC2022/Day10/test.txt
Normal file
@@ -0,0 +1,146 @@
|
||||
addx 15
|
||||
addx -11
|
||||
addx 6
|
||||
addx -3
|
||||
addx 5
|
||||
addx -1
|
||||
addx -8
|
||||
addx 13
|
||||
addx 4
|
||||
noop
|
||||
addx -1
|
||||
addx 5
|
||||
addx -1
|
||||
addx 5
|
||||
addx -1
|
||||
addx 5
|
||||
addx -1
|
||||
addx 5
|
||||
addx -1
|
||||
addx -35
|
||||
addx 1
|
||||
addx 24
|
||||
addx -19
|
||||
addx 1
|
||||
addx 16
|
||||
addx -11
|
||||
noop
|
||||
noop
|
||||
addx 21
|
||||
addx -15
|
||||
noop
|
||||
noop
|
||||
addx -3
|
||||
addx 9
|
||||
addx 1
|
||||
addx -3
|
||||
addx 8
|
||||
addx 1
|
||||
addx 5
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx -36
|
||||
noop
|
||||
addx 1
|
||||
addx 7
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 2
|
||||
addx 6
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 1
|
||||
noop
|
||||
noop
|
||||
addx 7
|
||||
addx 1
|
||||
noop
|
||||
addx -13
|
||||
addx 13
|
||||
addx 7
|
||||
noop
|
||||
addx 1
|
||||
addx -33
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 2
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 8
|
||||
noop
|
||||
addx -1
|
||||
addx 2
|
||||
addx 1
|
||||
noop
|
||||
addx 17
|
||||
addx -9
|
||||
addx 1
|
||||
addx 1
|
||||
addx -3
|
||||
addx 11
|
||||
noop
|
||||
noop
|
||||
addx 1
|
||||
noop
|
||||
addx 1
|
||||
noop
|
||||
noop
|
||||
addx -13
|
||||
addx -19
|
||||
addx 1
|
||||
addx 3
|
||||
addx 26
|
||||
addx -30
|
||||
addx 12
|
||||
addx -1
|
||||
addx 3
|
||||
addx 1
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx -9
|
||||
addx 18
|
||||
addx 1
|
||||
addx 2
|
||||
noop
|
||||
noop
|
||||
addx 9
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx -1
|
||||
addx 2
|
||||
addx -37
|
||||
addx 1
|
||||
addx 3
|
||||
noop
|
||||
addx 15
|
||||
addx -21
|
||||
addx 22
|
||||
addx -6
|
||||
addx 1
|
||||
noop
|
||||
addx 2
|
||||
addx 1
|
||||
noop
|
||||
addx -10
|
||||
noop
|
||||
noop
|
||||
addx 20
|
||||
addx 1
|
||||
addx 2
|
||||
addx 2
|
||||
addx -6
|
||||
addx -11
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
Reference in New Issue
Block a user