AdventOfCode/Year2022/Day7.cs

103 lines
2.7 KiB
C#
Raw Permalink Normal View History

2023-06-07 21:56:41 +00:00
namespace AdventOfCode.Year2022;
public class Day7
{
2023-06-07 23:12:01 +00:00
private static readonly Dictionary<string, List<string>> Tree = GetTree();
2023-06-24 21:26:39 +00:00
2023-06-07 21:56:41 +00:00
public Day7()
{
2023-06-24 21:26:39 +00:00
Console.WriteLine($@"
Day7 Solution
Part1 Result: {Part1()}
Part2 Result: {Part2()}
=============================");
2023-06-07 21:56:41 +00:00
}
private static int Part1()
{
2023-06-24 21:26:39 +00:00
var sum = 0;
foreach (var path in Tree.Keys)
2023-06-07 21:56:41 +00:00
{
2023-06-24 21:26:39 +00:00
var size = CalculateDirSize(path);
2023-06-07 21:56:41 +00:00
if (size <= 100000)
sum += size;
}
return sum;
}
2023-06-24 21:26:39 +00:00
2023-06-07 21:56:41 +00:00
private static int Part2()
{
2023-06-24 21:26:39 +00:00
var neededSpace = CalculateDirSize("/") - 40000000;
if (neededSpace <= 0) return 0;
2023-06-07 21:56:41 +00:00
2023-06-24 21:26:39 +00:00
var bigEnoughDir = new List<int>();
foreach (var path in Tree.Keys)
2023-06-07 21:56:41 +00:00
{
2023-06-24 21:26:39 +00:00
var size = CalculateDirSize(path);
2023-06-07 21:56:41 +00:00
if (size > neededSpace)
bigEnoughDir.Add(size);
}
bigEnoughDir.Sort();
return bigEnoughDir[0];
}
private static int CalculateDirSize(string path)
{
2023-06-24 21:26:39 +00:00
var size = 0;
var dirContent = Tree[path];
foreach (var content in dirContent)
2023-06-07 21:56:41 +00:00
{
2023-06-24 21:26:39 +00:00
var properties = content.Split(" ");
2023-06-07 21:56:41 +00:00
if (properties[0] == "dir")
size += CalculateDirSize(path + properties[1] + "/");
else
size += Convert.ToInt32(properties[0]);
}
return size;
}
2023-06-07 23:12:01 +00:00
private static Dictionary<string, List<string>> GetTree()
2023-06-07 21:56:41 +00:00
{
2023-06-24 21:26:39 +00:00
var tree = new Dictionary<string, List<string>>();
var currentPath = "";
2023-06-07 21:56:41 +00:00
2023-06-24 21:26:39 +00:00
var input = File.ReadLines("inputs/day7.txt");
foreach (var line in input)
2023-06-07 21:56:41 +00:00
if (line.StartsWith("$"))
{
2023-06-24 21:26:39 +00:00
var cmdLine = line.Substring(2).Split(" ");
2023-06-07 21:56:41 +00:00
if (cmdLine[0] == "cd")
{
2023-06-24 21:26:39 +00:00
var dir = cmdLine[1];
2023-06-07 21:56:41 +00:00
if (dir == "/")
2023-06-24 21:26:39 +00:00
{
2023-06-07 21:56:41 +00:00
currentPath = "/";
2023-06-24 21:26:39 +00:00
}
2023-06-07 21:56:41 +00:00
else if (dir == "..")
{
if (currentPath == "/") continue;
2023-06-24 21:26:39 +00:00
var slashIndex = currentPath
2023-06-07 21:56:41 +00:00
.Substring(0, currentPath.Length - 1)
.LastIndexOf('/');
currentPath = currentPath.Substring(0, slashIndex + 1);
}
else
2023-06-24 21:26:39 +00:00
{
2023-06-07 21:56:41 +00:00
currentPath += dir + "/";
2023-06-24 21:26:39 +00:00
}
2023-06-07 21:56:41 +00:00
}
}
else
{
if (!tree.ContainsKey(currentPath))
tree.Add(currentPath, new List<string>());
tree[currentPath].Add(line);
}
2023-06-07 23:12:01 +00:00
return tree;
2023-06-07 21:56:41 +00:00
}
}