Formatting
This commit is contained in:
parent
f308601bfc
commit
ee3a3f9233
1 changed files with 9 additions and 9 deletions
|
@ -4,39 +4,39 @@ from collections import defaultdict
|
|||
|
||||
def parse(instr: str) -> tuple[list[int], list[int]]:
|
||||
a, b = [], []
|
||||
|
||||
|
||||
for line in instr.splitlines():
|
||||
ai, bi = line.split(" ")
|
||||
a.append(int(ai))
|
||||
b.append(int(bi))
|
||||
|
||||
|
||||
return a, b
|
||||
|
||||
|
||||
def one(instr: str) -> int:
|
||||
a, b = parse(instr)
|
||||
|
||||
|
||||
a = sorted(a)
|
||||
b = sorted(b)
|
||||
|
||||
|
||||
acc = 0
|
||||
for (x, y) in zip(a, b):
|
||||
acc += abs(y - x)
|
||||
|
||||
|
||||
return acc
|
||||
|
||||
|
||||
def two(instr: str):
|
||||
a, b = parse(instr)
|
||||
|
||||
|
||||
counts = defaultdict(lambda: 0)
|
||||
for val in b:
|
||||
counts[val] = counts[val] + 1
|
||||
|
||||
|
||||
acc = 0
|
||||
for val in a:
|
||||
acc += counts[val] * val
|
||||
|
||||
|
||||
return acc
|
||||
|
||||
|
||||
|
@ -53,4 +53,4 @@ if __name__ == "__main__":
|
|||
if sys.argv[1] == "1":
|
||||
print(one(inp))
|
||||
else:
|
||||
print(two(inp))
|
||||
print(two(inp))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue