-
Notifications
You must be signed in to change notification settings - Fork 0
/
AoC Day 07.m
45 lines (33 loc) · 969 Bytes
/
AoC Day 07.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
(* ::Package:: *)
(* ::Text:: *)
(*Written December 7th, 2022.*)
(*Import*)
day = 7;
inputPath = FileNameJoin[{NotebookDirectory[], "Day" <> ToString[day] <> "Input.txt"}];
input=Import[inputPath,"List"][[1]];
(*Setup*)
directory = {}; level = {"/"};
Do[
Which[
line[[2]] == "cd",
Which[
line[[3]] == "/", level = {"/"},
line[[3]] == "..", level = level[[;; -2]],
True, level = Join[level, {line[[3]]}]
],
IntegerQ[line[[1]]],
AppendTo[directory, Join[level, {line[[2]]}] -> line[[1]]];
]
, {line, input}];
ClearAll[fileSizes];
fileSizes[l_] := fileSizes[l] = 0;
Do[
fileSizes[directory[[f, 1, ;; i]]] += directory[[f, 2]],
{f, Length[directory]},
{i, Length[directory[[f, 1]]] - 1}];
(*Part 1*)
Total[Select[DownValues[fileSizes][[;; , 2]], # <= 100000 &]]
(*Part 2*)
used = Max[DownValues[fileSizes][[;; , 2]]];
unused = 30000000 - (70000000 - used);
Min[Select[DownValues[fileSizes][[;; , 2]], # >= unused &]]