Day 12 solution
This was my initial solution I used to submit the puzzle answer. Runtime for part 2 on my input set was around a second, so now I'm going to find optimization targets. I'm sure there's a lot I could be doing much better.
This commit is contained in:
@ -51,6 +51,9 @@
|
|||||||
<None Update="inputs\11.txt">
|
<None Update="inputs\11.txt">
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
</None>
|
</None>
|
||||||
|
<None Update="inputs\12.txt">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
24
inputs/12.txt
Normal file
24
inputs/12.txt
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
rf-RL
|
||||||
|
rf-wz
|
||||||
|
wz-RL
|
||||||
|
AV-mh
|
||||||
|
end-wz
|
||||||
|
end-dm
|
||||||
|
wz-gy
|
||||||
|
wz-dm
|
||||||
|
cg-AV
|
||||||
|
rf-AV
|
||||||
|
rf-gy
|
||||||
|
end-mh
|
||||||
|
cg-gy
|
||||||
|
cg-RL
|
||||||
|
gy-RL
|
||||||
|
VI-gy
|
||||||
|
AV-gy
|
||||||
|
dm-rf
|
||||||
|
start-cg
|
||||||
|
start-RL
|
||||||
|
rf-mh
|
||||||
|
AV-start
|
||||||
|
qk-mh
|
||||||
|
wz-mh
|
93
src/12.cs
Normal file
93
src/12.cs
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
namespace aoc2021;
|
||||||
|
|
||||||
|
internal class Day12
|
||||||
|
{
|
||||||
|
internal static void Go()
|
||||||
|
{
|
||||||
|
Logger.Log("Day 12");
|
||||||
|
Logger.Log("-----");
|
||||||
|
var lines = File.ReadAllLines("inputs/12.txt");
|
||||||
|
var paths = new Dictionary<string, List<string>>();
|
||||||
|
foreach (var line in lines)
|
||||||
|
{
|
||||||
|
var points = line.Split('-');
|
||||||
|
if (!paths.ContainsKey(points[0]))
|
||||||
|
{
|
||||||
|
paths[points[0]] = new List<string>();
|
||||||
|
}
|
||||||
|
if (!paths.ContainsKey(points[1]))
|
||||||
|
{
|
||||||
|
paths[points[1]] = new List<string>();
|
||||||
|
}
|
||||||
|
|
||||||
|
paths[points[0]].Add(points[1]);
|
||||||
|
paths[points[1]].Add(points[0]);
|
||||||
|
}
|
||||||
|
Part1(paths);
|
||||||
|
Part2(paths);
|
||||||
|
Logger.Log("");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void Part1(Dictionary<string, List<string>> paths)
|
||||||
|
{
|
||||||
|
using var t = new Timer();
|
||||||
|
|
||||||
|
var validPaths = new List<List<string>>();
|
||||||
|
FindPaths(paths, validPaths, new List<string>(){ "start" }, false);
|
||||||
|
|
||||||
|
Logger.Log($"part1: {validPaths.Count}");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void FindPaths(Dictionary<string, List<string>> paths, List<List<string>> routes, List<string> currRoute, bool canVisitSmallCaveTwice)
|
||||||
|
{
|
||||||
|
//Logger.Log($"Current path: {string.Join(',', currRoute)}");
|
||||||
|
var curr = currRoute.Last();
|
||||||
|
foreach (var next in paths[curr])
|
||||||
|
{
|
||||||
|
//Logger.Log($" Evaluating next: {next}");
|
||||||
|
if (next == "start")
|
||||||
|
{
|
||||||
|
//Logger.Log($" is start, skipping");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (next != "end" && next.ToLower() == next && currRoute.Contains(next))
|
||||||
|
{
|
||||||
|
if (!canVisitSmallCaveTwice)
|
||||||
|
{
|
||||||
|
//Logger.Log($" is already-visited small cave, skipping");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else if (currRoute.Any(x => x.ToLower() == x && currRoute.Count(y => x == y) == 2))
|
||||||
|
{
|
||||||
|
//Logger.Log($" is already-visited small cave and we have already been to another one twice, skipping");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//Logger.Log($" adding to route");
|
||||||
|
currRoute.Add(next);
|
||||||
|
if (next == "end")
|
||||||
|
{
|
||||||
|
//Logger.Log($" is end, so adding {string.Join(',', currRoute)} to valid paths");
|
||||||
|
routes.Add(currRoute);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
FindPaths(paths, routes, new List<string>(currRoute), canVisitSmallCaveTwice);
|
||||||
|
}
|
||||||
|
|
||||||
|
currRoute.RemoveAt(currRoute.Count - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Logger.Log(" done with route");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void Part2(Dictionary<string, List<string>> paths)
|
||||||
|
{
|
||||||
|
using var t = new Timer();
|
||||||
|
|
||||||
|
var validPaths = new List<List<string>>();
|
||||||
|
FindPaths(paths, validPaths, new List<string>() { "start" }, true);
|
||||||
|
|
||||||
|
Logger.Log($"part2: {validPaths.Count}");
|
||||||
|
}
|
||||||
|
}
|
@ -37,7 +37,11 @@ switch (arg)
|
|||||||
aoc2021.Day10.Go();
|
aoc2021.Day10.Go();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
case "11":
|
||||||
aoc2021.Day11.Go();
|
aoc2021.Day11.Go();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
aoc2021.Day12.Go();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user