commit 52dd2774d7008095a7e7c39048e874e694ff6153
parent fcb6488ab7d307a0b9316ee8c7fbfc61b17a3687
Author: Eamon Caddigan <eamon.caddigan@gmail.com>
Date: Wed, 7 Dec 2022 19:13:13 -0800
Solution to day 7, part 2.
Diffstat:
1 file changed, 20 insertions(+), 13 deletions(-)
diff --git a/src/day_7.jl b/src/day_7.jl
@@ -2,26 +2,26 @@
# https://adventofcode.com/2022/day/7
using AdventOfCode
-example = readlines(IOBuffer("""
- \$ cd /
- \$ ls
+example = readlines(IOBuffer(raw"""
+ $ cd /
+ $ ls
dir a
14848514 b.txt
8504156 c.dat
dir d
- \$ cd a
- \$ ls
+ $ cd a
+ $ ls
dir e
29116 f
2557 g
62596 h.lst
- \$ cd e
- \$ ls
+ $ cd e
+ $ ls
584 i
- \$ cd ..
- \$ cd ..
- \$ cd d
- \$ ls
+ $ cd ..
+ $ cd ..
+ $ cd d
+ $ ls
4060174 j
8033020 d.log
5626152 d.ext
@@ -120,7 +120,14 @@ end
@info part_1(input)
function part_2(input)
- nothing
+ # The total disk space available is 70000000, and we need at least 30000000
+ # available. Find out how much we need to free:
+ fs = build_directory(input)
+ dirsizes = size_directories(fs)
+ needtofree = dirsizes[fs] - (70000000 - 30000000)
+
+ # Find the smallest directory, if deleted, that would free up enough space
+ minimum([x for x in values(dirsizes) if x > needtofree])
end
-@assert part_2(example) == nothing
+@assert part_2(example) == 24933642
@info part_2(input)