Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- RawData := FileOpen(A_ScriptDir "\aoc7.txt", "r").Read(), RawData.Close()
- Data := StrSplit(RawData, "`n", "`r")
- FileTree := {"/":{Child: [], files:[]}}
- for k, v in Data
- {
- line := StrSplit(v, A_Space)
- if (line[1] = "$")
- {
- if (line[2] = "cd")
- {
- Switch line[3]
- {
- Case "/":
- FullPath := "/"
- Case "..":
- FullPath := SubStr(FullPath, 1, InStr(FullPath, "\", ,0)-1)
- Default:
- FullPath := FullPath "\" line[3]
- }
- }
- }
- else if (line[1] = "dir")
- {
- FileTree[FullPath "\" line[2]] := {child: [], files: []}
- FileTree[FullPath].child.push(FullPath "\" line[2])
- }
- else
- FileTree[FullPath].files.push(line[1])
- }
- ; part 1
- total := 0
- for k, dir in FileTree
- total += (s := SubSum(dir)) > 100000 ? 0 : s
- msgbox % "part1: " total
- ; part 2
- totalSize := 0
- for k, dir in FileTree
- for i, size in dir.files
- totalSize += size
- absoluteSpace := 70000000, neededSpace := 30000000
- toFree := neededSpace - (absoluteSpace - totalSize)
- cDir := totalSize
- for k, dir in FileTree
- {
- size := SubSum(dir)
- if ((size >= toFree) AND (size < cDir))
- cDir := size
- }
- msgbox % "part2: " cDir
- ; func
- ; returns sum of all .files size in folder + all child folders
- SubSum(obj)
- {
- global FileTree
- sum := 0
- for k, size in obj.files
- sum += size
- if obj.child.count()
- for k, child in obj.child
- sum += SubSum(FileTree[child])
- return sum
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement