Untitled

mail@pastecode.io avatar
unknown
csharp
2 years ago
4.0 kB
9
Indexable
Never
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 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>() };
            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.Parent != null)
                    {
                        addToParent(currentNode.Parent, int.Parse(line.Split(' ')[0]));
                    }
                    currentNode.Size += int.Parse(line.Split(' ')[0]);
                }
            }
            var part1Sizes = GetSizes(new List<Directory> { root }, new List<int>());
            var part1 = part1Sizes.Sum();
            Console.WriteLine(part1);

            var spaceLeft = 70000000 - root.Size;
            var spaceNeeded = 30000000 - spaceLeft;
            var part2Sizes = GetAllSizes(new List<Directory> { root }, new List<int>());
            var part2 = part2Sizes.Where(x => x >= spaceNeeded).Min();
            Console.WriteLine(part2);
            Console.ReadLine();
        }
        public static void addToParent(Directory parent, int toAdd)
        {
            parent.Size += toAdd;
            if (parent.Parent != null)
            {
                addToParent(parent.Parent, toAdd);
            }
        }

        public static List<int> GetSizes(List<Directory> directories, List<int> values)
        {
            foreach (var directory in directories)
            {
                if (directory.Size < 100000)
                {
                    values.Add(directory.Size);

                    if (directory.Directories.Any())
                    {
                        GetSizes(directory.Directories, values);
                    }
                }
                else
                {
                    GetSizes(directory.Directories, values);
                }
            }
            return values;
        }
        public static List<int> GetAllSizes(List<Directory> directories, List<int> values)
        {
            foreach (var directory in directories)
            {
                    values.Add(directory.Size);
                    if (directory.Directories.Any())
                    {
                        GetAllSizes(directory.Directories, values);
                    }
            }
            return values;
        }
    }
}