Added day 9
This commit is contained in:
parent
04949eb733
commit
974083b721
@ -3,15 +3,18 @@
|
|||||||
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 Day08Part1();
|
Answerable answerable = new Day09Part1();
|
||||||
byte[] dataArray = File.ReadAllBytes(answerable.DefaultInputFile);
|
byte[] dataArray = File.ReadAllBytes(answerable.DefaultInputFile);
|
||||||
//dataArray = Encoding.UTF8.GetBytes(_demoData);
|
dataArray = Encoding.UTF8.GetBytes(_demoData);
|
||||||
|
|
||||||
Console.WriteLine($"Max value: {answerable.GetAnswer(dataArray)}");
|
Console.WriteLine($"Max value: {answerable.GetAnswer(dataArray)}");
|
||||||
Console.ReadKey(true);
|
Console.ReadKey(true);
|
||||||
20
Advent Of Code Library/2022/Day 09/Day09Part1.cs
Normal file
20
Advent Of Code Library/2022/Day 09/Day09Part1.cs
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
namespace AdventOfCodeLibrary._2022
|
||||||
|
{
|
||||||
|
using AdventOfCodeLibrary._2022.Day_09;
|
||||||
|
using AdventOfCodeLibrary.Shared;
|
||||||
|
|
||||||
|
public class Day09Part1 : Answerable
|
||||||
|
{
|
||||||
|
public override int Year { get; set; } = 2022;
|
||||||
|
public override int Day { get; set; } = 9;
|
||||||
|
public override int Part { get; set; } = 1;
|
||||||
|
|
||||||
|
public override string GetAnswer(byte[] data)
|
||||||
|
{
|
||||||
|
string[] movements = GetAsStringArray(data);
|
||||||
|
RopeWalker walker = new();
|
||||||
|
|
||||||
|
return walker.ProcessMovement(movements).ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
23
Advent Of Code Library/2022/Day 09/Day09Part2.cs
Normal file
23
Advent Of Code Library/2022/Day 09/Day09Part2.cs
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
namespace AdventOfCodeLibrary._2022
|
||||||
|
{
|
||||||
|
using AdventOfCodeLibrary.Shared;
|
||||||
|
|
||||||
|
public class Day09Part2 : Answerable
|
||||||
|
{
|
||||||
|
public override int Year { get; set; } = 2022;
|
||||||
|
public override int Day { get; set; } = 9;
|
||||||
|
public override int Part { get; set; } = 2;
|
||||||
|
|
||||||
|
public override string GetAnswer(byte[] data)
|
||||||
|
{
|
||||||
|
int markerLength = 14;
|
||||||
|
for (int skip = 0; skip < data.Length - 1 - markerLength; skip++)
|
||||||
|
{
|
||||||
|
if (data.Skip(skip).Take(markerLength).Distinct().Count() == markerLength)
|
||||||
|
return (skip + markerLength).ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
return "nothing found";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
94
Advent Of Code Library/2022/Day 09/RopeWalker.cs
Normal file
94
Advent Of Code Library/2022/Day 09/RopeWalker.cs
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
namespace AdventOfCodeLibrary._2022.Day_09
|
||||||
|
{
|
||||||
|
internal class VirtualPoint
|
||||||
|
{
|
||||||
|
internal int X { get; private set; } = 0;
|
||||||
|
|
||||||
|
internal int Y { get; private set; } = 0;
|
||||||
|
|
||||||
|
internal void MoveUp()
|
||||||
|
{
|
||||||
|
Y++;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal void MoveDown()
|
||||||
|
{
|
||||||
|
Y--;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal void MoveRight()
|
||||||
|
{
|
||||||
|
X++;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal void MoveLeft()
|
||||||
|
{
|
||||||
|
X--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal class RopeWalker
|
||||||
|
{
|
||||||
|
private VirtualPoint HeadLocation = new();
|
||||||
|
private VirtualPoint LastHeadLocation = new();
|
||||||
|
|
||||||
|
private VirtualPoint TailLocation = new();
|
||||||
|
|
||||||
|
private List<string> TailVistedLocations = new();
|
||||||
|
|
||||||
|
internal int ProcessMovement(string[] movements)
|
||||||
|
{
|
||||||
|
// add the start position
|
||||||
|
TailVistedLocations.Add($"0,0");
|
||||||
|
|
||||||
|
for (int movementIndex = 0; movementIndex < movements.Length; movementIndex++)
|
||||||
|
{
|
||||||
|
char direction = movements[movementIndex][0];
|
||||||
|
int amountToMove = Convert.ToInt32(movements[movementIndex].Substring(2));
|
||||||
|
|
||||||
|
for (int movement = 0; movement < amountToMove; movement++)
|
||||||
|
{
|
||||||
|
// set the last pos before moving
|
||||||
|
LastHeadLocation = HeadLocation;
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if the tail is touching
|
||||||
|
if (!IsTailTouchingHead(direction))
|
||||||
|
{
|
||||||
|
// if not, move tail to last head position
|
||||||
|
TailLocation = LastHeadLocation;
|
||||||
|
|
||||||
|
// add the new position to the list of visited positions
|
||||||
|
TailVistedLocations.Add($"{TailLocation.X},{TailLocation.Y}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return TailVistedLocations.Distinct().Count();
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool IsTailTouchingHead(char direction)
|
||||||
|
{
|
||||||
|
return direction switch
|
||||||
|
{
|
||||||
|
// up; Y + 1
|
||||||
|
'U' => TailLocation.Y + 1 >= HeadLocation.Y,
|
||||||
|
// down; Y - 1
|
||||||
|
'D' => TailLocation.Y - 1 <= HeadLocation.Y,
|
||||||
|
// right; X + 1
|
||||||
|
'R' => TailLocation.X + 1 >= HeadLocation.X,
|
||||||
|
// left; X - 1
|
||||||
|
'L' => TailLocation.X - 1 <= HeadLocation.X,
|
||||||
|
_ => throw new AccessViolationException(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
2000
Advent Of Code Library/2022/Day 09/day-09-input.txt
Normal file
2000
Advent Of Code Library/2022/Day 09/day-09-input.txt
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user