advent_of_code_2024_day_1
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
with open(r'2024/01/input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
test_input = '''3 4
|
||||
4 3
|
||||
2 5
|
||||
1 3
|
||||
3 9
|
||||
3 3'''
|
||||
|
||||
|
||||
# first split the input into two lists, left and right
|
||||
left_list = []
|
||||
right_list = []
|
||||
|
||||
for line in input.split('\n'):
|
||||
left, right = line.split(' ')
|
||||
left_list.append(int(left))
|
||||
right_list.append(int(right))
|
||||
|
||||
# now sort each list acecnding
|
||||
left_list.sort()
|
||||
right_list.sort()
|
||||
# print('left ',left_list)
|
||||
# print('right',right_list)
|
||||
|
||||
# now compare each element in the left list with the corresponding element in the right list
|
||||
# we need to get the difference between the two elements
|
||||
diff_list = []
|
||||
for i in range(len(left_list)):
|
||||
diff_list.append(abs(left_list[i] - right_list[i]))
|
||||
|
||||
# print(diff_list)
|
||||
|
||||
# now we sum up all the items in the list
|
||||
|
||||
print(sum(diff_list))
|
||||
@@ -0,0 +1,30 @@
|
||||
with open(r'2024/01/input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
test_input = '''3 4
|
||||
4 3
|
||||
2 5
|
||||
1 3
|
||||
3 9
|
||||
3 3'''
|
||||
|
||||
|
||||
# first split the input into two lists, left and right
|
||||
left_list = []
|
||||
right_list = []
|
||||
|
||||
for line in input.split('\n'):
|
||||
left, right = line.split(' ')
|
||||
left_list.append(int(left))
|
||||
right_list.append(int(right))
|
||||
|
||||
# for every item in the left list we need to count how many times it appears in the right list
|
||||
|
||||
score = []
|
||||
for i in left_list:
|
||||
if i in right_list:
|
||||
score.append(i * right_list.count(i))
|
||||
|
||||
|
||||
#print(score)
|
||||
print(sum(score))
|
||||
Reference in New Issue
Block a user