-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday07_part01.fs
97 lines (89 loc) · 3.81 KB
/
day07_part01.fs
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
module day07_part01
open AdventOfCode_Utilities
open AoC_2022.Modules
open AdventOfCode_2022.Modules.LocalHelper
//let path = "day07/test_input_01.txt"
let path = "day07/day07_input.txt"
let inputLines = GetLinesFromFile(path) |> Seq.toList
let createElement (parent: option<FileSystemItem>) (input: string) =
let newElement =
match input.Split(' ') with
| parts when parts.[0] = "dir" ->
let directory = {
Name = parts.[1];
NodeType = NodeEnum.Directory;
Size = 0;
Parent = if parent.IsSome then parent.Value.Path else "";
Path = if parent.IsSome then parent.Value.Path + "§" + parts.[1] else ""
}
directory
| parts ->
let file = {
Name = parts.[1];
NodeType = NodeEnum.File;
Size= (int)(parts.[0]);
Parent = if parent.IsSome then parent.Value.Path else "";
Path = if parent.IsSome then parent.Value.Path + "§" + parts.[1] else ""
}
file
newElement
let rec updateParent (elements: FileSystemItem list) (current: FileSystemItem) (size: int) =
let newCurrent = {
Name = current.Name;
NodeType = current.NodeType;
Size = current.Size + size;
Parent = current.Parent;
Path = current.Path
}
let newElements =
match (elements |> List.tryFindIndex(fun e -> e.Path = current.Path)) with
| Some currenIdx ->
Utilities.updateElement currenIdx newCurrent elements
| None -> elements
match (newElements |> List.tryFindIndex(fun e -> e.Path = newCurrent.Parent)) with
| Some currenIdx ->
updateParent newElements (newElements.Item(currenIdx)) size
| None -> newElements
let rec parseInstruction
(elements: FileSystemItem list) (current: FileSystemItem) (instructions: string list) =
match instructions with
| head :: tail ->
match head.StartsWith("$") with
| true ->
match head.Split(' ') with
| opParts when opParts.Length = 3 && opParts.[2] = ".." -> // "$ cd .."
let newCurrent = elements |> List.find(fun e -> e.Path = current.Parent)
parseInstruction elements newCurrent tail
| opParts when opParts.Length = 3 -> // "$ cd /" or "$ cd <dir>"
match opParts.[2] with
| "/" -> // "$ cd /"
parseInstruction elements elements.Head tail
| _ -> // "$ cd <dir>"
let newCurrent = elements |> List.find(fun e -> e.Path = current.Path + "§" + opParts.[2])
parseInstruction elements newCurrent tail
| _ -> // $ ls
parseInstruction elements current tail // $ ls
| false ->
let newElement = createElement (Some(current)) head
let tmpElements =
if elements |> List.exists(fun e -> e.Path = newElement.Path) then
elements
else
elements @ [newElement]
let newElements =
match newElement.Size with
| 0 -> tmpElements
| _ -> updateParent tmpElements current newElement.Size
let newCurrent = newElements |> List.find(fun e -> e.Path = current.Path)
parseInstruction newElements newCurrent tail
| [] -> elements
let execute =
let startElement = {
Name = "/";
NodeType = NodeEnum.Directory;
Size = 0;
Parent = "";
Path = $"/"
}
let structure = parseInstruction [startElement] startElement inputLines
structure |> List.filter(fun f -> f.Size <=100000 && f.NodeType = NodeEnum.Directory) |> List.sumBy(fun e-> e.Size)