-
Notifications
You must be signed in to change notification settings - Fork 0
/
day07.dwl
69 lines (60 loc) · 2.11 KB
/
day07.dwl
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
%dw 2.0
output application/json
import * from dw::core::Arrays
import * from dw::core::Strings
type File = {|
name: String,
size: Number
|}
type Directory = {|
path: Array<String>,
size?: Number,
children: Array<String | File>
|}
type Context = {| rest: Array<String>, parsed: Directory |}
@TailRec()
fun ls(context: Context): Context = context.rest match {
case [] -> // end of ls
{ rest: context.rest, parsed: context.parsed }
case [cur ~ rest] -> if (cur startsWith "\$") // end of ls
{ rest: context.rest, parsed: context.parsed }
else ls({
rest: rest,
parsed: {
path: context.parsed.path,
children: if(cur startsWith "dir") // found dir
context.parsed.children << words(cur)[1]
else // found file
context.parsed.children << (words(cur) then {
name: $[1],
size: $[0] as Number
})
}})
}
@TailRec()
fun parse(rest: Array<String>, path: Array<String> = [], parsed: Array<Directory> = []): Array<Directory> =
rest match {
case [] -> parsed
case [current ~ newRest] ->
if (current startsWith "\$ ls")
ls({ rest: newRest, parsed: { path: path, children: [] } })
then parse($.rest, path, parsed + $.parsed)
else parse(
newRest,
if (current startsWith "\$ cd ..") path[0 to -2] else path + words(current)[2],
parsed
)
}
fun matchPaths(current: Array<String>, toCheck: Array<String>) = current map $ == toCheck[$$] every $
fun dirSizes(dirs: Array<Directory>): Array<Directory> = dirs map (cur) -> {
path: cur.path,
children: cur.children,
size: sum(dirs filter (matchPaths(cur.path, $.path)) flatMap ($.children filter ($ is File) map $.size))
}
var parsed = parse(lines(payload)) then dirSizes($)
var occupied = max(parsed.size)
---
{
part1: sum(parsed.size filter ($ <= 100000)),
part2: min((parsed filter (occupied - $.size < 70000000 - 30000000)).size)
}