38 lines
963 B
Python
38 lines
963 B
Python
from pathlib import Path
|
|
|
|
from day1.common import get_ordered_lists
|
|
|
|
|
|
def get_elem_amount(data: list[int]) -> dict[int, int]:
|
|
data_set = set(data)
|
|
amount = {}
|
|
for element in data_set:
|
|
nb_elem = data.count(element)
|
|
data.remove(element)
|
|
amount[element] = nb_elem
|
|
return amount
|
|
|
|
|
|
def compute_similarity_score(
|
|
elem_amount1: dict[int, int], elem_amount2: dict[int, int]
|
|
) -> int:
|
|
score = 0
|
|
for k1, v1 in elem_amount1.items():
|
|
if k1 in elem_amount2.keys():
|
|
score += k1 * v1 * elem_amount2[k1]
|
|
return score
|
|
|
|
|
|
def main(data_file: Path):
|
|
list1, list2 = get_ordered_lists(data_file)
|
|
elem_amount1 = get_elem_amount(list1)
|
|
elem_amount2 = get_elem_amount(list2)
|
|
|
|
similarity_score = compute_similarity_score(elem_amount1, elem_amount2)
|
|
print(similarity_score)
|
|
return similarity_score
|
|
|
|
|
|
if __name__ == "__main__":
|
|
file = Path(__file__).parent / "input-data"
|
|
main(file)
|