first 3 days
This commit is contained in:
13
1/1.py
Normal file
13
1/1.py
Normal file
@@ -0,0 +1,13 @@
|
||||
def distances(left: list[int], right: list[int]) -> list[int]:
|
||||
left, right = sorted(left), sorted(right)
|
||||
return [abs(l-r) for l, r in zip(left, right)]
|
||||
|
||||
if __name__ == '__main__':
|
||||
with open('1.input') as file:
|
||||
left, right = [], []
|
||||
for line in file:
|
||||
l, r = line.split()
|
||||
l, r = int(l), int(r)
|
||||
left.append(l)
|
||||
right.append(r)
|
||||
print(sum(distances(left, right)))
|
||||
28
1/2.py
Normal file
28
1/2.py
Normal file
@@ -0,0 +1,28 @@
|
||||
def counts(nums: list[int]) -> dict[int, int]:
|
||||
ret = {}
|
||||
for n in nums:
|
||||
if n in ret:
|
||||
ret[n] += 1
|
||||
else:
|
||||
ret[n] = 1
|
||||
return ret
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
with open('1.input') as file:
|
||||
left, right = [], []
|
||||
for line in file:
|
||||
l, r = line.split()
|
||||
l, r = int(l), int(r)
|
||||
left.append(l)
|
||||
right.append(r)
|
||||
# how many times does each number apear in each list
|
||||
right = counts(right)
|
||||
score = 0
|
||||
for n in left:
|
||||
if n not in right: continue
|
||||
score += n * right[n]
|
||||
print(score)
|
||||
|
||||
|
||||
|
||||
6
1/sample.input
Normal file
6
1/sample.input
Normal file
@@ -0,0 +1,6 @@
|
||||
3 4
|
||||
4 3
|
||||
2 5
|
||||
1 3
|
||||
3 9
|
||||
3 3
|
||||
Reference in New Issue
Block a user