Skip to content

Commit

Permalink
Day7 solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
yashpatil1998 committed Dec 8, 2022
1 parent fdb6900 commit 763a003
Show file tree
Hide file tree
Showing 3 changed files with 1,136 additions and 0 deletions.
104 changes: 104 additions & 0 deletions day7/day7.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package day7

import (
"strconv"
"strings"
)

func Day7Logic() (int64, int64) {
input := strings.Split(input7, "\n")
root := buildFileSystem(input)

unusedDiskSpace := part2TotalDiskSpace - root.Size
spaceToFreeUp := part2SpaceNeeded - unusedDiskSpace

part1, part2 := day7Logic(&root, spaceToFreeUp, root.Size)

return part1, part2
}

type directory struct {
Name string
Directories []*directory
Files []file
Parent *directory
Size int64
}

type file struct {
Name string
Size int64
}

const part1DirMaxLimit = 100000
const part2TotalDiskSpace = 70000000
const part2SpaceNeeded = 30000000

func day7Logic(dir *directory, spaceNeeded int64, spaceToDelete int64) (int64, int64) {

var part1 int64
var part2 = spaceToDelete

if dir.Size <= part1DirMaxLimit {
part1 += dir.Size
}

if dir.Size >= spaceNeeded && dir.Size < part2 {
part2 = dir.Size
}

for _, d := range dir.Directories {
temp1, temp2 := day7Logic(d, spaceNeeded, part2)

part1 += temp1
part2 = temp2
}

return part1, part2
}

func buildFileSystem(commands []string) directory {
var root directory
var currentDir = &root

for _, command := range commands {
cmd := strings.Split(command, " ")
if strings.HasPrefix(command, "$") {
// if here, means input is a command
switch cmd[1] {
case "cd":
if cmd[2] == ".." {
currentDir = currentDir.Parent
} else {
for _, dir := range currentDir.Directories {
if dir.Name == cmd[2] {
currentDir = dir
}
}
}
case "ls":
}
} else {
// if here, means input is a file or directory
switch cmd[0] {
case "dir":
dir := directory{Name: cmd[1], Parent: currentDir}
currentDir.Directories = append(currentDir.Directories, &dir)
default:
size, _ := strconv.ParseInt(cmd[0], 10, 64)
newFile := file{Name: cmd[1], Size: size}

currentDir.Files = append(currentDir.Files, newFile)
increaseDirSize(size, currentDir)
}
}
}
return root
}

func increaseDirSize(size int64, dir *directory) {
dir.Size += size
if dir.Parent != nil {
increaseDirSize(size, dir.Parent)
}
}
Loading

0 comments on commit 763a003

Please sign in to comment.