From f68940b2896f031753b9212832821b00397a0fff Mon Sep 17 00:00:00 2001 From: Luke Hubmayer-Werner Date: Sun, 18 Dec 2022 03:12:54 +1030 Subject: [PATCH] 2022 Day 7 Scala --- 2022/day07.scala | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 2022/day07.scala diff --git a/2022/day07.scala b/2022/day07.scala new file mode 100644 index 0000000..5738b00 --- /dev/null +++ b/2022/day07.scala @@ -0,0 +1,35 @@ +val fileSizePattern = raw"(\d+) ([\w.]+)".r +val cdPattern = "\\$ cd ([\\/\\w.]+)".r // raw string interpolation doesn't like \$ +val rootPattern = raw"\/(.*)".r + +def cd(cwd: String, to: String): String = to match + case rootPattern(absPath) => absPath // Absolute path + case ".." => + val i = cwd.lastIndexOf('/') + if i < 0 then cwd else cwd.substring(0, i) // Up one subfolder + case _ => s"$cwd/$to" + +@main def main() = + val inputLines = scala.io.Source.fromFile("input/07").getLines + + var cwd = "" // We append the / at time of use + val folders = scala.collection.mutable.HashSet[String](cwd) + val fileSizes = scala.collection.mutable.HashMap[String, Int]() + for line <- inputLines do + line match + case cdPattern(dir) => + cwd = cd(cwd, dir) + folders += cwd + case fileSizePattern(sizeStr, filename) => + fileSizes += (s"$cwd/$filename" -> sizeStr.toInt) + case _ => {} // "$ ls", "dir ..." + // Very inefficient way of calculating folder sizes, but cute enough + val folderSizes = folders.map(f => (f, fileSizes.filter((k,v) => k startsWith s"$f/").values.sum)).toMap + + println(s"Part 1: ${folderSizes.values.filter(v => v <= 100_000).sum}") + + val totalCapacity = 70_000_000 + val desiredRemaining = 30_000_000 + val currentRemaining = totalCapacity - folderSizes("") + val sizeToDelete = desiredRemaining - currentRemaining + println(s"Part 2: ${folderSizes.values.filter(v => v > sizeToDelete).min}")