AdventOfCode/Year2022/Day9.cs

99 lines
3.7 KiB
C#
Raw Normal View History

2023-06-08 23:43:55 +00:00
using AdventOfCode.Utils;
2023-06-08 20:09:21 +00:00
namespace AdventOfCode.Year2022;
public class Day9
{
public Day9()
{
Console.WriteLine("Day9 Solution");
2023-06-08 23:43:55 +00:00
List<(char, int)> moves = new List<(char, int)>();
2023-06-08 20:09:21 +00:00
File.ReadAllLines("inputs/day9.txt").ToList().ForEach(line =>
{
string[] split = line.Split(" ");
2023-06-08 23:43:55 +00:00
moves.Add((Convert.ToChar(split[0]), Convert.ToInt32(split[1])));
2023-06-08 20:09:21 +00:00
});
2023-06-08 23:43:55 +00:00
Console.WriteLine($"Part1 Result: {GetPositionCount(moves, 2)}");
Console.WriteLine($"Part2 Result: {GetPositionCount(moves, 10)}");
2023-06-08 20:09:21 +00:00
Console.WriteLine("\n=============================\n");
}
2023-06-08 23:43:55 +00:00
private static int GetPositionCount(List<(char, int)> moves, int knots)
2023-06-08 20:09:21 +00:00
{
2023-06-08 23:43:55 +00:00
List<List<int>> currentPositions = new List<List<int>>();
currentPositions.Fill(knots, new List<int>(new[]{0,0}));
HashSet<(int, int)> tailHistory = new HashSet<(int, int)>();
2023-06-08 20:09:21 +00:00
2023-06-08 23:43:55 +00:00
foreach ((char, int) move in moves)
for (int moveN = 0; moveN < move.Item2; moveN++)
2023-06-08 20:09:21 +00:00
{
2023-06-08 23:43:55 +00:00
int[] vector = GetVector(move.Item1);
int[] previousTailPosition = currentPositions[0].ToArray();
currentPositions[0] = UpdateHead(vector, currentPositions[0]);
for (int tailN = 0; tailN < knots-1; tailN++)
2023-06-08 20:09:21 +00:00
{
2023-06-08 23:43:55 +00:00
int[] nextPreviousTailPosition = currentPositions[tailN + 1].ToArray();
currentPositions[tailN + 1] = UpdateTail(currentPositions[tailN], currentPositions[tailN + 1], previousTailPosition);
previousTailPosition = nextPreviousTailPosition;
if (tailN == knots - 2)
tailHistory.Add((currentPositions[knots - 1][0], currentPositions[knots - 1][1]));
2023-06-08 20:09:21 +00:00
}
}
2023-06-08 23:43:55 +00:00
return tailHistory.Count;
2023-06-08 20:09:21 +00:00
}
2023-06-08 23:43:55 +00:00
private static List<int> UpdateHead(int[] vector, List<int> currentPosition)
2023-06-08 20:09:21 +00:00
{
2023-06-08 23:43:55 +00:00
currentPosition[0] += vector[0];
currentPosition[1] += vector[1];
return new List<int> { currentPosition[0], currentPosition[1] };
}
2023-06-08 20:09:21 +00:00
2023-06-08 23:43:55 +00:00
private static List<int> UpdateTail(List<int> currentHeadPosition, List<int> currentTailPosition, int[] previousTailPosition)
{
List<int> head = new List<int>
{
currentHeadPosition[0] - previousTailPosition[0],
currentHeadPosition[1] - previousTailPosition[1]
};
if (Math.Abs(currentHeadPosition[0] - currentTailPosition[0]) > 1 ||
Math.Abs(currentHeadPosition[1] - currentTailPosition[1]) > 1)
2023-06-08 20:09:21 +00:00
{
2023-06-08 23:43:55 +00:00
if (previousTailPosition[0] == currentTailPosition[0] ||
previousTailPosition[1] == currentTailPosition[1])
return new List<int>
{
currentTailPosition[0] + head[0],
currentTailPosition[1] + head[1]
};
2023-06-08 20:09:21 +00:00
2023-06-08 23:43:55 +00:00
List<int> difference = new List<int>
{
currentHeadPosition[0] - currentTailPosition[0],
currentHeadPosition[1] - currentTailPosition[1]
};
return new List<int>
{
currentTailPosition[0] + difference[0].Signal() * Math.Min(Math.Abs(difference[0]), 1),
currentTailPosition[1] + difference[1].Signal() * Math.Min(Math.Abs(difference[1]), 1)
};
2023-06-08 20:09:21 +00:00
}
2023-06-08 23:43:55 +00:00
return currentTailPosition;
2023-06-08 20:09:21 +00:00
}
2023-06-08 23:43:55 +00:00
private static int[] GetVector(char direction)
{
return direction switch
{
'U' => new[] { 1, 0 },
'D' => new[] { -1, 0 },
'R' => new[] { 0, 1 },
'L' => new[] { 0, -1 },
_ => new[] { 0, 0 }
};
}
2023-06-08 20:09:21 +00:00
}