Untitled

 avatar
unknown
plain_text
3 years ago
3.8 kB
10
Indexable
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace aoc2022
{
    internal class Day6
    {
        public static void First()
        {
            var lines = File.ReadAllLines("input.txt");
            var root = new DirectoryItem();
            var currentNode = root;
            foreach (var line in lines)
            {
                var fileMatch = Regex.Match(line, "(\\d+)\\s([\\w.]+)");
                var directoryMatch = Regex.Match(line, "dir\\s(\\w+)");
                var changeDirectoryMatch = Regex.Match(line, "\\$ cd\\s(\\w+)");

                if (line == "$ cd /")
                    currentNode = root;
                else if (line == "$ cd ..")
                    currentNode = currentNode.Parent;
                else if (fileMatch.Success)
                {
                    EnsureHaveFile(currentNode, new FileItem() { Name = fileMatch.Groups[2].Value, Size = int.Parse(fileMatch.Groups[1].Value) });
                }
                else if (directoryMatch.Success)
                {
                    EnsureHaveDirectory(currentNode, new DirectoryItem() { Name = directoryMatch.Groups[1].Value });
                }
                else if (changeDirectoryMatch.Success)
                {
                    EnsureHaveDirectory(currentNode, new DirectoryItem() { Name = changeDirectoryMatch.Groups[1].Value });
                    currentNode = currentNode.Items.OfType<DirectoryItem>().First(x => x.Name == changeDirectoryMatch.Groups[1].Value);
                }
            }
            var part1 = root.Sizes(0).Where(x => x.Size<= 100000).Sum(x => x.Size);
            var rootSize = root.Sizes(0).Where(x => x.Level == 0).Sum(x => x.Size);
            var part2 = root.Sizes(0).Where(x => x.Size >= rootSize + 30000000 - 70000000).OrderBy(x => x.Size).First().Size; ;
        }
        private static void EnsureHaveFile(DirectoryItem location, FileItem file)
        {
            if (!location.Items.OfType<FileItem>().Any(x => x.Name == file.Name && x.Size == file.Size))
            {
                file.Parent = location;
                location.Items.Add(file);
            }
        }
        private static void EnsureHaveDirectory(DirectoryItem location, DirectoryItem directory)
        {
            if (!location.Items.OfType<DirectoryItem>().Any(x => x.Name == directory.Name))
            {
                directory.Parent = location;
                location.Items.Add(directory);
            }
        }
    }

   


    public class Item
    {
        public string Name { get; set; }
        public DirectoryItem Parent { get; set; }
        public override string ToString()
        {
            return this.GetType().Name + ": " + Name;
        }
    }

    public class FileItem : Item
    {
        public int Size { get; set; }
    }
    public class DirectoryItem : Item
    {
        public List<Item> Items { get; set; } = new List<Item>();
        public IEnumerable<(int Size, int Level)> Sizes(int level)
        {
            var thisDirectoryFileSize = Items.OfType<FileItem>().Sum(x => x.Size);
            var subdirectorySizes = Items.OfType<DirectoryItem>().SelectMany(x => x.Sizes(level + 1));
            foreach (var subdirectorySize in subdirectorySizes)
            {
                yield return subdirectorySize;
            }
            yield return (thisDirectoryFileSize + subdirectorySizes.Where(x => x.Level == level + 1).Sum(x => x.Size), level);
        }

    }
}
Editor is loading...