Untitled

mail@pastecode.io avatar
unknown
csharp
2 years ago
5.6 kB
2
Indexable
Never
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Day42022
{

    public class Folder
    {
        public string Name { get; set; }
        public List<Folder> Folders { get; set; }
        public List<Item> Items { get; set; }
    }
    public class Item
    {
        public int Size { get; set; }
    }

    internal class Program
    {
        public static string[] Input => File.ReadAllLines(@"input.txt").ToArray();
        public static List<Folder> allFolders => new List<Folder>();
        static void Main(string[] args)
        {
            var allFolders = new List<Folder>();

            int lastItem = Input.Skip(2).ToList().FindIndex(x => x.StartsWith("$ cd")) + 2;
            var children = Input[2..lastItem];

            var bannedList = new List<string>();
            
            var RootFolder = new Folder()
            {
                Name = "root",
                Folders = GetFoldersRecursive(children, new List<Folder>(), new List<string>()),
                Items = GetFiles(children)
            };
            allFolders.Add(RootFolder);

            var value = CheckFoldersRecursive(allFolders, new List<int>(), 0);

            var summerized = value.Sum();

            Console.WriteLine("Klar");
            Console.ReadLine();
        }
        public static List<int> CheckFoldersRecursive(List<Folder> folders, List<int> values, int combo = 0)
        {
            foreach (var folder in folders)
            {
                if (combo > 0)
                {
                    if (folder.Items.Select(x => x.Size).Sum() + combo < 100000 && folder.Folders.Any())
                    {
                        var combinedLevel = folder.Items.Select(x => x.Size).Sum();
                        CheckFoldersRecursive(folder.Folders, values, combinedLevel);
                    }
                    else if (folder.Items.Select(x => x.Size).Sum() *2 + combo < 100000)
                    {
                        values.Add(folder.Items.Select(x => x.Size).Sum() *2 + combo);
                    }
                }
                else
                {
                    if (folder.Items.Any())
                    {
                        if (folder.Items.Select(x => x.Size).Sum() < 100000 && folder.Folders.Any())
                        {
                            var combinedLevel = folder.Items.Select(x => x.Size).Sum();
                            CheckFoldersRecursive(folder.Folders, values, combinedLevel);
                        }
                        else if (folder.Items.Select(x => x.Size).Sum() < 100000)
                        {
                            values.Add(folder.Items.Select(x => x.Size).Sum());
                        }
                    }
                    if (folder.Folders.Any())
                    {
                        CheckFoldersRecursive(folder.Folders, values);
                    }
                }
            }
            return values;
        }
        public static List<Folder> GetFoldersRecursive(string[] children, List<Folder> folders, List<string> banned)
        {
            folders = new List<Folder>();
            foreach (var child in children)
            {
                
                if (child.StartsWith("dir"))
                {
                    var actualFirstItem = Input.ToList().Where(x => x == $"$ cd {child.Split(' ')[1]}");
                    int firstItem = Input.ToList().FindIndex(x => x == $"$ cd {child.Split(' ')[1]}") + 2;

                    var childrenToTake = Input.Skip(firstItem).ToList().FindIndex(x => x.StartsWith("$ cd")) != -1 ?
                        Input.Skip(firstItem).ToList().FindIndex(x => x.StartsWith("$ cd")) : Input.Length;
                    
                    var allChildren = Input.Skip(firstItem).Take(childrenToTake).ToArray();

                    var newAllchildren = allChildren.Where(a => !banned.Contains(a)).ToArray(); ;

                    banned.AddRange(allChildren);


                    if (newAllchildren.Any(x => x.StartsWith("dir ")))
                    {
                        var folderToAdd = new Folder()
                        {
                            Name = child.Split(' ')[1],
                            Items = GetFiles(allChildren),
                            Folders = GetFoldersRecursive(newAllchildren, folders, banned)
                        };
                        folders.Add(folderToAdd);
                    }
                    else
                    {
                        var folderToAdd = new Folder()
                        {
                            Name = child.Split(' ')[1],
                            Items = GetFiles(newAllchildren),
                            Folders = new List<Folder>()
                        };
                        folders.Add(folderToAdd);
                    }
                }
            }
            return folders;

        }
        
        public static List<Item> GetFiles(string[] children)
        {
            var files = new List<Item>();
            foreach (var child in children)
            {
                var i = 0;
                if (int.TryParse(child.Split(' ')[0], out i))
                {
                    files.Add(new Item() { Size = int.Parse(child.Split(' ')[0]) });
                }
            }
            return files;
        }
    }
}