1
0
Fork 0
adventofcode/2024/01/main.py

52 lines
1.1 KiB
Python
Raw Permalink Normal View History

2024-12-06 12:06:10 +00:00
#!/usr/bin/env python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# Author: Sebastian Mark
# CC-BY-SA (https://creativecommons.org/licenses/by-sa/4.0/deed.de)
# pylint: disable=missing-module-docstring,missing-function-docstring,consider-using-f-string
def readinput() -> (list[int], list[int]):
a = []
b = []
with open("input", "r", encoding="utf-8") as file:
for line in file:
num1, num2 = map(int, line.split())
a.append(num1)
b.append(num2)
return a, b
def distance(list_a: list[int], list_b: list[int]) -> int:
list_a.sort()
list_b.sort()
dist = 0
for a_val, b_val in zip(list_a, list_b):
dist += abs(a_val - b_val)
return dist
def similarity(list_a: list[int], list_b: list[int]) -> int:
sim = 0
for a_val in list_a:
sim += a_val * list_b.count(a_val)
return sim
def main():
list_a, list_b = readinput()
# part1
print("Distance: %d" % distance(list_a, list_b))
# part 2
print("Similarity: %d" % similarity(list_a, list_b))
if __name__ == "__main__":
main()