Untitled

 avatar
unknown
csharp
3 years ago
4.1 kB
6
Indexable
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;

namespace Day7
{
    public class Directory
    {
        public string Name { get; set; }
        public Directory Parent { get; set; }
        public List<Directory> Directories { get; set; }
        public List<FileItem> Items { get; set; }
    }
    public class FileItem
    {
        public Directory Parent { get; set; }
        public int Size { get; set; }
    }

    internal class Program
    {
        public static string[] Input => File.ReadAllLines(@"input.txt").ToArray();

        static void Main(string[] args)
        {
            var root = new Directory() { Name = "root", Directories = new List<Directory>(), Items = new List<FileItem>() };
            var currentNode = root;

            foreach (var line in Input)
            {
                if (line == "$ cd /")
                {
                    currentNode = root;
                }
                else if (line == "$ cd ..")
                {
                    currentNode = currentNode.Parent;
                }
                //change dir
                else if (Regex.Match(line, "\\$ cd\\s(\\w+)").Success)
                {
                    currentNode = currentNode.Directories.FirstOrDefault(x => x.Name == line.Split(' ')[2]);
                }
                //new directory
                else if (Regex.Match(line, "dir\\s(\\w+)").Success)
                {
                    if (currentNode.Directories == null)
                    {
                        currentNode.Directories = new List<Directory>();
                    }
                    currentNode.Directories.Add(new Directory() { Name = line.Split(' ')[1], Parent = currentNode, Directories = new List<Directory>() });
                }
                //new file
                else if (Regex.Match(line, "(\\d+)\\s([\\w.]+)").Success)
                {

                    if (currentNode.Items == null)
                    {
                        currentNode.Items = new List<FileItem>();
                    }
                    currentNode.Items.Add(new FileItem() { Parent = currentNode.Parent, Size = int.Parse(line.Split(' ')[0]) });
                }
            }

            var test = GetSizes(new List<Directory>() { root }, new List<int>(), 0);

            Console.ReadLine();

        }
        public static List<int> GetSizes(List<Directory> nodes, List<int> values, int ToAdd)
        {
            foreach (var node in nodes)
            {
                if (node.Items != null)
                {
                    var sum = node.Items.Select(x => x.Size).Sum();
                    if (sum > 100000 && node.Directories.Any())
                    {
                        GetSizes(node.Directories, values, ToAdd);
                    }
                    if (sum < 100000 && node.Directories.Any())
                    {
                        var childItems = node.Directories.Where(x => x.Items != null).SelectMany(x => x.Items.Select(s => s.Size)).Sum();
                        var childhasDirectories = node.Directories.Select(x => x.Directories.Any()).Any();
                        if (sum + childItems < 100000 && !childhasDirectories)
                        {
                            values.Add(sum + childItems);
                            values.Add(childItems);
                        }
                        else if (sum + childItems < 100000 && childhasDirectories)
                        {
                            GetSizes(node.Directories, values, sum + childItems);
                        }

                    }
                    else if (sum < 100000 && !node.Directories.Any())
                    {
                        values.Add(sum + ToAdd);
                    }
                }
            }
            return values;            
        }
    }
}
Editor is loading...