Created
December 7, 2022 15:43
-
-
Save tslater2006/66d084dd62a2711a984407150af293d6 to your computer and use it in GitHub Desktop.
Advent of Code 2022 Day 7 Solve with ChatGPT doing the input parsing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| using System.Threading.Tasks; | |
| namespace AdventOfCode | |
| { | |
| /* ChatGPT created class, Renamed by human to not conflict with System.IO.File */ | |
| public class AoCFile | |
| { | |
| public string Name { get; set; } | |
| public int Size { get; set; } | |
| public AoCFile(string name, int size) | |
| { | |
| Name = name; | |
| Size = size; | |
| } | |
| } | |
| /* ChatGPT create class */ | |
| public class Directory | |
| { | |
| public string Name { get; set; } | |
| public List<AoCFile> Files { get; set; } | |
| public List<Directory> Directories { get; set; } | |
| public Directory ParentDirectory { get; set; } | |
| public Directory(string name, Directory parentDirectory = null) | |
| { | |
| Name = name; | |
| Files = new List<AoCFile>(); | |
| Directories = new List<Directory>(); | |
| ParentDirectory = parentDirectory; | |
| } | |
| public int GetTotalSize() | |
| { | |
| // calculate the total size of all files in the directory and its subdirectories | |
| var totalSize = Files.Sum(f => f.Size); | |
| foreach (var directory in Directories) | |
| { | |
| // call the GetTotalSize method recursively for each subdirectory | |
| totalSize += directory.GetTotalSize(); | |
| } | |
| return totalSize; | |
| } | |
| public List<Directory> GetDirectoriesUnderSize(int maxSize) | |
| { | |
| // create a list of directories under the specified size | |
| var directories = new List<Directory>(); | |
| // start at the root directory and call the GetTotalSize method on each subdirectory | |
| foreach (var directory in Directories) | |
| { | |
| var totalSize = directory.GetTotalSize(); | |
| if (totalSize <= maxSize) | |
| { | |
| directories.Add(directory); | |
| } | |
| directories.AddRange(directory.GetDirectoriesUnderSize(maxSize)); | |
| } | |
| return directories; | |
| } | |
| /* HUMAN IMPLEMENTED ALTERATION OF UNDER */ | |
| public List<Directory> GetDirectoriesAboveOrEqualToSize(int minSize) | |
| { | |
| // create a list of directories under the specified size | |
| var directories = new List<Directory>(); | |
| // start at the root directory and call the GetTotalSize method on each subdirectory | |
| foreach (var directory in Directories) | |
| { | |
| var totalSize = directory.GetTotalSize(); | |
| if (totalSize >= minSize) | |
| { | |
| directories.Add(directory); | |
| } | |
| directories.AddRange(directory.GetDirectoriesAboveOrEqualToSize(minSize)); | |
| } | |
| return directories; | |
| } | |
| } | |
| public class DirectoryNavigator | |
| { | |
| public Directory CurrentDirectory { get; set; } | |
| /* HUMAN ADDED PROPERTY */ | |
| public Directory RootDirectory { get; set; } | |
| public DirectoryNavigator(Directory rootDirectory) | |
| { | |
| CurrentDirectory = rootDirectory; | |
| RootDirectory = rootDirectory; | |
| } | |
| public void Cd(string directoryName) | |
| { | |
| if (directoryName == "..") | |
| { | |
| // move up one level if ".." is given | |
| CurrentDirectory = CurrentDirectory.ParentDirectory; | |
| } | |
| else | |
| { | |
| /* HUMAN SUPPORT FOR CD TO ROOT */ | |
| if (directoryName == "/") | |
| { | |
| CurrentDirectory = RootDirectory; | |
| return; | |
| } | |
| // navigate to the specified directory | |
| var directory = CurrentDirectory.Directories | |
| .FirstOrDefault(d => d.Name == directoryName); | |
| if (directory == null) | |
| { | |
| // create a new directory if it does not exist | |
| directory = new Directory(directoryName, CurrentDirectory); | |
| CurrentDirectory.Directories.Add(directory); | |
| } | |
| CurrentDirectory = directory; | |
| } | |
| } | |
| public List<string> Ls(List<string> input) | |
| { | |
| // create a list of strings representing the files and directories in the current directory | |
| var output = new List<string>(); | |
| foreach (var str in input) | |
| { | |
| if (str.StartsWith("dir")) | |
| { | |
| // add the directory name to the output list | |
| output.Add(str); | |
| } | |
| else | |
| { | |
| // split the input into separate file size and file name fields | |
| var fields = str.Split(" "); | |
| var size = int.Parse(fields[0]); | |
| var name = fields[1]; | |
| // create a new File object and add it to the current directory | |
| var file = new AoCFile(name, size); | |
| CurrentDirectory.Files.Add(file); | |
| // add the file size and name to the output list | |
| output.Add($"{file.Size} {file.Name}"); | |
| } | |
| } | |
| return output; | |
| } | |
| } | |
| internal class Day07 : BaseDay | |
| { | |
| /* HUMAN CLASS PROPERTY */ | |
| DirectoryNavigator navigator = new DirectoryNavigator(new Directory("/")); | |
| public Day07() | |
| { | |
| /* HUMAN PARSE INPUT AS LINES */ | |
| var lines = File.ReadAllLines(InputFilePath); | |
| /* ChatGTP code for parsing the input */ | |
| var lsInput = new List<string>(); | |
| foreach (var line in lines) | |
| { | |
| if (line.StartsWith("$ cd") || line.StartsWith("$ ls")) | |
| { | |
| if (lsInput.Any()) | |
| { | |
| // call the Ls method with the input collected so far | |
| navigator.Ls(lsInput); | |
| lsInput.Clear(); | |
| } | |
| if (line.StartsWith("$ cd")) | |
| { | |
| // extract the directory name and call the Cd method | |
| var directoryName = line.Split(" ")[2]; | |
| navigator.Cd(directoryName); | |
| } | |
| } | |
| else | |
| { | |
| // collect input for the Ls method | |
| lsInput.Add(line); | |
| } | |
| } | |
| // call the Ls method with any remaining input | |
| if (lsInput.Any()) | |
| { | |
| navigator.Ls(lsInput); | |
| } | |
| } | |
| /* HUMAN IMPLEMENTED Solve_1 */ | |
| public override ValueTask<string> Solve_1() | |
| { | |
| var ans = navigator.RootDirectory.GetDirectoriesUnderSize(100000).Sum(d => d.GetTotalSize()); | |
| return new ValueTask<string>(ans.ToString()); | |
| } | |
| /* HUMAN IMPLEMENTED Solve_2 */ | |
| public override ValueTask<string> Solve_2() | |
| { | |
| const int FS_SIZE = 70000000; | |
| var freeSpaceRequired = 30000000; | |
| var totalUsed = navigator.RootDirectory.GetTotalSize(); | |
| var freeTarget = freeSpaceRequired - (FS_SIZE - totalUsed); | |
| var bestDirectory = navigator.RootDirectory.GetDirectoriesAboveOrEqualToSize(freeTarget).OrderBy(d => d.GetTotalSize()).First(); | |
| var bestSize = bestDirectory.GetTotalSize(); | |
| return new(bestSize.ToString()); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment