-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
156 lines (142 loc) · 2.57 KB
/
index.ts
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import run from "aocrunner";
import { copyFileSync } from "fs";
import { filter, isEmpty, isNumber, isUndefined, keys, map, min, set, sum, toNumber, toPairs } from "lodash-es";
import { array, lines } from "../utils/index.js";
const parseInput = lines(array(String));
const buildTree = (cmd: string[][]) => {
const tree = {};
let currentPath: string[] = [];
for(const line of cmd) {
if (line[0] === '$') {
if (line[1] === 'cd') {
if (line[2] === '..') {
currentPath.pop();
} else {
currentPath.push(line[2]);
}}
} else if (/[0-9]+/.test(line[0])) {
const filePath = [...currentPath, line[1]];
set(tree, filePath, toNumber(line[0]));
}
}
return tree;
}
const getSize = (x): number => {
if (isNumber(x)) {
return x;
}
let size = 0;
for(const k of keys(x)) {
size += getSize(x[k]);
}
return size;
}
const recurse = (x) => {
if(isNumber(x)) {
return 0;
}
const thisSize = getSize(x);
const childSize = sum(map(toPairs(x), ([k,v]: any) => recurse(v)));
if (thisSize < 100000) {
return thisSize + childSize;
}
return childSize;
}
let sizes = [];
const recurse2 = (x, s) => {
if(isNumber(x)) {
return;
}
const thisSize = getSize(x);
sizes.push(thisSize)
const childSizes: number[] = filter(map(toPairs(x), ([k,v]: any) => recurse2(v, s)), z => isNumber(z));
if (thisSize >= s) {
if ((!isEmpty(childSizes) && thisSize > min(childSizes))) {
return min(childSizes);
}
return thisSize;
} else if(!isEmpty(childSizes)) {
return min(childSizes);
}
return;
}
const part1 = (rawInput: string) => {
const input = parseInput(rawInput);
const tree = buildTree(input);
const x =recurse(tree);
return x;
};
const part2 = (rawInput: string) => {
const input = parseInput(rawInput);
const tree = buildTree(input);
const x = recurse2(tree, getSize(tree) - 40000000);
return x;
};
run({
part1: {
tests: [
{
input: `$ cd /
$ ls
dir a
14848514 b.txt
8504156 c.dat
dir d
$ cd a
$ ls
dir e
29116 f
2557 g
62596 h.lst
$ cd e
$ ls
584 i
$ cd ..
$ cd ..
$ cd d
$ ls
4060174 j
8033020 d.log
5626152 d.ext
7214296 k
`,
expected: 95437,
},
],
solution: part1,
},
part2: {
tests: [
{
input: `$ cd /
$ ls
dir a
14848514 b.txt
8504156 c.dat
dir d
$ cd a
$ ls
dir e
29116 f
2557 g
62596 h.lst
$ cd e
$ ls
584 i
$ cd ..
$ cd ..
$ cd d
$ ls
4060174 j
8033020 d.log
5626152 d.ext
7214296 k
`,
expected: 24933642,
},
],
solution: part2,
},
trimTestInputs: true,
onlyTests: false,
});