Skip to content

Commit

Permalink
2024 day 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Evgeni Gordeev committed Dec 1, 2024
1 parent 6fc9a37 commit 8e6e945
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions 2024/01.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ def read_input() -> str:
return data


# MAIN FUNCTIONS

# MAIN
def part1(data: List[str]) -> int:
"""
find distance between 2 sorted lists
"""
return sum(abs(a - b)
for a, b in zip( # zip 2 lists into a list of tuples and find distance between each item in 2 lists
*map(sorted, zip(*( # unzip a list of tuples into 2 lists and sort them
Expand All @@ -25,14 +27,16 @@ def part1(data: List[str]) -> int:


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


# TEST
def test():
# GIVEN
given = parser("""
3 4
4 3
Expand All @@ -54,7 +58,7 @@ def test():
part_1 = part1(lines)
print(part_1)
assert part_1 == 3_508_942
# # TWO #2
# TWO #2
part_2 = part2(lines)
print(part_2)
assert part_2 == 26_593_248
Expand Down

0 comments on commit 8e6e945

Please sign in to comment.