mirror of
https://github.com/jcreek/advent-of-code.git
synced 2026-07-13 11:13:47 +00:00
feat(*): Add 2021-15 part 1 files
This commit is contained in:
@@ -0,0 +1,17 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
|
<RootNamespace>_15</RootNamespace>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Update="input.txt">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
namespace Day15
|
||||||
|
{
|
||||||
|
class Program
|
||||||
|
{
|
||||||
|
static void Main(string[] args)
|
||||||
|
{
|
||||||
|
string[] lines = File.ReadAllLines("input.txt");
|
||||||
|
|
||||||
|
|
||||||
|
Part1(lines);
|
||||||
|
//Part2(lines);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void Part1(string[] lines)
|
||||||
|
{
|
||||||
|
Dictionary<Position, int> cavernMap = GenerateCavernMap(lines);
|
||||||
|
|
||||||
|
// Disjktra's algorithm
|
||||||
|
|
||||||
|
Position startPosition = new Position(0, 0);
|
||||||
|
Position endPosition = new Position(cavernMap.Keys.MaxBy(p => p.x).x, cavernMap.Keys.MaxBy(p => p.y).y);
|
||||||
|
|
||||||
|
PriorityQueue<Position, int> priorityQueue = new PriorityQueue<Position, int>();
|
||||||
|
Dictionary<Position, int> totalRiskMap = new Dictionary<Position, int>();
|
||||||
|
|
||||||
|
totalRiskMap[startPosition] = 0;
|
||||||
|
priorityQueue.Enqueue(startPosition, 0);
|
||||||
|
|
||||||
|
while (priorityQueue.Count > 0)
|
||||||
|
{
|
||||||
|
Position position = priorityQueue.Dequeue();
|
||||||
|
|
||||||
|
IEnumerable<Position> nearbyPositions = GetNearbyNonDiagonalPositions(position);
|
||||||
|
|
||||||
|
foreach (Position nearbyPosition in nearbyPositions)
|
||||||
|
{
|
||||||
|
// If it's in the cavern but we haven't calculated its risk yet
|
||||||
|
if (cavernMap.ContainsKey(nearbyPosition) && !totalRiskMap.ContainsKey(nearbyPosition))
|
||||||
|
{
|
||||||
|
// The total risk for this nearby position is the total risk for the current position,
|
||||||
|
// plus the value at this nearby position
|
||||||
|
int totalRisk = totalRiskMap[position] + cavernMap[nearbyPosition];
|
||||||
|
totalRiskMap[nearbyPosition] = totalRisk;
|
||||||
|
|
||||||
|
// If we've reached the end position of the cavern, then stop
|
||||||
|
if (nearbyPosition == endPosition)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add the position to the queue
|
||||||
|
priorityQueue.Enqueue(nearbyPosition, totalRisk);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// What is the lowest total risk of any path from the top left to the bottom right?
|
||||||
|
Console.WriteLine(totalRiskMap[endPosition]);
|
||||||
|
}
|
||||||
|
|
||||||
|
static IEnumerable<Position> GetNearbyNonDiagonalPositions(Position position)
|
||||||
|
{
|
||||||
|
return new[] {
|
||||||
|
position with {y = position.y + 1},
|
||||||
|
position with {y = position.y - 1},
|
||||||
|
position with {x = position.x + 1},
|
||||||
|
position with {x = position.x - 1},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
static Dictionary<Position, int> GenerateCavernMap(string[] lines)
|
||||||
|
{
|
||||||
|
Dictionary<Position, int> keyValuePairs = new Dictionary<Position, int>();
|
||||||
|
for (int x = 0; x < lines.Length; x++)
|
||||||
|
{
|
||||||
|
for (int y = 0; y < lines[x].Length; y++)
|
||||||
|
{
|
||||||
|
// TODO - why is this "lines[y][x] - '0'" rather than "lines[x][y]"
|
||||||
|
// as I think it should be?
|
||||||
|
keyValuePairs.Add(new Position(x, y), lines[y][x] - '0');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return keyValuePairs;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
record Position(int x, int y);
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
--- Day 15: Chiton ---
|
||||||
|
You've almost reached the exit of the cave, but the walls are getting closer together. Your submarine can barely still fit, though; the main problem is that the walls of the cave are covered in chitons, and it would be best not to bump any of them.
|
||||||
|
|
||||||
|
The cavern is large, but has a very low ceiling, restricting your motion to two dimensions. The shape of the cavern resembles a square; a quick scan of chiton density produces a map of risk level throughout the cave (your puzzle input). For example:
|
||||||
|
|
||||||
|
1163751742
|
||||||
|
1381373672
|
||||||
|
2136511328
|
||||||
|
3694931569
|
||||||
|
7463417111
|
||||||
|
1319128137
|
||||||
|
1359912421
|
||||||
|
3125421639
|
||||||
|
1293138521
|
||||||
|
2311944581
|
||||||
|
You start in the top left position, your destination is the bottom right position, and you cannot move diagonally. The number at each position is its risk level; to determine the total risk of an entire path, add up the risk levels of each position you enter (that is, don't count the risk level of your starting position unless you enter it; leaving it adds no risk to your total).
|
||||||
|
|
||||||
|
Your goal is to find a path with the lowest total risk. In this example, a path with the lowest total risk is highlighted here:
|
||||||
|
|
||||||
|
1163751742
|
||||||
|
1381373672
|
||||||
|
2136511328
|
||||||
|
3694931569
|
||||||
|
7463417111
|
||||||
|
1319128137
|
||||||
|
1359912421
|
||||||
|
3125421639
|
||||||
|
1293138521
|
||||||
|
2311944581
|
||||||
|
The total risk of this path is 40 (the starting position is never entered, so its risk is not counted).
|
||||||
|
|
||||||
|
What is the lowest total risk of any path from the top left to the bottom right?
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
1163751742
|
||||||
|
1381373672
|
||||||
|
2136511328
|
||||||
|
3694931569
|
||||||
|
7463417111
|
||||||
|
1319128137
|
||||||
|
1359912421
|
||||||
|
3125421639
|
||||||
|
1293138521
|
||||||
|
2311944581
|
||||||
Reference in New Issue
Block a user