Compare commits

...

10 Commits

Author SHA1 Message Date
Rob
2ad0676c49 started on astar code 2023-11-27 21:50:58 +01:00
Rob Stoffelen
0e5a63d58a Completed Day 11 p 2 2022-12-16 10:52:57 +01:00
Rob
ee6da383ca worked on day 11 p 2 2022-12-15 22:56:10 +01:00
Rob Stoffelen
389a85869b Added day 11, created monkey parser 2022-12-15 17:39:15 +01:00
Rob Stoffelen
43c01b9db7 added day 11 2022-12-15 10:26:47 +01:00
Rob Stoffelen
de2ce7e9b0 Completed Day 9 p 2 2022-12-15 09:34:57 +01:00
Rob Stoffelen
08dbff1008 Updated Day 9 rope walker, implemented translation via Math.Sign using the delta between 2 points 2022-12-15 09:19:19 +01:00
Rob Stoffelen
cf2a2e1f39 Added 10 10, still got to complete day 9 p 2 2022-12-15 09:07:39 +01:00
Rob
691a3e351c Completed day 8 2022-12-11 15:25:13 +01:00
Rob Stoffelen
974083b721 Added day 9 2022-12-09 14:28:25 +01:00
27 changed files with 3001 additions and 141 deletions

View File

@ -9,7 +9,7 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Advent Of Code Library\Advent Of Code Library.csproj" />
<ProjectReference Include="..\Advent Of Code Library\AdventOfCode.Library.csproj" />
</ItemGroup>
</Project>

View File

@ -3,15 +3,15 @@
using AdventOfCodeLibrary._2022;
using AdventOfCodeLibrary.Shared;
string _demoData = @"30373
25512
65332
33549
35390";
string _demoData = @"Sabqponm
abcryxxl
accszExk
acctuvwj
abdefghi";
Answerable answerable = new Day08Part1();
Answerable answerable = new Day12Part1();
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);

View File

@ -1,6 +1,5 @@
namespace AdventOfCodeLibrary._2022
{
using AdventOfCodeLibrary._2022.Day_08;
using AdventOfCodeLibrary.Shared;
public class Day08Part1 : Answerable
@ -12,9 +11,23 @@
public override string GetAnswer(byte[] data)
{
string[] treeGrid = GetAsStringArray(data);
TreeChecker checker = new TreeChecker();
return checker.VisibleTrees(treeGrid).ToString();
int visibleTreeCount = 0;
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();
}
}
}

View File

@ -10,14 +10,47 @@
public override string GetAnswer(byte[] data)
{
int markerLength = 14;
for (int skip = 0; skip < data.Length - 1 - markerLength; skip++)
string[] treeGrid = GetAsStringArray(data);
int highestScenicScore = 0;
for (int row = 0; row < treeGrid.Length; row++)
{
if (data.Skip(skip).Take(markerLength).Distinct().Count() == markerLength)
return (skip + markerLength).ToString();
for (int col = 0; col < treeGrid[row].Length; col++)
{
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();
}
}
}

View File

@ -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;
}
}
}

View 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();
}
}
}

View 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();
}
}
}

View 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);
}
}
}

File diff suppressed because it is too large Load Diff

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

View 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]}";
}
}
}

View 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;
}
}
}

View 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
}
}

View 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

View 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;
}
}
}

View 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;
}
}
}

View File

@ -0,0 +1,10 @@
namespace AdventOfCodeLibrary._2022.Day_12
{
internal class Map
{
internal void BuildMap(string[] mapData)
{
}
}
}

View 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

View 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);
}
}
}

View 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;
}
}
}

View File

@ -14,13 +14,13 @@ namespace AdventOfCodeLibrary.Shared
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);
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)
{

View 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));
}
}
}

View 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);
}
}
}

View File

@ -3,9 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.4.33103.184
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
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
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution