File tree Expand file tree Collapse file tree 2 files changed +1025
-0
lines changed
clients/typescript/solutions Expand file tree Collapse file tree 2 files changed +1025
-0
lines changed Original file line number Diff line number Diff line change
1
+ import ISolution from "./ISolution.ts" ;
2
+
3
+ export default class S2401 implements ISolution {
4
+ firstPart ( input : string ) : ( number | string ) | Promise < number | string > {
5
+ const lists = input . split ( "\n" ) . map ( x => x . split ( " " ) ) . filter ( x => x . length === 2 ) ;
6
+ const list1 = lists . map ( x => Number ( x [ 0 ] ) ) . sort ( ) ;
7
+ const list2 = lists . map ( x => Number ( x [ 1 ] ) ) . sort ( ) ;
8
+ let sum = 0 ;
9
+ for ( let i = 0 ; i < list1 . length ; i ++ ) {
10
+ sum += Math . abs ( list1 [ i ] - list2 [ i ] ) ;
11
+ }
12
+ return sum ;
13
+ }
14
+ secondPart ( input : string ) : ( number | string ) | Promise < number | string > {
15
+ const lists = input . split ( "\n" ) . map ( x => x . split ( " " ) ) . filter ( x => x . length === 2 ) ;
16
+ const list1 = lists . map ( x => Number ( x [ 0 ] ) ) ;
17
+ const list2 = lists . map ( x => Number ( x [ 1 ] ) ) ;
18
+ let sum = 0 ;
19
+ for ( const number of list1 ) {
20
+ sum += number * list2 . filter ( x => x === number ) . length ;
21
+ }
22
+ return sum ;
23
+ }
24
+
25
+ }
You can’t perform that action at this time.
0 commit comments