Skip to content

Instantly share code, notes, and snippets.

@bas-kirill
Created April 16, 2025 18:35
Show Gist options
  • Save bas-kirill/2e92f75995d384c1f32612444becdea3 to your computer and use it in GitHub Desktop.
Save bas-kirill/2e92f75995d384c1f32612444becdea3 to your computer and use it in GitHub Desktop.
Solution
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.attribute.BasicFileAttributes
import kotlin.system.exitProcess
import kotlin.streams.toList
fun main(vararg args: String) {
val dir = try {
Path.of(args[0])
} catch (_: Exception) {
println("The target directory must be specified as the only argument of the application")
exitProcess(1)
}
check(Files.exists(dir))
val result = solution(dir)
for ((path, size) in result.fileSizes.entries.sortedBy { it.key }) {
println("$path: $size")
}
println("Total: ${result.totalSize}")
}
class SolutionResult(
val fileSizes: Map<Path, Long>,
val totalSize: Long,
)
private fun solution(dir: Path): SolutionResult {
val fileSizes = mutableMapOf<Path, Long>()
var totalSize = 0L
try {
Files.walk(dir).use { paths ->
for (path in paths.toList()) {
try {
val attr = Files.readAttributes(path, BasicFileAttributes::class.java)
if (attr.isRegularFile) {
val size = Files.size(path)
fileSizes[path] = size
totalSize += size
}
} catch (_: Exception) {
// Ignore files we cannot access or read
}
}
}
} catch (_: Exception) {
// Ignore directory read errors
}
return SolutionResult(fileSizes, totalSize)
}
@vladimirlagunov
Copy link

import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.attribute.BasicFileAttributes

fun solution(dir: Path): SolutionResult {
    val fileSizes = mutableMapOf<Any, Long>()
    val fileNames = mutableMapOf<Any, MutableCollection<Path>>()

    try {
        Files.walk(dir).use { paths ->
            for (path in paths.toList()) {
                try {
                    val attr = Files.readAttributes(path, BasicFileAttributes::class.java)
                    val key = attr.fileKey()
                    if (key in fileNames) {
                        fileNames[key]!!.add(path)
                        continue
                    }
                    fileNames[key] = mutableListOf(path)

                    if (attr.isRegularFile) {
                        val size = Files.size(path)
                        fileSizes[key] = size
                    }
                } catch (_: Exception) {
                    // Ignore files we cannot access or read
                }
            }
        }
    } catch (_: Exception) {
        // Ignore directory read errors
    }

    return SolutionResult(
        fileSizes = fileNames.entries
            .flatMap { (identity, paths) -> paths.map { identity to it } }
            .associate { (identity, path) -> path to (fileSizes[identity] ?: 0) }
            .filter {  (_, fileSize) -> fileSize > 0 },
        totalSize = fileSizes.values.sum(),
    )
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment