Compare commits
10 Commits
04949eb733
...
2ad0676c49
| Author | SHA1 | Date | |
|---|---|---|---|
| 2ad0676c49 | |||
|
|
0e5a63d58a | ||
| ee6da383ca | |||
|
|
389a85869b | ||
|
|
43c01b9db7 | ||
|
|
de2ce7e9b0 | ||
|
|
08dbff1008 | ||
|
|
cf2a2e1f39 | ||
| 691a3e351c | |||
|
|
974083b721 |
@ -9,7 +9,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\Advent Of Code Library\Advent Of Code Library.csproj" />
|
<ProjectReference Include="..\Advent Of Code Library\AdventOfCode.Library.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
@ -3,15 +3,15 @@
|
|||||||
using AdventOfCodeLibrary._2022;
|
using AdventOfCodeLibrary._2022;
|
||||||
using AdventOfCodeLibrary.Shared;
|
using AdventOfCodeLibrary.Shared;
|
||||||
|
|
||||||
string _demoData = @"30373
|
string _demoData = @"Sabqponm
|
||||||
25512
|
abcryxxl
|
||||||
65332
|
accszExk
|
||||||
33549
|
acctuvwj
|
||||||
35390";
|
abdefghi";
|
||||||
|
|
||||||
Answerable answerable = new Day08Part1();
|
Answerable answerable = new Day12Part1();
|
||||||
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($"Answer: {answerable.GetAnswer(dataArray)}");
|
||||||
Console.ReadKey(true);
|
Console.ReadKey(true);
|
||||||
@ -1,6 +1,5 @@
|
|||||||
namespace AdventOfCodeLibrary._2022
|
namespace AdventOfCodeLibrary._2022
|
||||||
{
|
{
|
||||||
using AdventOfCodeLibrary._2022.Day_08;
|
|
||||||
using AdventOfCodeLibrary.Shared;
|
using AdventOfCodeLibrary.Shared;
|
||||||
|
|
||||||
public class Day08Part1 : Answerable
|
public class Day08Part1 : Answerable
|
||||||
@ -13,8 +12,22 @@
|
|||||||
{
|
{
|
||||||
string[] treeGrid = GetAsStringArray(data);
|
string[] treeGrid = GetAsStringArray(data);
|
||||||
|
|
||||||
TreeChecker checker = new TreeChecker();
|
int visibleTreeCount = 0;
|
||||||
return checker.VisibleTrees(treeGrid).ToString();
|
for (int row = 0; row < treeGrid.Length; row++)
|
||||||
|
{
|
||||||
|
for (int col = 0; col < treeGrid[row].Length; col++)
|
||||||
|
{
|
||||||
|
char treeHeight = treeGrid[row][col];
|
||||||
|
bool visibleFromLeft = !Enumerable.Range(0, col).Any(leftIndex => treeGrid[row][leftIndex] >= treeHeight);
|
||||||
|
bool visibleFromRight = !Enumerable.Range(col + 1, treeGrid[row].Length - col - 1).Any(rightIndex => treeGrid[row][rightIndex] >= treeHeight);
|
||||||
|
bool visibleFromTop = !Enumerable.Range(0, row).Any(topIndex => treeGrid[topIndex][col] >= treeHeight);
|
||||||
|
bool visibleFromBottom = !Enumerable.Range(row + 1, treeGrid.Length - row - 1).Any(bottomIndex => treeGrid[bottomIndex][col] >= treeHeight);
|
||||||
|
|
||||||
|
if (visibleFromBottom || visibleFromLeft || visibleFromRight || visibleFromTop) visibleTreeCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return visibleTreeCount.ToString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,14 +10,47 @@
|
|||||||
|
|
||||||
public override string GetAnswer(byte[] data)
|
public override string GetAnswer(byte[] data)
|
||||||
{
|
{
|
||||||
int markerLength = 14;
|
string[] treeGrid = GetAsStringArray(data);
|
||||||
for (int skip = 0; skip < data.Length - 1 - markerLength; skip++)
|
|
||||||
|
int highestScenicScore = 0;
|
||||||
|
for (int row = 0; row < treeGrid.Length; row++)
|
||||||
{
|
{
|
||||||
if (data.Skip(skip).Take(markerLength).Distinct().Count() == markerLength)
|
for (int col = 0; col < treeGrid[row].Length; col++)
|
||||||
return (skip + markerLength).ToString();
|
{
|
||||||
|
if (row == 0 || col == 0 || row == treeGrid.Length - 1 || col == treeGrid[row].Length - 1)
|
||||||
|
{
|
||||||
|
// skip the edges
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// get the height of the current tree
|
||||||
|
char treeHeight = treeGrid[row][col];
|
||||||
|
|
||||||
|
List<char> leftTrees = treeGrid[row].Take(col).ToList();
|
||||||
|
int firstLeftBlockingTreeIdx = leftTrees.FindLastIndex(height => height >= treeHeight);
|
||||||
|
int viewCountFromLeft = firstLeftBlockingTreeIdx == -1 ? leftTrees.Count : col - firstLeftBlockingTreeIdx;
|
||||||
|
|
||||||
|
List<char> rightTrees = treeGrid[row].Skip(col + 1).Take(treeGrid[row].Length - col - 1).Reverse().ToList();
|
||||||
|
int firstRightBlockingTreeIdx = rightTrees.FindLastIndex(height => height >= treeHeight);
|
||||||
|
int viewCountFromRight = firstRightBlockingTreeIdx == -1 ? rightTrees.Count : treeGrid[row].Length - col - firstRightBlockingTreeIdx - 1;
|
||||||
|
|
||||||
|
List<char> topTrees = treeGrid.Skip(0).Take(row).Select(ints => ints[col]).ToList();
|
||||||
|
int firstTopBlockingTreeIdx = topTrees.FindLastIndex(height => height >= treeHeight);
|
||||||
|
int viewCountFromTop = firstTopBlockingTreeIdx == -1 ? topTrees.Count : row - firstTopBlockingTreeIdx;
|
||||||
|
|
||||||
|
List<char> bottomTrees = treeGrid.Skip(row + 1).Take(treeGrid.Length - row - 1).Select(ints => ints[col]).Reverse().ToList();
|
||||||
|
int firstBottomBlockingTreeIdx = bottomTrees.FindLastIndex(height => height >= treeHeight);
|
||||||
|
int viewCountFromBottom = firstBottomBlockingTreeIdx == -1 ? bottomTrees.Count : treeGrid.Length - row - firstBottomBlockingTreeIdx - 1;
|
||||||
|
|
||||||
|
int scenicScore = viewCountFromLeft * viewCountFromRight * viewCountFromTop * viewCountFromBottom;
|
||||||
|
if (scenicScore > highestScenicScore)
|
||||||
|
{
|
||||||
|
highestScenicScore = scenicScore;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return "nothing found";
|
return highestScenicScore.ToString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,118 +0,0 @@
|
|||||||
namespace AdventOfCodeLibrary._2022.Day_08
|
|
||||||
{
|
|
||||||
internal struct Tree
|
|
||||||
{
|
|
||||||
internal int X;
|
|
||||||
internal int Y;
|
|
||||||
internal char Height;
|
|
||||||
internal bool IsVisible;
|
|
||||||
|
|
||||||
internal Tree(int x, int y, char height)
|
|
||||||
{
|
|
||||||
X = x;
|
|
||||||
Y = y;
|
|
||||||
Height = height;
|
|
||||||
IsVisible = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal class TreeChecker
|
|
||||||
{
|
|
||||||
private Tree[][] treeGrid;
|
|
||||||
|
|
||||||
private Tree[][] rotatedGrid;
|
|
||||||
|
|
||||||
internal int VisibleTrees(string[] treeArray)
|
|
||||||
{
|
|
||||||
treeGrid = new Tree[treeArray.Length][];
|
|
||||||
rotatedGrid = new Tree[treeArray[0].Length][];
|
|
||||||
|
|
||||||
for (int verticalIndex = 0; verticalIndex < treeArray.Length; verticalIndex++)
|
|
||||||
{
|
|
||||||
bool firstVertial = true;
|
|
||||||
for (int horizontalIndex = 0; horizontalIndex < treeArray[0].Length; horizontalIndex++)
|
|
||||||
{
|
|
||||||
if (firstVertial)
|
|
||||||
{
|
|
||||||
treeGrid[verticalIndex] = new Tree[treeArray[0].Length];
|
|
||||||
|
|
||||||
firstVertial = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (verticalIndex == 0)
|
|
||||||
{
|
|
||||||
rotatedGrid[horizontalIndex] = new Tree[treeArray.Length];
|
|
||||||
}
|
|
||||||
|
|
||||||
Tree tree = new Tree(verticalIndex, horizontalIndex, treeArray[verticalIndex][horizontalIndex]);
|
|
||||||
|
|
||||||
//Console.WriteLine($"Adding Tree to [{verticalIndex}][{horizontalIndex}] and [{horizontalIndex}][{verticalIndex}]");
|
|
||||||
|
|
||||||
treeGrid[verticalIndex][horizontalIndex] = tree;
|
|
||||||
rotatedGrid[horizontalIndex][verticalIndex] = tree;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int visible = 0;
|
|
||||||
|
|
||||||
// start from the top
|
|
||||||
Tree[] currentHighest = treeGrid[0];
|
|
||||||
|
|
||||||
// from top to bottom
|
|
||||||
for (int treeIndex = 1; treeIndex < treeArray.Length - 2; treeIndex++)
|
|
||||||
{
|
|
||||||
int tmp = CheckVisibleTrees(ref currentHighest, treeGrid[treeIndex]);
|
|
||||||
visible += tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
currentHighest = treeGrid[^1];
|
|
||||||
// from bottom to top
|
|
||||||
for (int treeIndex = treeArray.Length - 1; treeIndex > 1 ; treeIndex--)
|
|
||||||
{
|
|
||||||
int tmp = CheckVisibleTrees(ref currentHighest, treeGrid[treeIndex]);
|
|
||||||
visible += tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
currentHighest = rotatedGrid[0];
|
|
||||||
// from top to bottom with the rotated array
|
|
||||||
for (int treeIndex = rotatedGrid.Length - 1; treeIndex > 1; treeIndex--)
|
|
||||||
{
|
|
||||||
int tmp = CheckVisibleTrees(ref currentHighest, rotatedGrid[treeIndex]);
|
|
||||||
visible += tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
currentHighest = rotatedGrid[^1];
|
|
||||||
// from bottom to top with the rotated array
|
|
||||||
for (int treeIndex = rotatedGrid.Length - 1; treeIndex > 1; treeIndex--)
|
|
||||||
{
|
|
||||||
int tmp = CheckVisibleTrees(ref currentHighest, rotatedGrid[treeIndex]);
|
|
||||||
visible += tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
// add the outside trees
|
|
||||||
int edgeTrees = (2 * treeGrid.Length) + (2 * (treeGrid[0].Length - 2));
|
|
||||||
|
|
||||||
// return the result
|
|
||||||
return visible + edgeTrees;
|
|
||||||
}
|
|
||||||
|
|
||||||
private int CheckVisibleTrees(ref Tree[] currentHighest, Tree[] treeLineToCheck)
|
|
||||||
{
|
|
||||||
int visible = 0;
|
|
||||||
for (int treeIndex = 1; treeIndex < treeLineToCheck.Length - 1; treeIndex++)
|
|
||||||
{
|
|
||||||
Tree toCheck = treeLineToCheck[treeIndex];
|
|
||||||
int diff = toCheck.Height - currentHighest[treeIndex].Height;
|
|
||||||
if (diff > 0 && !treeGrid[toCheck.X][toCheck.Y].IsVisible)
|
|
||||||
{
|
|
||||||
visible++;
|
|
||||||
treeGrid[toCheck.X][toCheck.Y].IsVisible = true;
|
|
||||||
currentHighest[treeIndex] = treeLineToCheck[treeIndex];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return visible;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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, 2).ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
20
Advent Of Code Library/2022/Day 09/Day09Part2.cs
Normal file
20
Advent Of Code Library/2022/Day 09/Day09Part2.cs
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
namespace AdventOfCodeLibrary._2022
|
||||||
|
{
|
||||||
|
using AdventOfCodeLibrary._2022.Day_09;
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
string[] movements = GetAsStringArray(data);
|
||||||
|
RopeWalker walker = new();
|
||||||
|
|
||||||
|
return walker.ProcessMovement(movements, 10).ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
75
Advent Of Code Library/2022/Day 09/RopeWalker.cs
Normal file
75
Advent Of Code Library/2022/Day 09/RopeWalker.cs
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
namespace AdventOfCodeLibrary._2022.Day_09
|
||||||
|
{
|
||||||
|
internal class VirtualPoint
|
||||||
|
{
|
||||||
|
internal int X { get; set; } = 0;
|
||||||
|
|
||||||
|
internal int Y { get; set; } = 0;
|
||||||
|
|
||||||
|
internal void Translate(int x, int y)
|
||||||
|
{
|
||||||
|
X += x;
|
||||||
|
Y += y;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal class RopeWalker
|
||||||
|
{
|
||||||
|
private List<string> TailVistedLocations = new();
|
||||||
|
|
||||||
|
internal int ProcessMovement(string[] movements, int ropeLength)
|
||||||
|
{
|
||||||
|
VirtualPoint[] rope = new VirtualPoint[ropeLength];
|
||||||
|
for (int ropeIndex = 0; ropeIndex < rope.Length; ropeIndex++)
|
||||||
|
{
|
||||||
|
rope[ropeIndex] = new VirtualPoint();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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++)
|
||||||
|
{
|
||||||
|
|
||||||
|
// do the actual move
|
||||||
|
switch (direction)
|
||||||
|
{
|
||||||
|
case 'U': rope[0].Translate(0, 1); break;
|
||||||
|
case 'D': rope[0].Translate(0, -1); break;
|
||||||
|
case 'R': rope[0].Translate(1, 0); break;
|
||||||
|
case 'L': rope[0].Translate(-1, 0); break;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int ropeIndex = 1; ropeIndex < rope.Length; ropeIndex++)
|
||||||
|
{
|
||||||
|
if (!IsTailTouchingHead(rope[ropeIndex - 1], rope[ropeIndex]))
|
||||||
|
{
|
||||||
|
rope[ropeIndex].Translate(Math.Sign(rope[ropeIndex - 1].X - rope[ropeIndex].X), Math.Sign(rope[ropeIndex - 1].Y - rope[ropeIndex].Y));
|
||||||
|
|
||||||
|
// add the new position to the list of visited positions
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// if not touching nothing has changed so stop with checking
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
TailVistedLocations.Add($"{rope[^1].X},{rope[^1].Y}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return TailVistedLocations.Distinct().Count();
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool IsTailTouchingHead(VirtualPoint point1, VirtualPoint point2)
|
||||||
|
{
|
||||||
|
return !(Math.Abs(point1.X - point2.X) > 1) && !(Math.Abs(point1.Y - point2.Y) > 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
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
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
|
||||||
45
Advent Of Code Library/2022/Day 11/Day11Part1.cs
Normal file
45
Advent Of Code Library/2022/Day 11/Day11Part1.cs
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
namespace AdventOfCodeLibrary._2022
|
||||||
|
{
|
||||||
|
using AdventOfCodeLibrary._2022.Day_11;
|
||||||
|
using AdventOfCodeLibrary.Shared;
|
||||||
|
|
||||||
|
public class Day11Part1 : Answerable
|
||||||
|
{
|
||||||
|
public override int Year { get; set; } = 2022;
|
||||||
|
public override int Day { get; set; } = 11;
|
||||||
|
public override int Part { get; set; } = 1;
|
||||||
|
|
||||||
|
public override string GetAnswer(byte[] data)
|
||||||
|
{
|
||||||
|
string[] monkeyString = GetAsString(data).Split(NewLine + NewLine);
|
||||||
|
|
||||||
|
Monkey[] monkeys = new Monkey[monkeyString.Length];
|
||||||
|
|
||||||
|
for (int monkeyIndex = 0; monkeyIndex < monkeys.Length; monkeyIndex++)
|
||||||
|
{
|
||||||
|
monkeys[monkeyIndex] = new(monkeyIndex, 3, monkeyString[monkeyIndex].Split(NewLine));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int monkeyIndex = 0; monkeyIndex < monkeys.Length; monkeyIndex++)
|
||||||
|
{
|
||||||
|
string[] monkey = monkeyString[monkeyIndex].Split(NewLine);
|
||||||
|
int trueMonkey = Convert.ToInt32(monkey[4].Split(' ')[^1]);
|
||||||
|
int falseMonkey = Convert.ToInt32(monkey[5].Split(' ')[^1]);
|
||||||
|
|
||||||
|
monkeys[monkeyIndex].SetThrowTargets(monkeys[trueMonkey], monkeys[falseMonkey]);
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int round = 0; round < 20; round++)
|
||||||
|
{
|
||||||
|
for (int monkey = 0; monkey < monkeys.Length; monkey++)
|
||||||
|
{
|
||||||
|
monkeys[monkey].DoTurn();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int[] mostInspects = monkeys.Select(m => m.TotalInspections).OrderByDescending(i => i).Take(2).ToArray();
|
||||||
|
|
||||||
|
return $"{mostInspects[0] * mostInspects[1]}";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
70
Advent Of Code Library/2022/Day 11/Day11Part2.cs
Normal file
70
Advent Of Code Library/2022/Day 11/Day11Part2.cs
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
namespace AdventOfCodeLibrary._2022
|
||||||
|
{
|
||||||
|
using AdventOfCodeLibrary._2022.Day_11;
|
||||||
|
using AdventOfCodeLibrary.Shared;
|
||||||
|
|
||||||
|
public class Day11Part2 : Answerable
|
||||||
|
{
|
||||||
|
public override int Year { get; set; } = 2022;
|
||||||
|
public override int Day { get; set; } = 11;
|
||||||
|
public override int Part { get; set; } = 2;
|
||||||
|
|
||||||
|
public override string GetAnswer(byte[] data)
|
||||||
|
{
|
||||||
|
string[] monkeyString = GetAsString(data).Split(NewLine + NewLine);
|
||||||
|
int lowestLcm = 1;
|
||||||
|
Monkey[] monkeys = new Monkey[monkeyString.Length];
|
||||||
|
|
||||||
|
for (int monkeyIndex = 0; monkeyIndex < monkeys.Length; monkeyIndex++)
|
||||||
|
{
|
||||||
|
monkeys[monkeyIndex] = new(monkeyIndex, 1, monkeyString[monkeyIndex].Split(NewLine));
|
||||||
|
lowestLcm = CalculateLeastCommonMultiple(lowestLcm, monkeys[monkeyIndex].TestDevision);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int monkeyIndex = 0; monkeyIndex < monkeys.Length; monkeyIndex++)
|
||||||
|
{
|
||||||
|
string[] monkey = monkeyString[monkeyIndex].Split(NewLine);
|
||||||
|
int trueMonkey = Convert.ToInt32(monkey[4].Split(' ')[^1]);
|
||||||
|
int falseMonkey = Convert.ToInt32(monkey[5].Split(' ')[^1]);
|
||||||
|
|
||||||
|
monkeys[monkeyIndex].SetThrowTargets(monkeys[trueMonkey], monkeys[falseMonkey]);
|
||||||
|
|
||||||
|
monkeys[monkeyIndex].lcm = lowestLcm;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int round = 1; round <= 10_000; round++)
|
||||||
|
{
|
||||||
|
for (int monkey = 0; monkey < monkeys.Length; monkey++)
|
||||||
|
{
|
||||||
|
monkeys[monkey].DoTurn(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
long[] mostInspects = monkeys.Select(m => (long)m.TotalInspections).OrderByDescending(i => i).Take(2).ToArray();
|
||||||
|
|
||||||
|
return $"{mostInspects[0] * mostInspects[1]}";
|
||||||
|
}
|
||||||
|
|
||||||
|
private int CalculateLeastCommonMultiple(int valueA, int valueB)
|
||||||
|
{
|
||||||
|
int num1, num2;
|
||||||
|
if (valueA > valueB)
|
||||||
|
{
|
||||||
|
num1 = valueA; num2 = valueB;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
num1 = valueB; num2 = valueA;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 1; i <= num2; i++)
|
||||||
|
{
|
||||||
|
if ((num1 * i) % num2 == 0)
|
||||||
|
{
|
||||||
|
return i * num1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return num2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
138
Advent Of Code Library/2022/Day 11/Monkey.cs
Normal file
138
Advent Of Code Library/2022/Day 11/Monkey.cs
Normal file
@ -0,0 +1,138 @@
|
|||||||
|
namespace AdventOfCodeLibrary._2022.Day_11
|
||||||
|
{
|
||||||
|
internal class Monkey
|
||||||
|
{
|
||||||
|
internal int MonkeyId;
|
||||||
|
|
||||||
|
internal int TotalInspections = 0;
|
||||||
|
|
||||||
|
internal List<long> Items;
|
||||||
|
|
||||||
|
internal int TestDevision;
|
||||||
|
private int WorryDevision;
|
||||||
|
internal int lcm;
|
||||||
|
|
||||||
|
private OperationEnum Operation;
|
||||||
|
|
||||||
|
private long? OperationValue;
|
||||||
|
|
||||||
|
private Monkey ThrowTrueMonkey;
|
||||||
|
private Monkey ThrowFalseMonkey;
|
||||||
|
|
||||||
|
/*
|
||||||
|
Monkey 0:
|
||||||
|
Starting items: 79, 98
|
||||||
|
Operation: new = old * 19
|
||||||
|
Test: divisible by 23
|
||||||
|
If true: throw to monkey 2
|
||||||
|
If false: throw to monkey 3
|
||||||
|
*/
|
||||||
|
internal Monkey (int monkeyId, int worryDivideNumber, string[] monkeyData)
|
||||||
|
{
|
||||||
|
MonkeyId = monkeyId;
|
||||||
|
WorryDevision = worryDivideNumber;
|
||||||
|
|
||||||
|
Items = monkeyData[1].Split(':')[^1].Split(',').Select(s => Convert.ToInt64(s.Trim())).ToList();
|
||||||
|
string[] operation = monkeyData[2].Split(' ');
|
||||||
|
SetOperation(operation[^2][0], operation[^1]);
|
||||||
|
SetTestValue(Convert.ToInt32(monkeyData[3].Split(' ')[^1]));
|
||||||
|
}
|
||||||
|
|
||||||
|
internal void DoTurn(bool calcByWorryDivision = true)
|
||||||
|
{
|
||||||
|
for (int worryIndex = 0; worryIndex < Items.Count; worryIndex++)
|
||||||
|
{
|
||||||
|
long item = Items[worryIndex];
|
||||||
|
item = GetNewWorry(item, calcByWorryDivision);
|
||||||
|
ThrowToMonkey(item);
|
||||||
|
TotalInspections++;
|
||||||
|
}
|
||||||
|
|
||||||
|
Items.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
internal long GetNewWorry(long oldWorry, bool calcByWorryDivision)
|
||||||
|
{
|
||||||
|
long secondValue = OperationValue ?? oldWorry;
|
||||||
|
|
||||||
|
switch (Operation)
|
||||||
|
{
|
||||||
|
case OperationEnum.Add:
|
||||||
|
secondValue = oldWorry + secondValue;
|
||||||
|
break;
|
||||||
|
case OperationEnum.Subtract:
|
||||||
|
secondValue = oldWorry - secondValue;
|
||||||
|
break;
|
||||||
|
case OperationEnum.Divide:
|
||||||
|
secondValue = oldWorry / secondValue;
|
||||||
|
break;
|
||||||
|
case OperationEnum.Multiply:
|
||||||
|
secondValue = oldWorry * secondValue;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (calcByWorryDivision)
|
||||||
|
{
|
||||||
|
return secondValue / WorryDevision;
|
||||||
|
}
|
||||||
|
|
||||||
|
return secondValue % lcm;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
internal void ThrowToMonkey(long worry)
|
||||||
|
{
|
||||||
|
if (worry % TestDevision == 0)
|
||||||
|
{
|
||||||
|
ThrowTrueMonkey.Items.Add(worry);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ThrowFalseMonkey.Items.Add(worry);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
internal void SetOperation(char operation, string value)
|
||||||
|
{
|
||||||
|
switch (operation)
|
||||||
|
{
|
||||||
|
case '+': Operation = OperationEnum.Add;
|
||||||
|
break;
|
||||||
|
case '-':
|
||||||
|
Operation = OperationEnum.Subtract;
|
||||||
|
break;
|
||||||
|
case '/':
|
||||||
|
Operation = OperationEnum.Divide;
|
||||||
|
break;
|
||||||
|
case '*':
|
||||||
|
Operation = OperationEnum.Multiply;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!value.Equals("old"))
|
||||||
|
{
|
||||||
|
OperationValue = Convert.ToInt64(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal void SetTestValue(int value)
|
||||||
|
{
|
||||||
|
TestDevision = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal void SetThrowTargets(Monkey trueMonkey, Monkey falseMonkey)
|
||||||
|
{
|
||||||
|
ThrowTrueMonkey = trueMonkey;
|
||||||
|
ThrowFalseMonkey = falseMonkey;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal enum OperationEnum
|
||||||
|
{
|
||||||
|
Add,
|
||||||
|
Subtract,
|
||||||
|
Multiply,
|
||||||
|
Divide
|
||||||
|
}
|
||||||
|
}
|
||||||
55
Advent Of Code Library/2022/Day 11/day-11-input.txt
Normal file
55
Advent Of Code Library/2022/Day 11/day-11-input.txt
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
Monkey 0:
|
||||||
|
Starting items: 66, 71, 94
|
||||||
|
Operation: new = old * 5
|
||||||
|
Test: divisible by 3
|
||||||
|
If true: throw to monkey 7
|
||||||
|
If false: throw to monkey 4
|
||||||
|
|
||||||
|
Monkey 1:
|
||||||
|
Starting items: 70
|
||||||
|
Operation: new = old + 6
|
||||||
|
Test: divisible by 17
|
||||||
|
If true: throw to monkey 3
|
||||||
|
If false: throw to monkey 0
|
||||||
|
|
||||||
|
Monkey 2:
|
||||||
|
Starting items: 62, 68, 56, 65, 94, 78
|
||||||
|
Operation: new = old + 5
|
||||||
|
Test: divisible by 2
|
||||||
|
If true: throw to monkey 3
|
||||||
|
If false: throw to monkey 1
|
||||||
|
|
||||||
|
Monkey 3:
|
||||||
|
Starting items: 89, 94, 94, 67
|
||||||
|
Operation: new = old + 2
|
||||||
|
Test: divisible by 19
|
||||||
|
If true: throw to monkey 7
|
||||||
|
If false: throw to monkey 0
|
||||||
|
|
||||||
|
Monkey 4:
|
||||||
|
Starting items: 71, 61, 73, 65, 98, 98, 63
|
||||||
|
Operation: new = old * 7
|
||||||
|
Test: divisible by 11
|
||||||
|
If true: throw to monkey 5
|
||||||
|
If false: throw to monkey 6
|
||||||
|
|
||||||
|
Monkey 5:
|
||||||
|
Starting items: 55, 62, 68, 61, 60
|
||||||
|
Operation: new = old + 7
|
||||||
|
Test: divisible by 5
|
||||||
|
If true: throw to monkey 2
|
||||||
|
If false: throw to monkey 1
|
||||||
|
|
||||||
|
Monkey 6:
|
||||||
|
Starting items: 93, 91, 69, 64, 72, 89, 50, 71
|
||||||
|
Operation: new = old + 1
|
||||||
|
Test: divisible by 13
|
||||||
|
If true: throw to monkey 5
|
||||||
|
If false: throw to monkey 2
|
||||||
|
|
||||||
|
Monkey 7:
|
||||||
|
Starting items: 76, 50
|
||||||
|
Operation: new = old * old
|
||||||
|
Test: divisible by 7
|
||||||
|
If true: throw to monkey 4
|
||||||
|
If false: throw to monkey 6
|
||||||
30
Advent Of Code Library/2022/Day 12/Day12Part1.cs
Normal file
30
Advent Of Code Library/2022/Day 12/Day12Part1.cs
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
namespace AdventOfCodeLibrary._2022
|
||||||
|
{
|
||||||
|
using AdventOfCodeLibrary.Shared;
|
||||||
|
|
||||||
|
public class Day12Part1 : Answerable
|
||||||
|
{
|
||||||
|
public override int Year { get; set; } = 2022;
|
||||||
|
public override int Day { get; set; } = 12;
|
||||||
|
public override int Part { get; set; } = 1;
|
||||||
|
|
||||||
|
public override string GetAnswer(byte[] data)
|
||||||
|
{
|
||||||
|
int totalWidth = data.First(b => b == 10 || b == 13);
|
||||||
|
int totalHeigth = data.Count(b => b == 10);
|
||||||
|
|
||||||
|
int[][] map = new int[totalHeigth][];
|
||||||
|
|
||||||
|
for (int heigthIndex = 0; heigthIndex <= totalHeigth; heigthIndex++)
|
||||||
|
{
|
||||||
|
map[heigthIndex] = new int[totalWidth];
|
||||||
|
for (int widthIndex = 0; widthIndex <= totalWidth; widthIndex++)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return string.Empty;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
16
Advent Of Code Library/2022/Day 12/Day12Part2.cs
Normal file
16
Advent Of Code Library/2022/Day 12/Day12Part2.cs
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
namespace AdventOfCodeLibrary._2022
|
||||||
|
{
|
||||||
|
using AdventOfCodeLibrary.Shared;
|
||||||
|
|
||||||
|
public class Day12Part2 : Answerable
|
||||||
|
{
|
||||||
|
public override int Year { get; set; } = 2022;
|
||||||
|
public override int Day { get; set; } = 12;
|
||||||
|
public override int Part { get; set; } = 2;
|
||||||
|
|
||||||
|
public override string GetAnswer(byte[] data)
|
||||||
|
{
|
||||||
|
return string.Empty;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
10
Advent Of Code Library/2022/Day 12/Map.cs
Normal file
10
Advent Of Code Library/2022/Day 12/Map.cs
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
namespace AdventOfCodeLibrary._2022.Day_12
|
||||||
|
{
|
||||||
|
internal class Map
|
||||||
|
{
|
||||||
|
internal void BuildMap(string[] mapData)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
55
Advent Of Code Library/2022/Day 12/day-12-input.txt
Normal file
55
Advent Of Code Library/2022/Day 12/day-12-input.txt
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
Monkey 0:
|
||||||
|
Starting items: 66, 71, 94
|
||||||
|
Operation: new = old * 5
|
||||||
|
Test: divisible by 3
|
||||||
|
If true: throw to monkey 7
|
||||||
|
If false: throw to monkey 4
|
||||||
|
|
||||||
|
Monkey 1:
|
||||||
|
Starting items: 70
|
||||||
|
Operation: new = old + 6
|
||||||
|
Test: divisible by 17
|
||||||
|
If true: throw to monkey 3
|
||||||
|
If false: throw to monkey 0
|
||||||
|
|
||||||
|
Monkey 2:
|
||||||
|
Starting items: 62, 68, 56, 65, 94, 78
|
||||||
|
Operation: new = old + 5
|
||||||
|
Test: divisible by 2
|
||||||
|
If true: throw to monkey 3
|
||||||
|
If false: throw to monkey 1
|
||||||
|
|
||||||
|
Monkey 3:
|
||||||
|
Starting items: 89, 94, 94, 67
|
||||||
|
Operation: new = old + 2
|
||||||
|
Test: divisible by 19
|
||||||
|
If true: throw to monkey 7
|
||||||
|
If false: throw to monkey 0
|
||||||
|
|
||||||
|
Monkey 4:
|
||||||
|
Starting items: 71, 61, 73, 65, 98, 98, 63
|
||||||
|
Operation: new = old * 7
|
||||||
|
Test: divisible by 11
|
||||||
|
If true: throw to monkey 5
|
||||||
|
If false: throw to monkey 6
|
||||||
|
|
||||||
|
Monkey 5:
|
||||||
|
Starting items: 55, 62, 68, 61, 60
|
||||||
|
Operation: new = old + 7
|
||||||
|
Test: divisible by 5
|
||||||
|
If true: throw to monkey 2
|
||||||
|
If false: throw to monkey 1
|
||||||
|
|
||||||
|
Monkey 6:
|
||||||
|
Starting items: 93, 91, 69, 64, 72, 89, 50, 71
|
||||||
|
Operation: new = old + 1
|
||||||
|
Test: divisible by 13
|
||||||
|
If true: throw to monkey 5
|
||||||
|
If false: throw to monkey 2
|
||||||
|
|
||||||
|
Monkey 7:
|
||||||
|
Starting items: 76, 50
|
||||||
|
Operation: new = old * old
|
||||||
|
Test: divisible by 7
|
||||||
|
If true: throw to monkey 4
|
||||||
|
If false: throw to monkey 6
|
||||||
46
Advent Of Code Library/Shared/A-Star/AStarGrid.cs
Normal file
46
Advent Of Code Library/Shared/A-Star/AStarGrid.cs
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
namespace AdventOfCodeLibrary.Shared.A_Star
|
||||||
|
{
|
||||||
|
public class AStarGrid : Grid<AStarNode>
|
||||||
|
{
|
||||||
|
const int TILE_MOVE_COST = 10;
|
||||||
|
|
||||||
|
public AStarGrid(AStarNode[] data) : base(data)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public int MinimalSteps(AStarNode start, AStarNode destination)
|
||||||
|
{
|
||||||
|
List<AStarNode> closedNodes = new();
|
||||||
|
List<AStarNode> openNodes = new List<AStarNode> { start };
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
AStarNode current = openNodes.OrderBy(n => n.FCost).First();
|
||||||
|
openNodes.Remove(current);
|
||||||
|
current.CloseNode();
|
||||||
|
closedNodes.Add(current);
|
||||||
|
|
||||||
|
if (current == destination)
|
||||||
|
{
|
||||||
|
//done
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
var neighbors = GetNeighbors(current, false);
|
||||||
|
neighbors = neighbors.Where(n => current.CanMoveTo(n));
|
||||||
|
|
||||||
|
// calc costs
|
||||||
|
foreach(AStarNode neighbor in neighbors)
|
||||||
|
{
|
||||||
|
neighbor.ParentNode = current;
|
||||||
|
// since we only move straight the move cost is always the same
|
||||||
|
neighbor.GCost = current.GCost + TILE_MOVE_COST;
|
||||||
|
neighbor.HCost = neighbor.DistanceTo(destination) * TILE_MOVE_COST;
|
||||||
|
neighbor.OpenNode();
|
||||||
|
openNodes.Add(neighbor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while (true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
40
Advent Of Code Library/Shared/A-Star/AStarNode.cs
Normal file
40
Advent Of Code Library/Shared/A-Star/AStarNode.cs
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
namespace AdventOfCodeLibrary.Shared.A_Star
|
||||||
|
{
|
||||||
|
public class AStarNode : Node
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Distance from start node
|
||||||
|
/// </summary>
|
||||||
|
public int GCost { get; set; } = 0;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Distance from end node
|
||||||
|
/// </summary>
|
||||||
|
public int HCost { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Total cost (G + F)
|
||||||
|
/// </summary>
|
||||||
|
public int FCost => GCost + HCost;
|
||||||
|
|
||||||
|
public bool? IsClosed { get; private set; } = null;
|
||||||
|
|
||||||
|
public AStarNode? ParentNode { get; set; }
|
||||||
|
|
||||||
|
public AStarNode(Node position) : base(position)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
public AStarNode(int x, int y, char value) : base(x, y, value)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
public void OpenNode() => IsClosed = false;
|
||||||
|
|
||||||
|
public void CloseNode() => IsClosed = true;
|
||||||
|
|
||||||
|
public bool CanMoveTo(AStarNode target)
|
||||||
|
{
|
||||||
|
int diff = target.Integer - Integer; ;
|
||||||
|
return diff == 0 || diff == 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -14,13 +14,13 @@ namespace AdventOfCodeLibrary.Shared
|
|||||||
|
|
||||||
public string DefaultInputFile => $"../../../../Advent Of Code Library/{Year}/Day {Day:00}/day-{Day:00}-input.txt";
|
public string DefaultInputFile => $"../../../../Advent Of Code Library/{Year}/Day {Day:00}/day-{Day:00}-input.txt";
|
||||||
|
|
||||||
protected static string NewLine => "\n";
|
protected static string NewLine => Environment.NewLine;
|
||||||
|
|
||||||
public abstract string GetAnswer(byte[] data);
|
public abstract string GetAnswer(byte[] data);
|
||||||
|
|
||||||
internal static string GetAsString(byte[] bytes) => Encoding.UTF8.GetString(bytes);
|
internal static string GetAsString(byte[] bytes) => Encoding.UTF8.GetString(bytes).ReplaceLineEndings();
|
||||||
|
|
||||||
internal static string[] GetAsStringArray(byte[] bytes) => Encoding.UTF8.GetString(bytes).Split(NewLine).Select(StripControlChars).ToArray();
|
internal static string[] GetAsStringArray(byte[] bytes) => GetAsString(bytes).Split(NewLine).ToArray();
|
||||||
|
|
||||||
private static string StripControlChars(string s)
|
private static string StripControlChars(string s)
|
||||||
{
|
{
|
||||||
|
|||||||
30
Advent Of Code Library/Shared/Grid.cs
Normal file
30
Advent Of Code Library/Shared/Grid.cs
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace AdventOfCodeLibrary.Shared
|
||||||
|
{
|
||||||
|
public class Grid<T> where T : Node
|
||||||
|
{
|
||||||
|
public List<T> DataGrid { get; set; } = new List<T>();
|
||||||
|
|
||||||
|
public Grid(T[] data) => DataGrid.AddRange(data);
|
||||||
|
|
||||||
|
public Grid(IEnumerable<T> data) : this(data.ToArray()) { }
|
||||||
|
|
||||||
|
public IEnumerable<T> GetRow(int rowNumber) => DataGrid.Where(n => n.X == rowNumber);
|
||||||
|
public IEnumerable<T> GetColumn(int columnNumber) => DataGrid.Where(n => n.Y == columnNumber);
|
||||||
|
|
||||||
|
public IEnumerable<T> GetNeighbors(T source, bool allowDiagonals = true)
|
||||||
|
{
|
||||||
|
IEnumerable <T> neighbors = DataGrid.Where(target => Math.Abs(source.X - target.X) <= 1 || Math.Abs(source.Y - target.Y) <= 1);
|
||||||
|
|
||||||
|
if (allowDiagonals)
|
||||||
|
{
|
||||||
|
return neighbors;
|
||||||
|
}
|
||||||
|
|
||||||
|
return neighbors.Where(target => !(Math.Abs(source.X - target.X) <= 1 && Math.Abs(source.Y - target.Y) <= 1));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
25
Advent Of Code Library/Shared/Node.cs
Normal file
25
Advent Of Code Library/Shared/Node.cs
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
namespace AdventOfCodeLibrary.Shared
|
||||||
|
{
|
||||||
|
public class Node
|
||||||
|
{
|
||||||
|
internal int X { get; set; }
|
||||||
|
internal int Y { get; set; }
|
||||||
|
internal char Char { get; }
|
||||||
|
internal short Integer => (short)Char;
|
||||||
|
|
||||||
|
public Node(int x, int y, char character)
|
||||||
|
{
|
||||||
|
X = x;
|
||||||
|
Y = y;
|
||||||
|
Char = character;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Node(Node position) : this(position.X, position.Y, position.Char)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
public int DistanceTo(Node other)
|
||||||
|
{
|
||||||
|
return Math.Abs(X - other.X) + Math.Abs(Y - other.Y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -3,9 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
|
|||||||
# Visual Studio Version 17
|
# Visual Studio Version 17
|
||||||
VisualStudioVersion = 17.4.33103.184
|
VisualStudioVersion = 17.4.33103.184
|
||||||
MinimumVisualStudioVersion = 10.0.40219.1
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Advent Of Code Runner", "Advend Of Code Runner\Advent Of Code Runner.csproj", "{6DCDC513-AF72-4029-932A-A0079BB5422B}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AdventOfCode.Runner", "Advend Of Code Runner\AdventOfCode.Runner.csproj", "{6DCDC513-AF72-4029-932A-A0079BB5422B}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Advent Of Code Library", "Advent Of Code Library\Advent Of Code Library.csproj", "{33CC3924-F18E-4B88-9989-A7A9077B9AC4}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AdventOfCode.Library", "Advent Of Code Library\AdventOfCode.Library.csproj", "{33CC3924-F18E-4B88-9989-A7A9077B9AC4}"
|
||||||
EndProject
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user