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]]:
|
def parse(instr: str) -> tuple[list[int], list[int]]:
|
||||||
a, b = [], []
|
a, b = [], []
|
||||||
|
|
||||||
for line in instr.splitlines():
|
for line in instr.splitlines():
|
||||||
ai, bi = line.split(" ")
|
ai, bi = line.split(" ")
|
||||||
a.append(int(ai))
|
a.append(int(ai))
|
||||||
b.append(int(bi))
|
b.append(int(bi))
|
||||||
|
|
||||||
return a, b
|
return a, b
|
||||||
|
|
||||||
|
|
||||||
def one(instr: str) -> int:
|
def one(instr: str) -> int:
|
||||||
a, b = parse(instr)
|
a, b = parse(instr)
|
||||||
|
|
||||||
a = sorted(a)
|
a = sorted(a)
|
||||||
b = sorted(b)
|
b = sorted(b)
|
||||||
|
|
||||||
acc = 0
|
acc = 0
|
||||||
for (x, y) in zip(a, b):
|
for (x, y) in zip(a, b):
|
||||||
acc += abs(y - x)
|
acc += abs(y - x)
|
||||||
|
|
||||||
return acc
|
return acc
|
||||||
|
|
||||||
|
|
||||||
def two(instr: str):
|
def two(instr: str):
|
||||||
a, b = parse(instr)
|
a, b = parse(instr)
|
||||||
|
|
||||||
counts = defaultdict(lambda: 0)
|
counts = defaultdict(lambda: 0)
|
||||||
for val in b:
|
for val in b:
|
||||||
counts[val] = counts[val] + 1
|
counts[val] = counts[val] + 1
|
||||||
|
|
||||||
acc = 0
|
acc = 0
|
||||||
for val in a:
|
for val in a:
|
||||||
acc += counts[val] * val
|
acc += counts[val] * val
|
||||||
|
|
||||||
return acc
|
return acc
|
||||||
|
|
||||||
|
|
||||||
|
@ -53,4 +53,4 @@ if __name__ == "__main__":
|
||||||
if sys.argv[1] == "1":
|
if sys.argv[1] == "1":
|
||||||
print(one(inp))
|
print(one(inp))
|
||||||
else:
|
else:
|
||||||
print(two(inp))
|
print(two(inp))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue