-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_seven.go
106 lines (88 loc) · 2.77 KB
/
day_seven.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"bufio"
"fmt"
"log"
"os"
"regexp"
"strings"
)
var minRequiredSize = 100000
func daySevenMain() {
file, err := os.Open("inputDate/day_seven_input.txt")
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
processFile(scanner)
}
func processFile(scanner *bufio.Scanner) {
digitCheck := regexp.MustCompile(`^[0-9]+$`) // regex for checking for type int
directoryDepthTrackingList := []string{"/"} // list of distinct folder names including historical depth
directorySizeTrackingList := make(map[string]int) // map of folder keys with size
for scanner.Scan() {
current := string(scanner.Text()) // current line being read from input file
currentSplit := strings.Split(current, " ") // split string by spaces
if digitCheck.MatchString(currentSplit[0]) { // current line read is file and size, add it to current folder total size
// iterate over current list of directories
for i := len(directoryDepthTrackingList) - 1; i >= 0; i-- {
dir := directoryDepthTrackingList[i]
directorySizeTrackingList[dir] += convertToInt(currentSplit[0])
}
} else if strings.Contains(current, "$ cd") {
direction := moveOutOrIn(current)
switch direction {
case "DOWN":
dirDrillDown := strings.Split(current, " ")
directoryDepthTrackingList = append(directoryDepthTrackingList, generateKey(directoryDepthTrackingList, dirDrillDown[2]))
case "UP":
if len(directoryDepthTrackingList) > 0 {
directoryDepthTrackingList = directoryDepthTrackingList[:len(directoryDepthTrackingList)-1]
}
default:
directoryDepthTrackingList = []string{"/"}
}
}
}
calcTotals(directorySizeTrackingList)
fmt.Println("Total Calculated Size for Root Folder - ", directorySizeTrackingList["/"])
fmt.Println("End Program!")
}
// Calculate sum of folder sizes
func calcTotals(dirList map[string]int) {
totalSum := 0
folderSizeToRemove := dirList["/"]
toDeleteLimit := 70000000 - dirList["/"]
if toDeleteLimit < 30000000 {
toDeleteLimit = 30000000 - toDeleteLimit
}
for _, size := range dirList {
if size <= minRequiredSize {
totalSum += size
}
if size >= toDeleteLimit && size < folderSizeToRemove {
folderSizeToRemove = size
}
}
fmt.Println("Some of Folder sizes under limit - Phase 1! ", totalSum)
fmt.Println("Folder to Delete - Phase 2! ", folderSizeToRemove)
}
// folder navigation
func moveOutOrIn(str string) string {
if str == "$ cd /" {
return "ROOT"
} else if str == "$ cd .." {
return "UP"
} else {
return "DOWN"
}
}
// Generate a distinct key for current directory based off of parent folders
func generateKey(list []string, identifier string) string {
tmpKey := ""
for _, current := range list {
tmpKey += string(current)
}
return tmpKey + identifier
}