Added 10 10, still got to complete day 9 p 2

This commit is contained in:
Rob Stoffelen 2022-12-15 09:07:39 +01:00
parent 691a3e351c
commit cf2a2e1f39
5 changed files with 281 additions and 10 deletions

View File

@ -3,13 +3,16 @@
using AdventOfCodeLibrary._2022;
using AdventOfCodeLibrary.Shared;
string _demoData = @"30373
25512
65332
33549
35390";
string _demoData = @"R 4
U 4
L 3
D 1
R 4
D 1
L 5
R 2";
Answerable answerable = new Day08Part2();
Answerable answerable = new Day10Part2();
byte[] dataArray = File.ReadAllBytes(answerable.DefaultInputFile);
//dataArray = Encoding.UTF8.GetBytes(_demoData);

View File

@ -25,6 +25,12 @@
{
X--;
}
internal void Translate(int x, int y)
{
X += x;
Y += y;
}
}
internal class RopeWalker
@ -54,10 +60,10 @@
// do the actual move
switch (direction)
{
case 'U': HeadLocation.MoveUp(); break;
case 'D': HeadLocation.MoveDown(); break;
case 'R': HeadLocation.MoveRight(); break;
case 'L': HeadLocation.MoveLeft(); break;
case 'U': HeadLocation.Translate(0, 1); break;
case 'D': HeadLocation.Translate(0, -1); break;
case 'R': HeadLocation.Translate(1, 0); break;
case 'L': HeadLocation.Translate(-1, 0); break;
}
// check if the tail is touching
@ -90,5 +96,10 @@
_ => throw new AccessViolationException(),
};
}
private bool IsTailTouchingHead(VirtualPoint point1, VirtualPoint point2)
{
return !(Math.Abs(point1.X - point2.X) > 1) && !(Math.Abs(point1.Y - point2.Y) > 1);
}
}
}

View File

@ -0,0 +1,52 @@
namespace AdventOfCodeLibrary._2022
{
using AdventOfCodeLibrary.Shared;
public class Day10Part1 : Answerable
{
public override int Year { get; set; } = 2022;
public override int Day { get; set; } = 10;
public override int Part { get; set; } = 1;
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++)
{
Cycles++;
if (measureCycles.Contains(Cycles))
{
measureResult += Cycles * X;
}
}
X += valueToAdd;
}
}
}

View File

@ -0,0 +1,68 @@
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;
}
}
}

View File

@ -0,0 +1,137 @@
addx 1
noop
addx 2
addx 5
addx 3
noop
addx -1
addx 5
noop
noop
addx 5
noop
addx 3
noop
addx 6
addx -4
noop
noop
addx 5
noop
addx -32
addx 35
addx 5
addx -31
addx 7
addx -13
addx 2
addx 2
addx 5
addx 6
addx -5
addx 2
addx 5
addx 2
addx 2
addx -17
addx 18
addx 5
addx 2
addx -30
addx 31
addx 2
addx 2
addx -32
addx -1
addx 10
addx -8
noop
noop
addx 6
addx 16
noop
addx -11
addx 3
addx -2
addx 3
noop
addx 6
noop
addx -2
noop
addx 7
addx 3
addx -2
addx 4
addx -20
noop
addx -14
addx -2
addx 6
addx 2
addx 3
noop
addx 2
addx -1
addx 4
noop
addx 5
noop
addx 2
addx 3
addx -2
addx 3
noop
addx 4
noop
addx 5
noop
addx 2
addx -24
addx -15
addx 17
addx -10
addx 2
addx -5
addx 6
noop
addx 3
addx 2
addx 2
noop
addx 3
addx 2
addx 5
addx -2
addx 3
addx 2
addx 2
addx 5
addx 2
addx -18
addx -19
noop
addx 1
addx 2
addx 5
addx 3
noop
noop
noop
noop
addx 2
addx 5
addx 2
addx 3
noop
addx -8
addx 11
noop
noop
addx 2
addx 5
addx 2
addx 3
noop
addx -34
noop