Advertisement
astrosofista

AoC7ab 2022

Dec 7th, 2022 (edited)
463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Autohotkey 0.95 KB | Source Code | 0 0
  1. ProcessFile(fileName, part) {
  2.     FileRead, data, % fileName
  3.  
  4.     arrDirs  := []
  5.     dictSize := {}
  6.  
  7.     For k, v in StrSplit(data, "`n") {
  8.         if RegExMatch(v, "^\$ cd +(.*)", m) {
  9.             if (m1 = "/") {
  10.                 arrDirs.push("/")
  11.             } else if (m1 = "..") {
  12.                 arrDirs.pop()
  13.             } else {
  14.                 arrDirs.push(arrDirs[arrDirs.count()] "/" m1)
  15.             }
  16.         } else if RegExMatch(v, "^(\d+)", m) {
  17.             For j, w in arrDirs {
  18.                 if !dictSize.hasKey(w)
  19.                     dictSize[w] := 0
  20.                 dictSize[w] += m
  21.             }
  22.         }
  23.     }
  24.  
  25.     if (part = 1) {
  26.         totalSize := 0
  27.         For k, v in dictSize {
  28.             if (v <= 100000)
  29.                 totalSize += v
  30.         }
  31.     } else if (part = 2) {
  32.         totalSize := 70000000
  33.         threshold := 30000000 - (totalSize - dictSize["/"])
  34.         for k, v in dictSize {
  35.             if (v >= threshold) && (v < totalSize)
  36.                 totalSize := v
  37.         }
  38.     } else
  39.         return "Invalid parameter value"
  40.  
  41.     return totalSize
  42. }
  43.  
  44. MsgBox, % ProcessFile(A_ScriptDir "\aoc7.txt", 1)
  45. MsgBox, % ProcessFile(A_ScriptDir "\aoc7.txt", 2)
  46.  
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement