diff --git a/Advend Of Code Runner/Program.cs b/Advend Of Code Runner/Program.cs index 258cdd0..1114f0a 100644 --- a/Advend Of Code Runner/Program.cs +++ b/Advend Of Code Runner/Program.cs @@ -1,14 +1,33 @@ -using AdventOfCodeLibrary._2022; +using System.Text; + +using AdventOfCodeLibrary._2022; using AdventOfCodeLibrary.Shared; -string _demoData = @"2-4,6-8 -2-3,4-5 -5-7,7-9 -2-8,3-7 -6-6,4-6 -2-6,4-8"; +string _demoData = @"$ cd / +$ ls +dir a +14848514 b.txt +8504156 c.dat +dir d +$ cd a +$ ls +dir e +29116 f +2557 g +62596 h.lst +$ cd e +$ ls +584 i +$ cd .. +$ cd .. +$ cd d +$ ls +4060174 j +8033020 d.log +5626152 d.ext +7214296 k"; -Answerable answerable = new Day07Part1(); +Answerable answerable = new Day07Part2(); byte[] dataArray = File.ReadAllBytes(answerable.DefaultInputFile); //dataArray = Encoding.UTF8.GetBytes(_demoData); diff --git a/Advent Of Code Library/2022/Day 07/Day07Part2.cs b/Advent Of Code Library/2022/Day 07/Day07Part2.cs index 626da3d..cc8ae1c 100644 --- a/Advent Of Code Library/2022/Day 07/Day07Part2.cs +++ b/Advent Of Code Library/2022/Day 07/Day07Part2.cs @@ -1,5 +1,6 @@ namespace AdventOfCodeLibrary._2022 { + using AdventOfCodeLibrary._2022.Day_07; using AdventOfCodeLibrary.Shared; public class Day07Part2 : Answerable @@ -10,14 +11,12 @@ public override string GetAnswer(byte[] data) { - int markerLength = 14; - for (int skip = 0; skip < data.Length - 1 - markerLength; skip++) - { - if (data.Skip(skip).Take(markerLength).Distinct().Count() == markerLength) - return (skip + markerLength).ToString(); - } - - return "nothing found"; + + int sizeNeeded = 30000000, totalSize = 70000000; + string[] output = GetAsStringArray(data); + DirectoryManager manager = new(); + manager.ParseTerminalOutput(output); + return manager.GetDirectoriesClosestTo(totalSize, sizeNeeded); } } } diff --git a/Advent Of Code Library/2022/Day 07/DirectoryManager.cs b/Advent Of Code Library/2022/Day 07/DirectoryManager.cs index db7c3e5..deefbfe 100644 --- a/Advent Of Code Library/2022/Day 07/DirectoryManager.cs +++ b/Advent Of Code Library/2022/Day 07/DirectoryManager.cs @@ -7,20 +7,19 @@ public List Directories { get; set; } = new(); public List Files { get; set; } = new(); - public long GetSizeOfAllFiles() => Files.Sum(f => f.Size) + Directories.Sum(d => d.GetSizeOfAllFiles()); + public int GetSizeOfAllFiles() => Files.Sum(f => f.Size) + Directories.Sum(d => d.GetSizeOfAllFiles()); } public struct File { public string Name { get; set; } - public long Size { get; set; } + public int Size { get; set; } } internal class DirectoryManager { - Directory RootDirectory; - + readonly Directory RootDirectory; Directory CurrentDirectory; internal DirectoryManager() @@ -41,21 +40,21 @@ // move to other folder if (parts[1] == "cd") { - // go to root - if (parts[2] == "/") + switch (parts[2]) { - CurrentDirectory = RootDirectory; - continue; + // go to root + case "/": + CurrentDirectory = RootDirectory; + continue; + // go to parent + case "..": + CurrentDirectory = CurrentDirectory.ParentDirectory; + continue; + // go to sub + default: + CurrentDirectory = CurrentDirectory.Directories.Single(dir => dir.Name == parts[2]); + break; } - - if (parts[2] == "..") - { - CurrentDirectory = CurrentDirectory.ParentDirectory; - continue; - } - - CurrentDirectory = CurrentDirectory.Directories.Single(dir => dir.Name == parts[2]); - continue; } // if none of the about, just skip @@ -70,7 +69,7 @@ } // it should be a number indicating the size of a file - CurrentDirectory.Files.Add(new File { Name = parts[1], Size = Convert.ToInt64(parts[0]) }); + CurrentDirectory.Files.Add(new File { Name = parts[1], Size = Convert.ToInt32(parts[0]) }); } } @@ -79,11 +78,19 @@ return GetDirectories(RootDirectory).Where(d => d.GetSizeOfAllFiles() <= maxSize).Sum(d => d.GetSizeOfAllFiles()).ToString(); } - private IEnumerable GetDirectories(Directory root) + internal string GetDirectoriesClosestTo(int totalSize, int sizeNeeded) { - yield return root; - foreach (Directory dir in root.Directories) + sizeNeeded -= totalSize - RootDirectory.GetSizeOfAllFiles(); + return GetDirectories(RootDirectory).Where(d => d.GetSizeOfAllFiles() >= sizeNeeded).Min(d => d.GetSizeOfAllFiles()).ToString(); + } + + private IEnumerable GetDirectories(Directory source) + { + yield return source; + foreach (Directory dir in source.Directories.SelectMany(GetDirectories)) + { yield return dir; + } } } } diff --git a/Advent Of Code Library/Shared/Answerable.cs b/Advent Of Code Library/Shared/Answerable.cs index abea5c2..2ce7894 100644 --- a/Advent Of Code Library/Shared/Answerable.cs +++ b/Advent Of Code Library/Shared/Answerable.cs @@ -12,12 +12,12 @@ namespace AdventOfCodeLibrary.Shared public string DefaultInputFile => $"../../../../Advent Of Code Library/{Year}/Day {Day:00}/day-{Day:00}-input.txt"; - protected string NewLine => "\n"; + protected static string NewLine => "\n"; public abstract string GetAnswer(byte[] data); internal static string GetAsString(byte[] bytes) => Encoding.UTF8.GetString(bytes); - internal static string[] GetAsStringArray(byte[] bytes) => Encoding.UTF8.GetString(bytes).Split(Environment.NewLine); + internal static string[] GetAsStringArray(byte[] bytes) => Encoding.UTF8.GetString(bytes).Split(NewLine); } }