AdventOfCode2022/Kalender/day7.cs

118 lines
3.4 KiB
C#
Raw Permalink Normal View History

2022-12-07 14:40:02 +01:00
using Microsoft.SqlServer.Server;
using System;
using System.Collections.Generic;
using System.ComponentModel.Design.Serialization;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Kalender
{
internal class day7
{
2022-12-12 12:27:41 +01:00
static string file = "../../../files/day7.txt";
2022-12-07 14:40:02 +01:00
static string[] lines = File.ReadAllLines(file);
static public void day7_part1()
{
Dictionary<string, Folder> folders = new Dictionary<string, Folder>();
Folder root = new Folder("/", new List<string>(), 0);
folders["/"] = root;
string currentPath = "/";
Folder currentFolder = root;
foreach (string line in lines)
{
if (line.StartsWith("$ cd ") && !line.StartsWith("$ cd .."))
{
var newFolder = line.Substring(5);
if (newFolder == "/")
{
currentPath = "/";
}
else
{
currentPath = newFolder;
2022-12-07 20:41:55 +01:00
if (folders.ContainsKey(newFolder))
{
currentPath += "_";
folders[currentPath] = new Folder(currentPath, new List<string>(), 0);
}
2022-12-07 14:40:02 +01:00
folders[currentPath] = new Folder(currentPath, new List<string>(), 0);
}
}
else if (line.StartsWith("dir "))
{
2022-12-07 20:41:55 +01:00
if (folders[currentPath].subFolders.Contains(line.Substring(4))){
currentPath += "_";
folders[currentPath].subFolders.Add(line.Substring(4));
}
2022-12-07 14:40:02 +01:00
folders[currentPath].subFolders.Add(line.Substring(4));
}
2022-12-07 20:41:55 +01:00
else if (line.StartsWith("$ ls "))
2022-12-07 14:40:02 +01:00
{
continue;
}
else
{
if (!line.Contains('$'))
{
folders[currentPath].fileSize += Convert.ToInt32(line.Split(' ')[0]);
}
2022-12-07 20:41:55 +01:00
2022-12-07 14:40:02 +01:00
}
}
2022-12-07 20:41:55 +01:00
Console.WriteLine(result(folders));
}
2022-12-07 14:40:02 +01:00
2022-12-07 20:41:55 +01:00
public static int result(Dictionary<string, Folder> folders)
{
int result = 0;
foreach (KeyValuePair<string, Folder> pair in folders)
2022-12-07 14:40:02 +01:00
{
2022-12-07 20:41:55 +01:00
foreach (string subfolder in pair.Value.subFolders)
2022-12-07 14:40:02 +01:00
{
pair.Value.fileSize += folders[subfolder].fileSize;
}
2022-12-07 20:41:55 +01:00
}
foreach (KeyValuePair<string, Folder> pair in folders)
{
2022-12-07 14:40:02 +01:00
if(pair.Value.fileSize <= 100000)
{
2022-12-07 20:41:55 +01:00
result += pair.Value.fileSize;
2022-12-07 14:40:02 +01:00
}
}
2022-12-07 20:41:55 +01:00
return result;
2022-12-07 14:40:02 +01:00
}
2022-12-07 20:41:55 +01:00
}
2022-12-07 14:40:02 +01:00
2022-12-07 20:41:55 +01:00
//1749646
//41412830
2022-12-07 14:40:02 +01:00
2022-12-07 20:41:55 +01:00
class Folder
2022-12-07 14:40:02 +01:00
{
public string name;
public List<string> subFolders;
public int fileSize;
public Folder(string name, List<string> subFolders, int fileSize)
{
this.name = name;
this.subFolders = subFolders;
this.fileSize = fileSize;
}
}
}