-
Notifications
You must be signed in to change notification settings - Fork 0
/
day7.ps1
93 lines (65 loc) · 2.35 KB
/
day7.ps1
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
# Day 7: No Space Left On Device
# Used the file system instead of creating a tree structure
# Interpreted the input as commands to create the files and folders
# Then used get-childitem and measure-object
##
# Part 1
##
$data = Get-Content input.txt
$data = $data -replace '/', '.'
$basepath = Get-Location
New-Item -ItemType Directory -Name "root"
Set-Location -Path .\root
for ($x=0; $x-lt $data.Length; $x++) {
$line = $data[$x] -split " "
if ($line[0] -eq '$') {
if ($line[1] -eq 'cd') {
if ($line[2] -ne '..') {
new-item -ItemType Directory -Name $line[2]
Set-Location $line[2]
}
elseif ($line[2] -eq '..') {
set-location $line[2]
}
}
}
if ($line[0] -ne '$') {
$line -join ","
if ($line[0] -eq 'dir') {
"it is a dir"
} else {
$fullpath = $(get-location).Path + "\" + $line[1]
$filesize = $line[0]
$file = [System.IO.File]::Create($fullpath)
$file.SetLength($filesize)
$file.Close()
}
}
}
$basepath | Set-Location
$sum =0
$folderlist = get-childitem -Path .\root -Directory -recurse | Select-Object -Property Fullname
foreach ($folder in $folderlist) {
$foldersize = (get-childitem -Path $folder.FullName -Recurse | Measure-Object -Property length -sum).Sum
if ($foldersize -le 100000) {
"{0}:{1}" -f $foldersize, $folder.fullname
$sum += $foldersize
}
}
"Total: {0}" -f $sum
##
# Part 2
##
$spaceused = (get-childitem -path .\root\* -Recurse | Measure-Object -Property length -sum).sum
$spaceavailable = 70000000
$spaceneeded = 30000000
$freeup = $spaceneeded - ($spaceavailable - $spaceused)
$currentlow = 99999999999
$basepath | Set-Location
$folderlist = get-childitem -Path .\root -Directory -recurse | Select-Object -Property Fullname
foreach ($folder in $folderlist) {
$foldersize = (get-childitem -Path $folder.FullName -Recurse | Measure-Object -Property length -sum).Sum
if ($foldersize -ge $freeup) {
if ($foldersize -lt $currentlow) { $currentlow = $foldersize; "current low {0}:{1}" -f $currentlow, $folder.fullname}
}
}