puzzle.jl 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using BenchmarkTools
  2. using DataStructures
  3. using AbstractTrees
  4. inputfile = joinpath(@__DIR__, "example.txt")
  5. input = readlines(inputfile)
  6. struct Elemen
  7. name::String
  8. size::Int64
  9. loeqwe
  10. end
  11. struct TreeNode
  12. name::String
  13. size::In
  14. children::Vector{Int}
  15. end
  16. function read_files_size(input, Directory, line_number)
  17. println("we are in the $folder")
  18. println(input[line_number])
  19. dir = Dict()
  20. dir[folder] = Dict()
  21. dir["size"] = 0
  22. # dir["size"] = 0
  23. while line_number <= length(input) && !startswith(input[line_number], "\$ cd")
  24. println(input[line_number])
  25. if startswith(input[line_number], "\$ ls")
  26. line_number += 1
  27. elseif startswith(input[line_number], "dir")
  28. # dir[folder][input[line_number][5:end]] = Dict()
  29. # dir[folder][input[line_number][5:end]]["size"] = 0
  30. line_number += 1
  31. else
  32. dir["size"] += parse(Int, split(input[line_number])[1])
  33. line_number += 1
  34. end
  35. end
  36. if startswith(input[line_number], "\$ cd")
  37. println(input[line_number])
  38. if input[line_number][6:end] == ".."
  39. return dir
  40. else
  41. dir[folder] = read_files_size(input, input[line_number][6:end], line_number + 1)
  42. end
  43. end
  44. return dir
  45. end
  46. # fs = read_files_size(input, "/", 2)
  47. # -------------
  48. stack_based_solution() = begin
  49. stack = []; sizes = []
  50. for line in input
  51. if line == "\$ cd .."
  52. size = pop!(stack)
  53. append!(sizes, size)
  54. stack[end] += size
  55. elseif startswith(line, "\$ cd")
  56. append!(stack, 0)
  57. elseif isnumeric(line[1])
  58. stack[end] += parse(Int, split(line)[1])
  59. end
  60. end
  61. s = vcat(sizes, stack)
  62. f = s |> x->filter(y -> y < 100_000, x) |> sum
  63. s = s |> x->filter(y -> y >= maximum(x) - 40_000_000, x) |> minimum
  64. f, s
  65. end
  66. # @btime stack_based_solution()