Fixed and completed day 6
This commit is contained in:
parent
d311ea97b7
commit
e78e715f16
@ -1,14 +1,33 @@
|
|||||||
using AdventOfCodeLibrary._2022;
|
using System.Text;
|
||||||
|
|
||||||
|
using AdventOfCodeLibrary._2022;
|
||||||
using AdventOfCodeLibrary.Shared;
|
using AdventOfCodeLibrary.Shared;
|
||||||
|
|
||||||
string _demoData = @"2-4,6-8
|
string _demoData = @"$ cd /
|
||||||
2-3,4-5
|
$ ls
|
||||||
5-7,7-9
|
dir a
|
||||||
2-8,3-7
|
14848514 b.txt
|
||||||
6-6,4-6
|
8504156 c.dat
|
||||||
2-6,4-8";
|
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);
|
byte[] dataArray = File.ReadAllBytes(answerable.DefaultInputFile);
|
||||||
//dataArray = Encoding.UTF8.GetBytes(_demoData);
|
//dataArray = Encoding.UTF8.GetBytes(_demoData);
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
namespace AdventOfCodeLibrary._2022
|
namespace AdventOfCodeLibrary._2022
|
||||||
{
|
{
|
||||||
|
using AdventOfCodeLibrary._2022.Day_07;
|
||||||
using AdventOfCodeLibrary.Shared;
|
using AdventOfCodeLibrary.Shared;
|
||||||
|
|
||||||
public class Day07Part2 : Answerable
|
public class Day07Part2 : Answerable
|
||||||
@ -10,14 +11,12 @@
|
|||||||
|
|
||||||
public override string GetAnswer(byte[] data)
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,20 +7,19 @@
|
|||||||
public List<Directory> Directories { get; set; } = new();
|
public List<Directory> Directories { get; set; } = new();
|
||||||
public List<File> Files { get; set; } = new();
|
public List<File> 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 struct File
|
||||||
{
|
{
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
|
|
||||||
public long Size { get; set; }
|
public int Size { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
internal class DirectoryManager
|
internal class DirectoryManager
|
||||||
{
|
{
|
||||||
Directory RootDirectory;
|
readonly Directory RootDirectory;
|
||||||
|
|
||||||
Directory CurrentDirectory;
|
Directory CurrentDirectory;
|
||||||
|
|
||||||
internal DirectoryManager()
|
internal DirectoryManager()
|
||||||
@ -41,21 +40,21 @@
|
|||||||
// move to other folder
|
// move to other folder
|
||||||
if (parts[1] == "cd")
|
if (parts[1] == "cd")
|
||||||
{
|
{
|
||||||
// go to root
|
switch (parts[2])
|
||||||
if (parts[2] == "/")
|
|
||||||
{
|
{
|
||||||
|
// go to root
|
||||||
|
case "/":
|
||||||
CurrentDirectory = RootDirectory;
|
CurrentDirectory = RootDirectory;
|
||||||
continue;
|
continue;
|
||||||
}
|
// go to parent
|
||||||
|
case "..":
|
||||||
if (parts[2] == "..")
|
|
||||||
{
|
|
||||||
CurrentDirectory = CurrentDirectory.ParentDirectory;
|
CurrentDirectory = CurrentDirectory.ParentDirectory;
|
||||||
continue;
|
continue;
|
||||||
}
|
// go to sub
|
||||||
|
default:
|
||||||
CurrentDirectory = CurrentDirectory.Directories.Single(dir => dir.Name == parts[2]);
|
CurrentDirectory = CurrentDirectory.Directories.Single(dir => dir.Name == parts[2]);
|
||||||
continue;
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// if none of the about, just skip
|
// if none of the about, just skip
|
||||||
@ -70,7 +69,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
// it should be a number indicating the size of a file
|
// 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();
|
return GetDirectories(RootDirectory).Where(d => d.GetSizeOfAllFiles() <= maxSize).Sum(d => d.GetSizeOfAllFiles()).ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private IEnumerable<Directory> GetDirectories(Directory root)
|
internal string GetDirectoriesClosestTo(int totalSize, int sizeNeeded)
|
||||||
|
{
|
||||||
|
sizeNeeded -= totalSize - RootDirectory.GetSizeOfAllFiles();
|
||||||
|
return GetDirectories(RootDirectory).Where(d => d.GetSizeOfAllFiles() >= sizeNeeded).Min(d => d.GetSizeOfAllFiles()).ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private IEnumerable<Directory> GetDirectories(Directory source)
|
||||||
|
{
|
||||||
|
yield return source;
|
||||||
|
foreach (Directory dir in source.Directories.SelectMany(GetDirectories))
|
||||||
{
|
{
|
||||||
yield return root;
|
|
||||||
foreach (Directory dir in root.Directories)
|
|
||||||
yield return dir;
|
yield return dir;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -12,12 +12,12 @@ namespace AdventOfCodeLibrary.Shared
|
|||||||
|
|
||||||
public string DefaultInputFile => $"../../../../Advent Of Code Library/{Year}/Day {Day:00}/day-{Day:00}-input.txt";
|
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);
|
public abstract string GetAnswer(byte[] data);
|
||||||
|
|
||||||
internal static string GetAsString(byte[] bytes) => Encoding.UTF8.GetString(bytes);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user