File tree Expand file tree Collapse file tree 3 files changed +43
-0
lines changed
Expand file tree Collapse file tree 3 files changed +43
-0
lines changed Original file line number Diff line number Diff line change 1+ * /input
2+ * /test-input
Original file line number Diff line number Diff line change 1+ with open ('input' , 'r' ) as f :
2+ lines = f .readlines ()
3+
4+ left , right = [], []
5+
6+ for line in lines :
7+ l , r = line .split ()
8+ left .append (int (l ))
9+ right .append (int (r ))
10+
11+ left .sort ()
12+ right .sort ()
13+
14+ if len (left ) != len (right ): print ('fail' )
15+ else :
16+ diff = 0
17+ for i in range (len (left )): diff += abs (left [i ] - right [i ])
18+ print (diff )
Original file line number Diff line number Diff line change 1+ with open ('input' , 'r' ) as f :
2+ lines = f .readlines ()
3+
4+ left , right = [], []
5+
6+ for line in lines :
7+ l , r = line .split ()
8+ left .append (int (l ))
9+ right .append (int (r ))
10+
11+ right2 = {}
12+ for r_item in right :
13+ if r_item in right2 .keys (): right2 [r_item ] += 1
14+ else : right2 [r_item ] = 1
15+
16+ print (right2 )
17+
18+ similarity = 0
19+ for l_item in left :
20+ if l_item in right2 .keys ():
21+ similarity += (l_item * right2 [l_item ])
22+
23+ print (similarity )
You can’t perform that action at this time.
0 commit comments