day 1 - part 1
This commit is contained in:
parent
bbd0702bf7
commit
4bdbcdbb56
4 changed files with 48 additions and 5 deletions
|
@ -997,5 +997,4 @@
|
|||
54344 21435
|
||||
17846 56504
|
||||
61807 42097
|
||||
93272 97487
|
||||
|
||||
93272 97487
|
|
@ -1,5 +1,29 @@
|
|||
def main():
|
||||
pass
|
||||
from pathlib import Path
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
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 / "first-input"
|
||||
print(compute(file))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
6
tests/2024/day1/first-data
Normal file
6
tests/2024/day1/first-data
Normal file
|
@ -0,0 +1,6 @@
|
|||
3 4
|
||||
4 3
|
||||
2 5
|
||||
1 3
|
||||
3 9
|
||||
3 3
|
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 first
|
||||
|
||||
TEST_FILE = Path(__file__).parent / "first-data"
|
||||
|
||||
|
||||
def test_first():
|
||||
assert first.compute(TEST_FILE) == 11
|
||||
|
||||
|
||||
def test_get_lists():
|
||||
assert first.get_ordered_lists(TEST_FILE)[0] == [1, 2, 3, 3, 3, 4]
|
||||
assert first.get_ordered_lists(TEST_FILE)[1] == [3, 3, 3, 4, 5, 9]
|
Loading…
Add table
Add a link
Reference in a new issue