Added 10 10, still got to complete day 9 p 2
This commit is contained in:
parent
691a3e351c
commit
cf2a2e1f39
@ -3,13 +3,16 @@
|
|||||||
using AdventOfCodeLibrary._2022;
|
using AdventOfCodeLibrary._2022;
|
||||||
using AdventOfCodeLibrary.Shared;
|
using AdventOfCodeLibrary.Shared;
|
||||||
|
|
||||||
string _demoData = @"30373
|
string _demoData = @"R 4
|
||||||
25512
|
U 4
|
||||||
65332
|
L 3
|
||||||
33549
|
D 1
|
||||||
35390";
|
R 4
|
||||||
|
D 1
|
||||||
|
L 5
|
||||||
|
R 2";
|
||||||
|
|
||||||
Answerable answerable = new Day08Part2();
|
Answerable answerable = new Day10Part2();
|
||||||
byte[] dataArray = File.ReadAllBytes(answerable.DefaultInputFile);
|
byte[] dataArray = File.ReadAllBytes(answerable.DefaultInputFile);
|
||||||
//dataArray = Encoding.UTF8.GetBytes(_demoData);
|
//dataArray = Encoding.UTF8.GetBytes(_demoData);
|
||||||
|
|
||||||
|
|||||||
@ -25,6 +25,12 @@
|
|||||||
{
|
{
|
||||||
X--;
|
X--;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal void Translate(int x, int y)
|
||||||
|
{
|
||||||
|
X += x;
|
||||||
|
Y += y;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal class RopeWalker
|
internal class RopeWalker
|
||||||
@ -54,10 +60,10 @@
|
|||||||
// do the actual move
|
// do the actual move
|
||||||
switch (direction)
|
switch (direction)
|
||||||
{
|
{
|
||||||
case 'U': HeadLocation.MoveUp(); break;
|
case 'U': HeadLocation.Translate(0, 1); break;
|
||||||
case 'D': HeadLocation.MoveDown(); break;
|
case 'D': HeadLocation.Translate(0, -1); break;
|
||||||
case 'R': HeadLocation.MoveRight(); break;
|
case 'R': HeadLocation.Translate(1, 0); break;
|
||||||
case 'L': HeadLocation.MoveLeft(); break;
|
case 'L': HeadLocation.Translate(-1, 0); break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if the tail is touching
|
// check if the tail is touching
|
||||||
@ -90,5 +96,10 @@
|
|||||||
_ => throw new AccessViolationException(),
|
_ => 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
52
Advent Of Code Library/2022/Day 10/Day10Part1.cs
Normal file
52
Advent Of Code Library/2022/Day 10/Day10Part1.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
68
Advent Of Code Library/2022/Day 10/Day10Part2.cs
Normal file
68
Advent Of Code Library/2022/Day 10/Day10Part2.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
137
Advent Of Code Library/2022/Day 10/day-10-input.txt
Normal file
137
Advent Of Code Library/2022/Day 10/day-10-input.txt
Normal 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
|
||||||
Loading…
Reference in New Issue
Block a user