Compare commits
No commits in common. "eac54767b570e1577887cd95fa1127491061bf09" and "42f3bcc63e583ba1c28fc8ae9a1eea6965928965" have entirely different histories.
eac54767b5
...
42f3bcc63e
12 changed files with 43 additions and 116 deletions
29
2024/day1/part1.py
Normal file
29
2024/day1/part1.py
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
|
def get_ordered_lists(data_file: Path) -> tuple[list[int], list[int]]:
|
||||||
|
list1 = list()
|
||||||
|
list2 = list()
|
||||||
|
for line in data_file.open():
|
||||||
|
numbers = line.strip().split(" ")
|
||||||
|
list1.append(int(numbers[0]))
|
||||||
|
list2.append(int(numbers[1]))
|
||||||
|
return sorted(list1), sorted(list2)
|
||||||
|
|
||||||
|
|
||||||
|
def compute_distance(list1, list2) -> int:
|
||||||
|
return sum([abs(item2 - item1) for item1, item2 in zip(list1, list2)])
|
||||||
|
|
||||||
|
|
||||||
|
def compute(data_file: Path) -> int:
|
||||||
|
list1, list2 = get_ordered_lists(data_file)
|
||||||
|
return compute_distance(list1, list2)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
file = Path(__file__).parent / "input-data"
|
||||||
|
print(compute(file))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
|
@ -1,16 +0,0 @@
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
|
|
||||||
def get_lists(data_file: Path) -> tuple[list[int], list[int]]:
|
|
||||||
list1 = []
|
|
||||||
list2 = []
|
|
||||||
for line in data_file.open():
|
|
||||||
numbers = line.strip().split(" ")
|
|
||||||
list1.append(int(numbers[0]))
|
|
||||||
list2.append(int(numbers[1]))
|
|
||||||
return list1, list2
|
|
||||||
|
|
||||||
|
|
||||||
def get_ordered_lists(data_file: Path) -> tuple[list[int], list[int]]:
|
|
||||||
list1, list2 = get_lists(data_file)
|
|
||||||
return sorted(list1), sorted(list2)
|
|
|
@ -1,19 +0,0 @@
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
from aoc_2024.day1.common import get_ordered_lists
|
|
||||||
|
|
||||||
|
|
||||||
def compute_distance(list1, list2) -> int:
|
|
||||||
return sum([abs(item2 - item1) for item1, item2 in zip(list1, list2)])
|
|
||||||
|
|
||||||
|
|
||||||
def main(data_file: Path):
|
|
||||||
list1, list2 = get_ordered_lists(data_file)
|
|
||||||
distance = compute_distance(list1, list2)
|
|
||||||
print(distance)
|
|
||||||
return distance
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
file = Path(__file__).parent / "input-data"
|
|
||||||
main(file)
|
|
|
@ -1,38 +0,0 @@
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
from aoc_2024.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)
|
|
14
tests/2024/day1/test_first.py
Normal file
14
tests/2024/day1/test_first.py
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from day1 import part1
|
||||||
|
|
||||||
|
TEST_FILE = Path(__file__).parent / "test-data"
|
||||||
|
|
||||||
|
|
||||||
|
def test_first():
|
||||||
|
assert part1.compute(TEST_FILE) == 11
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_lists():
|
||||||
|
assert part1.get_ordered_lists(TEST_FILE)[0] == [1, 2, 3, 3, 3, 4]
|
||||||
|
assert part1.get_ordered_lists(TEST_FILE)[1] == [3, 3, 3, 4, 5, 9]
|
|
@ -1,15 +0,0 @@
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
from aoc_2024.day1 import common
|
|
||||||
|
|
||||||
TEST_FILE = Path(__file__).parent / "test-data"
|
|
||||||
|
|
||||||
|
|
||||||
def test_get_lists():
|
|
||||||
assert common.get_lists(TEST_FILE)[0] == [3, 4, 2, 1, 3, 3]
|
|
||||||
assert common.get_lists(TEST_FILE)[1] == [4, 3, 5, 3, 9, 3]
|
|
||||||
|
|
||||||
|
|
||||||
def test_get_sorted_lists():
|
|
||||||
assert common.get_ordered_lists(TEST_FILE)[0] == [1, 2, 3, 3, 3, 4]
|
|
||||||
assert common.get_ordered_lists(TEST_FILE)[1] == [3, 3, 3, 4, 5, 9]
|
|
|
@ -1,14 +0,0 @@
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
from aoc_2024.day1 import part1
|
|
||||||
|
|
||||||
|
|
||||||
def test_compute_distance():
|
|
||||||
list1 = [1, 2, 3, 3, 3, 4]
|
|
||||||
list2 = [3, 3, 3, 4, 5, 9]
|
|
||||||
assert part1.compute_distance(list1, list2) == 11
|
|
||||||
|
|
||||||
|
|
||||||
def test_main():
|
|
||||||
test_file = Path(__file__).parent / "test-data"
|
|
||||||
assert part1.main(test_file) == 11
|
|
|
@ -1,14 +0,0 @@
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
from aoc_2024.day1 import part2
|
|
||||||
|
|
||||||
|
|
||||||
def test_compute_similarity_score():
|
|
||||||
dict1 = {1: 1, 2: 1, 3: 3, 4: 1}
|
|
||||||
dict2 = {3: 3, 4: 1, 5: 1, 9: 1}
|
|
||||||
assert part2.compute_similarity_score(dict1, dict2) == 31
|
|
||||||
|
|
||||||
|
|
||||||
def test_main():
|
|
||||||
test_file = Path(__file__).parent / "test-data"
|
|
||||||
assert part2.main(test_file) == 31
|
|
Loading…
Add table
Add a link
Reference in a new issue