AOC day 7

This commit is contained in:
2022-12-10 22:06:06 +00:00
parent b1a74740a2
commit 6ffff80070
4 changed files with 1100 additions and 1 deletions

View File

@@ -0,0 +1,60 @@
(import year2022.FileSystem)
(function :FileSystem reconstructFileSystem [file]
(let [:FileSystem rootFs (Folder [=>"/" (Folder (new Map))])
&mut :FileSystem currFs rootFs
terminalLines (Util.readLines file)]
(doFor line terminalLines
(case (line.split " ")
(["\$" "ls"] null)
(["dir" dir] null)
(["\$" "cd" dir]
(case currFs
((Folder contents)
(if (contents.exists dir)
(set currFs (dictGet contents dir))
(let [newFolder (Folder [=>".." currFs])]
(dictSet contents dir newFolder)
(set currFs newFolder))))
(never otherwise)))
([size file]
(case currFs
((Folder contents)
(dictSet contents file (File (Std.parseInt size))))
(never otherwise)))
(never otherwise)))
rootFs))
(var &mut :Float sumUnder100000)
(function :SizeStats sizeStats [:FileSystem fs &opt :Array<String> path :SizeStats stats]
(unless path
(set path [])
(set sumUnder100000 0))
(unless stats (set stats (new Map)))
(when !(stats.exists (path.join "/"))
(dictSet stats (path.join "/")
(case fs
((Folder contents)
(let [size
(apply +
(for =>innerPath innerFs contents
(ifLet [".." innerPath]
0
(let [fullPath (path.concat [innerPath])]
(sizeStats innerFs fullPath stats)
(dictGet stats (fullPath.join "/"))))))]
(when (>= 100000 size) (+= sumUnder100000 size))
size))
((File size)
size)
(never otherwise))))
stats)
(let [fs (reconstructFileSystem (input 7))
stats (sizeStats fs)
sizeAvailable (- 70000000 (dictGet stats "/"))
minSizeToDelete (- 30000000 sizeAvailable)]
(assert (= 1449447 sumUnder100000))
(assert (= 8679207 (apply min (for =>path size stats
(if (>= size minSizeToDelete)
size
(continue)))))))

View File

@@ -0,0 +1,8 @@
package year2022;
typedef SizeStats = Map<String,Int>;
enum FileSystem {
File(size:Int);
Folder(contents:Map<String,FileSystem>);
}

View File

@@ -9,7 +9,7 @@
(day 4 (load "Day4.kiss"))
(day 5 (load "Day5.kiss"))
(day 6 (load "Day6.kiss"))
(dayTodo 7)//(day 7 (load "Day7.kiss"))
(day 7 (load "Day7.kiss"))
(dayTodo 8)//(day 8 (load "Day8.kiss"))
(dayTodo 9)//(day 9 (load "Day9.kiss"))
(dayTodo 10)//(day 10 (load "Day10.kiss"))

File diff suppressed because it is too large Load Diff