Added day 7, not done
This commit is contained in:
parent
1b91473395
commit
d311ea97b7
@ -8,7 +8,7 @@ string _demoData = @"2-4,6-8
|
|||||||
6-6,4-6
|
6-6,4-6
|
||||||
2-6,4-8";
|
2-6,4-8";
|
||||||
|
|
||||||
Answerable answerable = new Day01Part1();
|
Answerable answerable = new Day07Part1();
|
||||||
byte[] dataArray = File.ReadAllBytes(answerable.DefaultInputFile);
|
byte[] dataArray = File.ReadAllBytes(answerable.DefaultInputFile);
|
||||||
//dataArray = Encoding.UTF8.GetBytes(_demoData);
|
//dataArray = Encoding.UTF8.GetBytes(_demoData);
|
||||||
|
|
||||||
|
|||||||
21
Advent Of Code Library/2022/Day 07/Day07Part1.cs
Normal file
21
Advent Of Code Library/2022/Day 07/Day07Part1.cs
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
namespace AdventOfCodeLibrary._2022
|
||||||
|
{
|
||||||
|
using AdventOfCodeLibrary._2022.Day_07;
|
||||||
|
using AdventOfCodeLibrary.Shared;
|
||||||
|
|
||||||
|
public class Day07Part1 : Answerable
|
||||||
|
{
|
||||||
|
public override int Year { get; set; } = 2022;
|
||||||
|
public override int Day { get; set; } = 7;
|
||||||
|
public override int Part { get; set; } = 1;
|
||||||
|
|
||||||
|
public override string GetAnswer(byte[] data)
|
||||||
|
{
|
||||||
|
int maxSize = 100000;
|
||||||
|
string[] output = GetAsStringArray(data);
|
||||||
|
DirectoryManager manager = new();
|
||||||
|
manager.ParseTerminalOutput(output);
|
||||||
|
return manager.GetDirectoriesWithMaxFilesSize(maxSize);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
23
Advent Of Code Library/2022/Day 07/Day07Part2.cs
Normal file
23
Advent Of Code Library/2022/Day 07/Day07Part2.cs
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
namespace AdventOfCodeLibrary._2022
|
||||||
|
{
|
||||||
|
using AdventOfCodeLibrary.Shared;
|
||||||
|
|
||||||
|
public class Day07Part2 : Answerable
|
||||||
|
{
|
||||||
|
public override int Year { get; set; } = 2022;
|
||||||
|
public override int Day { get; set; } = 7;
|
||||||
|
public override int Part { get; set; } = 2;
|
||||||
|
|
||||||
|
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";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
89
Advent Of Code Library/2022/Day 07/DirectoryManager.cs
Normal file
89
Advent Of Code Library/2022/Day 07/DirectoryManager.cs
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
namespace AdventOfCodeLibrary._2022.Day_07
|
||||||
|
{
|
||||||
|
public class Directory
|
||||||
|
{
|
||||||
|
public Directory? ParentDirectory { get; set; }
|
||||||
|
public string Name { get; set; } = string.Empty;
|
||||||
|
public List<Directory> Directories { get; set; } = new();
|
||||||
|
public List<File> Files { get; set; } = new();
|
||||||
|
|
||||||
|
public long GetSizeOfAllFiles() => Files.Sum(f => f.Size) + Directories.Sum(d => d.GetSizeOfAllFiles());
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct File
|
||||||
|
{
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
public long Size { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
internal class DirectoryManager
|
||||||
|
{
|
||||||
|
Directory RootDirectory;
|
||||||
|
|
||||||
|
Directory CurrentDirectory;
|
||||||
|
|
||||||
|
internal DirectoryManager()
|
||||||
|
{
|
||||||
|
RootDirectory = new Directory { Name = "/", ParentDirectory = null };
|
||||||
|
CurrentDirectory = RootDirectory;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal void ParseTerminalOutput(string[] lines)
|
||||||
|
{
|
||||||
|
for (int lineIndex = 0; lineIndex < lines.Length; lineIndex++)
|
||||||
|
{
|
||||||
|
string[] parts = lines[lineIndex].Split(' ');
|
||||||
|
|
||||||
|
// it is a command
|
||||||
|
if (parts[0] == "$")
|
||||||
|
{
|
||||||
|
// move to other folder
|
||||||
|
if (parts[1] == "cd")
|
||||||
|
{
|
||||||
|
// go to root
|
||||||
|
if (parts[2] == "/")
|
||||||
|
{
|
||||||
|
CurrentDirectory = RootDirectory;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parts[2] == "..")
|
||||||
|
{
|
||||||
|
CurrentDirectory = CurrentDirectory.ParentDirectory;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
CurrentDirectory = CurrentDirectory.Directories.Single(dir => dir.Name == parts[2]);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// if none of the about, just skip
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// if not a command it is the output from the ls command
|
||||||
|
if (parts[0] == "dir")
|
||||||
|
{
|
||||||
|
CurrentDirectory.Directories.Add(new Directory { Name = parts[1], ParentDirectory = CurrentDirectory });
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// it should be a number indicating the size of a file
|
||||||
|
CurrentDirectory.Files.Add(new File { Name = parts[1], Size = Convert.ToInt64(parts[0]) });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal string GetDirectoriesWithMaxFilesSize(int maxSize)
|
||||||
|
{
|
||||||
|
return GetDirectories(RootDirectory).Where(d => d.GetSizeOfAllFiles() <= maxSize).Sum(d => d.GetSizeOfAllFiles()).ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private IEnumerable<Directory> GetDirectories(Directory root)
|
||||||
|
{
|
||||||
|
yield return root;
|
||||||
|
foreach (Directory dir in root.Directories)
|
||||||
|
yield return dir;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
1096
Advent Of Code Library/2022/Day 07/day-07-input.txt
Normal file
1096
Advent Of Code Library/2022/Day 07/day-07-input.txt
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user