Skip to content

Commit 8e6e945

Browse files
author
Evgeni Gordeev
committed
2024 day 1
1 parent 6fc9a37 commit 8e6e945

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

2024/01.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ def read_input() -> str:
1414
return data
1515

1616

17-
# MAIN FUNCTIONS
18-
17+
# MAIN
1918
def part1(data: List[str]) -> int:
19+
"""
20+
find distance between 2 sorted lists
21+
"""
2022
return sum(abs(a - b)
2123
for a, b in zip( # zip 2 lists into a list of tuples and find distance between each item in 2 lists
2224
*map(sorted, zip(*( # unzip a list of tuples into 2 lists and sort them
@@ -25,14 +27,16 @@ def part1(data: List[str]) -> int:
2527

2628

2729
def part2(data: List[str]) -> int:
30+
"""
31+
find similarity score based on how many times an item from left list appears in the right one
32+
"""
2833
loc1, loc2 = zip(*(tuple(map(int, item.split())) for item in data))
2934
counter = Counter(loc2)
3035
return sum(i * counter.get(i, 0) for i in loc1)
3136

3237

3338
# TEST
3439
def test():
35-
# GIVEN
3640
given = parser("""
3741
3 4
3842
4 3
@@ -54,7 +58,7 @@ def test():
5458
part_1 = part1(lines)
5559
print(part_1)
5660
assert part_1 == 3_508_942
57-
# # TWO #2
61+
# TWO #2
5862
part_2 = part2(lines)
5963
print(part_2)
6064
assert part_2 == 26_593_248

0 commit comments

Comments
 (0)