-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart_2.py
36 lines (29 loc) · 1.04 KB
/
part_2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
### Function to parse the input file into two lists
def parseInput():
file_path = "D:/Projects/advent-of-code/2024/Go/Day1/input.txt"
data = []
with open(file_path, encoding="UTF-8") as file:
for line in file:
for n in line.split():
data.append(int(n))
left = []
right = []
# Distribute elements into left and right lists
for index, value in enumerate(data):
if index % 2 == 0: # Even indices go to 'left'
left.append(value)
else: # Odd indices go to 'right'
right.append(value)
return left, right
# Function to calculate the weighted sum based on counts (Part 2)
def similarityScore():
# Sort both lists (even if already sorted in Part 1, explicitly do it here)
left, right = parseInput()
left.sort()
right.sort()
# Calculate the weighted sum
weighted_sum = 0
for l_val in left:
count = right.count(l_val) # Count occurrences of `l_val` in `right`
weighted_sum += l_val * count
return weighted_sum