Advertisement
Guest User

Advent of Code 2022 Day 7

a guest
Jan 13th, 2023
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.71 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "io/ioutil"
  6.     "sort"
  7.     "strconv"
  8.     "strings"
  9. )
  10.  
  11. type File struct {
  12.     Name string
  13.     Size int
  14. }
  15.  
  16. type Directory struct {
  17.     Name       string
  18.     Parent     *Directory
  19.     ChildDirs  map[string]*Directory
  20.     ChildFiles []File
  21.     Size       int
  22. }
  23.  
  24. func main() {
  25.     input, _ := ioutil.ReadFile("day_seven/input.txt")
  26.     splitInput := strings.Split(strings.TrimSpace(string(input)), "\n")
  27.  
  28.     root := makeDirectory("root", nil)
  29.  
  30.     var command, arg string
  31.     var currentDir *Directory
  32.     for i := 0; i < len(splitInput); i++ {
  33.         fmt.Sscanf(splitInput[i], "$ %s %s", &command, &arg)
  34.  
  35.         if command == "cd" {
  36.             if arg == "/" {
  37.                 currentDir = root
  38.             } else if arg == ".." {
  39.                 currentDir = currentDir.Parent
  40.             } else {
  41.                 currentDir = currentDir.ChildDirs[arg]
  42.             }
  43.         } else if command == "ls" {
  44.             j := i + 1
  45.             for j < len(splitInput) && string(splitInput[j][0]) != "$" {
  46.                 s := strings.Split(splitInput[j], " ")
  47.                 part1, part2 := s[0], s[1]
  48.                 if part1 == "dir" {
  49.                     if _, ok := currentDir.ChildDirs[part2]; !ok {
  50.                         dir := makeDirectory(part2, currentDir)
  51.                         currentDir.ChildDirs[dir.Name] = dir
  52.                     }
  53.                 } else {
  54.                     size, _ := strconv.Atoi(part1)
  55.                     file := File{Name: part2, Size: size}
  56.                     currentDir.ChildFiles = append(currentDir.ChildFiles, file)
  57.                 }
  58.  
  59.                 j++
  60.             }
  61.             i = j - 1
  62.         }
  63.     }
  64.  
  65.     hydrateDirectorySize(root)
  66.     dirs := findDirectoriesOfAtMost(root, 100000)
  67.     totalSize := 0
  68.     for _, dir := range dirs {
  69.         totalSize += dir.Size
  70.     }
  71.     println(totalSize)
  72.  
  73.     directorySizes := getDirectorySizes(root)
  74.     sort.Ints(directorySizes)
  75.     sizeNeeded := 30000000 - (70000000 - root.Size)
  76.     var smallestPossible int
  77.     for i, size := range directorySizes {
  78.         _ = i
  79.         if size >= sizeNeeded {
  80.             smallestPossible = size
  81.             break
  82.         }
  83.     }
  84.     println(sizeNeeded)
  85.     println(smallestPossible)
  86.  
  87. }
  88.  
  89. func hydrateDirectorySize(dir *Directory) {
  90.     for _, file := range dir.ChildFiles {
  91.         dir.Size += file.Size
  92.     }
  93.  
  94.     for _, childDir := range dir.ChildDirs {
  95.         hydrateDirectorySize(childDir)
  96.         dir.Size += childDir.Size
  97.     }
  98. }
  99.  
  100. func findDirectoriesOfAtMost(dir *Directory, size int) []*Directory {
  101.     var dirs []*Directory
  102.     if dir.Size <= size {
  103.         dirs = append(dirs, dir)
  104.     }
  105.  
  106.     for _, childDir := range dir.ChildDirs {
  107.         dirs = append(dirs, findDirectoriesOfAtMost(childDir, size)...)
  108.     }
  109.  
  110.     return dirs
  111. }
  112.  
  113. func getDirectorySizes(dir *Directory) []int {
  114.     var sizes []int
  115.     sizes = append(sizes, dir.Size)
  116.     for _, dir := range dir.ChildDirs {
  117.         sizes = append(sizes, getDirectorySizes(dir)...)
  118.     }
  119.  
  120.     return sizes
  121. }
  122.  
  123. func makeDirectory(name string, parentDir *Directory) *Directory {
  124.     return &Directory{Name: name, Parent: parentDir, ChildDirs: map[string]*Directory{}}
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement