File tree Expand file tree Collapse file tree 1 file changed +62
-0
lines changed
Expand file tree Collapse file tree 1 file changed +62
-0
lines changed Original file line number Diff line number Diff line change 1+ ---
2+ descriptions : ["deno"]
3+ ---
4+
5+ ### 2025 Solution Deno
6+
7+ ### Output
8+
9+ ```
10+ Off Balance Weight: 580
11+ ```
12+
13+ ``` ts
14+ class Tree {
15+ private trunkWeight: number ;
16+ private LeftWeight: number ;
17+ private RightWeight: number ;
18+
19+ constructor (treeSpec : string ) {
20+ const treeParts = treeSpec .split (" \n " );
21+ this .trunkWeight = Number (treeParts [0 ]);
22+
23+ const branches = treeParts [1 ].split (" " );
24+ const ornaments = treeParts [2 ].split (" " );
25+
26+ for (let i = 0 ; i < ornaments .length ; i ++ ) {
27+ if (ornaments [i ] == " _" ) {
28+ ornaments [i ] = " 0" ;
29+ }
30+ }
31+
32+ this .LeftWeight =
33+ Number (branches [0 ]) + Number (ornaments [0 ]) + Number (ornaments [1 ]);
34+ this .RightWeight =
35+ Number (branches [1 ]) + Number (ornaments [2 ]) + Number (ornaments [3 ]);
36+ }
37+
38+ public isBalanced(): boolean {
39+ return this .LeftWeight - this .RightWeight > 1 ||
40+ this .RightWeight - this .LeftWeight > 1
41+ ? false
42+ : true ;
43+ }
44+ public getWeight(): number {
45+ return this .trunkWeight + this .LeftWeight + this .RightWeight ;
46+ }
47+ }
48+
49+ if (import .meta .main ) {
50+ const trees = (await Deno .readTextFile (" trees.txt" )).split (" \n\n " );
51+ let weightCounter = 0 ;
52+
53+ for (let i = 0 ; i < trees .length ; i ++ ) {
54+ const tree = new Tree (trees [i ]);
55+ if (! tree .isBalanced ()) {
56+ weightCounter += tree .getWeight ();
57+ }
58+ }
59+
60+ console .log (weightCounter );
61+ }
62+ ```
You can’t perform that action at this time.
0 commit comments