year,day,part,question,answer,solution,language | |
2024,1,1,"--- Day 1: Historian Hysteria --- | |
The Chief Historian is always present for the big Christmas sleigh launch, but nobody has seen him in months! Last anyone heard, he was visiting locations that are historically significant to the North Pole; a group of Senior Historians has asked you to accompany them as they check the places they think he was most likely to visit. | |
As each location is checked, they will mark it on their list with a star. They figure the Chief Historian must be in one of the first fifty places they'll look, so in order to save Christmas, you need to help them get fifty stars on their list before Santa takes off on December 25th. | |
Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck! | |
You haven't even left yet and the group of Elvish Senior Historians has already hit a problem: their list of locations to check is currently empty. Eventually, someone decides that the best place to check first would be the Chief Historian's office. | |
Upon pouring into the office, everyone confirms that the Chief Historian is indeed nowhere to be found. Instead, the Elves discover an assortment of notes and lists of historically significant locations! This seems to be the planning the Chief Historian was doing before he left. Perhaps these notes can be used to determine which locations to search? | |
Throughout the Chief's office, the historically significant locations are listed not by name but by a unique number called the location ID. To make sure they don't miss anything, The Historians split into two groups, each searching the office and trying to create their own complete list of location IDs. | |
There's just one problem: by holding the two lists up side by side (your puzzle input), it quickly becomes clear that the lists aren't very similar. Maybe you can help The Historians reconcile their lists? | |
For example: | |
3 4 | |
4 3 | |
2 5 | |
1 3 | |
3 9 | |
3 3 | |
Maybe the lists are only off by a small amount! To find out, pair up the numbers and measure how far apart they are. Pair up the smallest number in the left list with the smallest number in the right list, then the second-smallest left number with the second-smallest right number, and so on. | |
Within each pair, figure out how far apart the two numbers are; you'll need to add up all of those distances. For example, if you pair up a 3 from the left list with a 7 from the right list, the distance apart is 4; if you pair up a 9 with a 3, the distance apart is 6. | |
In the example list above, the pairs and distances would be as follows: | |
The smallest number in the left list is 1, and the smallest number in the right list is 3. The distance between them is 2. | |
The second-smallest number in the left list is 2, and the second-smallest number in the right list is another 3. The distance between them is 1. | |
The third-smallest number in both lists is 3, so the distance between them is 0. | |
The next numbers to pair up are 3 and 4, a distance of 1. | |
The fifth-smallest numbers in each list are 3 and 5, a distance of 2. | |
Finally, the largest number in the left list is 4, while the largest number in the right list is 9; these are a distance 5 apart. | |
To find the total distance between the left list and the right list, add up the distances between all of the pairs you found. In the example above, this is 2 + 1 + 0 + 1 + 2 + 5, a total distance of 11! | |
Your actual left and right lists contain many location IDs. What is the total distance between your lists?",2378066,"def get_diff_sum(list1, list2): | |
list1.sort() | |
list2.sort() | |
return sum([abs(list1[i] - list2[i]) for i in range(len(list1))]) | |
if __name__ == ""__main__"": | |
list1 = [] | |
list2 = [] | |
# Open file 'day1-1.txt' in read mode | |
with open('day1-1.txt', 'r') as f: | |
# Read each line of the file | |
for line in f: | |
nums = line.split() | |
list1.append(int(nums[0])) | |
list2.append(int(nums[1])) | |
result = get_diff_sum(list1, list2) | |
print(""Total Distance between lists: "" + str(result))",python:3.9 | |
2024,1,1,"--- Day 1: Historian Hysteria --- | |
The Chief Historian is always present for the big Christmas sleigh launch, but nobody has seen him in months! Last anyone heard, he was visiting locations that are historically significant to the North Pole; a group of Senior Historians has asked you to accompany them as they check the places they think he was most likely to visit. | |
As each location is checked, they will mark it on their list with a star. They figure the Chief Historian must be in one of the first fifty places they'll look, so in order to save Christmas, you need to help them get fifty stars on their list before Santa takes off on December 25th. | |
Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck! | |
You haven't even left yet and the group of Elvish Senior Historians has already hit a problem: their list of locations to check is currently empty. Eventually, someone decides that the best place to check first would be the Chief Historian's office. | |
Upon pouring into the office, everyone confirms that the Chief Historian is indeed nowhere to be found. Instead, the Elves discover an assortment of notes and lists of historically significant locations! This seems to be the planning the Chief Historian was doing before he left. Perhaps these notes can be used to determine which locations to search? | |
Throughout the Chief's office, the historically significant locations are listed not by name but by a unique number called the location ID. To make sure they don't miss anything, The Historians split into two groups, each searching the office and trying to create their own complete list of location IDs. | |
There's just one problem: by holding the two lists up side by side (your puzzle input), it quickly becomes clear that the lists aren't very similar. Maybe you can help The Historians reconcile their lists? | |
For example: | |
3 4 | |
4 3 | |
2 5 | |
1 3 | |
3 9 | |
3 3 | |
Maybe the lists are only off by a small amount! To find out, pair up the numbers and measure how far apart they are. Pair up the smallest number in the left list with the smallest number in the right list, then the second-smallest left number with the second-smallest right number, and so on. | |
Within each pair, figure out how far apart the two numbers are; you'll need to add up all of those distances. For example, if you pair up a 3 from the left list with a 7 from the right list, the distance apart is 4; if you pair up a 9 with a 3, the distance apart is 6. | |
In the example list above, the pairs and distances would be as follows: | |
The smallest number in the left list is 1, and the smallest number in the right list is 3. The distance between them is 2. | |
The second-smallest number in the left list is 2, and the second-smallest number in the right list is another 3. The distance between them is 1. | |
The third-smallest number in both lists is 3, so the distance between them is 0. | |
The next numbers to pair up are 3 and 4, a distance of 1. | |
The fifth-smallest numbers in each list are 3 and 5, a distance of 2. | |
Finally, the largest number in the left list is 4, while the largest number in the right list is 9; these are a distance 5 apart. | |
To find the total distance between the left list and the right list, add up the distances between all of the pairs you found. In the example above, this is 2 + 1 + 0 + 1 + 2 + 5, a total distance of 11! | |
Your actual left and right lists contain many location IDs. What is the total distance between your lists?",2378066,"with open('input.txt') as f: | |
lines = f.read().splitlines() | |
nums = [[int(v) for v in line.split()] for line in lines] | |
left, right = map(sorted, map(list, zip(*nums))) | |
distance = sum([abs(left[i] - right[i]) for i in range(len(left))]) | |
print(distance)",python:3.9 | |
2024,1,1,"--- Day 1: Historian Hysteria --- | |
The Chief Historian is always present for the big Christmas sleigh launch, but nobody has seen him in months! Last anyone heard, he was visiting locations that are historically significant to the North Pole; a group of Senior Historians has asked you to accompany them as they check the places they think he was most likely to visit. | |
As each location is checked, they will mark it on their list with a star. They figure the Chief Historian must be in one of the first fifty places they'll look, so in order to save Christmas, you need to help them get fifty stars on their list before Santa takes off on December 25th. | |
Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck! | |
You haven't even left yet and the group of Elvish Senior Historians has already hit a problem: their list of locations to check is currently empty. Eventually, someone decides that the best place to check first would be the Chief Historian's office. | |
Upon pouring into the office, everyone confirms that the Chief Historian is indeed nowhere to be found. Instead, the Elves discover an assortment of notes and lists of historically significant locations! This seems to be the planning the Chief Historian was doing before he left. Perhaps these notes can be used to determine which locations to search? | |
Throughout the Chief's office, the historically significant locations are listed not by name but by a unique number called the location ID. To make sure they don't miss anything, The Historians split into two groups, each searching the office and trying to create their own complete list of location IDs. | |
There's just one problem: by holding the two lists up side by side (your puzzle input), it quickly becomes clear that the lists aren't very similar. Maybe you can help The Historians reconcile their lists? | |
For example: | |
3 4 | |
4 3 | |
2 5 | |
1 3 | |
3 9 | |
3 3 | |
Maybe the lists are only off by a small amount! To find out, pair up the numbers and measure how far apart they are. Pair up the smallest number in the left list with the smallest number in the right list, then the second-smallest left number with the second-smallest right number, and so on. | |
Within each pair, figure out how far apart the two numbers are; you'll need to add up all of those distances. For example, if you pair up a 3 from the left list with a 7 from the right list, the distance apart is 4; if you pair up a 9 with a 3, the distance apart is 6. | |
In the example list above, the pairs and distances would be as follows: | |
The smallest number in the left list is 1, and the smallest number in the right list is 3. The distance between them is 2. | |
The second-smallest number in the left list is 2, and the second-smallest number in the right list is another 3. The distance between them is 1. | |
The third-smallest number in both lists is 3, so the distance between them is 0. | |
The next numbers to pair up are 3 and 4, a distance of 1. | |
The fifth-smallest numbers in each list are 3 and 5, a distance of 2. | |
Finally, the largest number in the left list is 4, while the largest number in the right list is 9; these are a distance 5 apart. | |
To find the total distance between the left list and the right list, add up the distances between all of the pairs you found. In the example above, this is 2 + 1 + 0 + 1 + 2 + 5, a total distance of 11! | |
Your actual left and right lists contain many location IDs. What is the total distance between your lists?",2378066,"class Solution: | |
def add_input(self, col1: list[int], col2: list[int]) -> int: | |
i = 0 | |
with open(""day1Input.txt"", 'r') as input: | |
for line in input: | |
line_split = line.split() | |
if len(line_split) == 2: | |
col1.append(int(line_split[0])) | |
col2.append(int(line_split[1])) | |
else: | |
print(f""Skipping invalid line: {line.strip()}"") | |
total_sum = 0 | |
while col1 and col2: | |
min1 = min(col1) | |
min2 = min(col2) | |
diff = abs(min2 - min1) | |
total_sum += diff | |
col1.remove(min1) | |
col2.remove(min2) | |
return total_sum | |
# Create an instance of the Solution class | |
solution = Solution() | |
# Initialize the two lists where input data will be stored | |
col1 = [] | |
col2 = [] | |
# Print the results | |
print(""Final sum:"", solution.add_input(col1, col2)) | |
",python:3.9 | |
2024,1,1,"--- Day 1: Historian Hysteria --- | |
The Chief Historian is always present for the big Christmas sleigh launch, but nobody has seen him in months! Last anyone heard, he was visiting locations that are historically significant to the North Pole; a group of Senior Historians has asked you to accompany them as they check the places they think he was most likely to visit. | |
As each location is checked, they will mark it on their list with a star. They figure the Chief Historian must be in one of the first fifty places they'll look, so in order to save Christmas, you need to help them get fifty stars on their list before Santa takes off on December 25th. | |
Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck! | |
You haven't even left yet and the group of Elvish Senior Historians has already hit a problem: their list of locations to check is currently empty. Eventually, someone decides that the best place to check first would be the Chief Historian's office. | |
Upon pouring into the office, everyone confirms that the Chief Historian is indeed nowhere to be found. Instead, the Elves discover an assortment of notes and lists of historically significant locations! This seems to be the planning the Chief Historian was doing before he left. Perhaps these notes can be used to determine which locations to search? | |
Throughout the Chief's office, the historically significant locations are listed not by name but by a unique number called the location ID. To make sure they don't miss anything, The Historians split into two groups, each searching the office and trying to create their own complete list of location IDs. | |
There's just one problem: by holding the two lists up side by side (your puzzle input), it quickly becomes clear that the lists aren't very similar. Maybe you can help The Historians reconcile their lists? | |
For example: | |
3 4 | |
4 3 | |
2 5 | |
1 3 | |
3 9 | |
3 3 | |
Maybe the lists are only off by a small amount! To find out, pair up the numbers and measure how far apart they are. Pair up the smallest number in the left list with the smallest number in the right list, then the second-smallest left number with the second-smallest right number, and so on. | |
Within each pair, figure out how far apart the two numbers are; you'll need to add up all of those distances. For example, if you pair up a 3 from the left list with a 7 from the right list, the distance apart is 4; if you pair up a 9 with a 3, the distance apart is 6. | |
In the example list above, the pairs and distances would be as follows: | |
The smallest number in the left list is 1, and the smallest number in the right list is 3. The distance between them is 2. | |
The second-smallest number in the left list is 2, and the second-smallest number in the right list is another 3. The distance between them is 1. | |
The third-smallest number in both lists is 3, so the distance between them is 0. | |
The next numbers to pair up are 3 and 4, a distance of 1. | |
The fifth-smallest numbers in each list are 3 and 5, a distance of 2. | |
Finally, the largest number in the left list is 4, while the largest number in the right list is 9; these are a distance 5 apart. | |
To find the total distance between the left list and the right list, add up the distances between all of the pairs you found. In the example above, this is 2 + 1 + 0 + 1 + 2 + 5, a total distance of 11! | |
Your actual left and right lists contain many location IDs. What is the total distance between your lists?",2378066,"def parseInput(): | |
left = [] | |
right = [] | |
with open('input.txt', 'r') as file: | |
input = file.read().splitlines() | |
for line in input: | |
split = line.split("" "") | |
left.append(int(split[0])) | |
right.append(int(split[1])) | |
return left, right | |
def sort(arr: list): | |
for i in range(len(arr)): | |
for j in range(i, len(arr)): | |
if arr[j] < arr[i]: | |
arr[i], arr[j] = arr[j], arr[i] | |
return arr | |
if __name__ == ""__main__"": | |
left, right = parseInput() | |
left = sort(left) | |
right = sort(right) | |
# print(left) | |
# print(right) | |
sumDist = 0 | |
for i in range(len(left)): | |
sumDist += left[i] - right[i] if left[i] > right[i] else right[i] - left[i] | |
print(sumDist) | |
",python:3.9 | |
2024,1,1,"--- Day 1: Historian Hysteria --- | |
The Chief Historian is always present for the big Christmas sleigh launch, but nobody has seen him in months! Last anyone heard, he was visiting locations that are historically significant to the North Pole; a group of Senior Historians has asked you to accompany them as they check the places they think he was most likely to visit. | |
As each location is checked, they will mark it on their list with a star. They figure the Chief Historian must be in one of the first fifty places they'll look, so in order to save Christmas, you need to help them get fifty stars on their list before Santa takes off on December 25th. | |
Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck! | |
You haven't even left yet and the group of Elvish Senior Historians has already hit a problem: their list of locations to check is currently empty. Eventually, someone decides that the best place to check first would be the Chief Historian's office. | |
Upon pouring into the office, everyone confirms that the Chief Historian is indeed nowhere to be found. Instead, the Elves discover an assortment of notes and lists of historically significant locations! This seems to be the planning the Chief Historian was doing before he left. Perhaps these notes can be used to determine which locations to search? | |
Throughout the Chief's office, the historically significant locations are listed not by name but by a unique number called the location ID. To make sure they don't miss anything, The Historians split into two groups, each searching the office and trying to create their own complete list of location IDs. | |
There's just one problem: by holding the two lists up side by side (your puzzle input), it quickly becomes clear that the lists aren't very similar. Maybe you can help The Historians reconcile their lists? | |
For example: | |
3 4 | |
4 3 | |
2 5 | |
1 3 | |
3 9 | |
3 3 | |
Maybe the lists are only off by a small amount! To find out, pair up the numbers and measure how far apart they are. Pair up the smallest number in the left list with the smallest number in the right list, then the second-smallest left number with the second-smallest right number, and so on. | |
Within each pair, figure out how far apart the two numbers are; you'll need to add up all of those distances. For example, if you pair up a 3 from the left list with a 7 from the right list, the distance apart is 4; if you pair up a 9 with a 3, the distance apart is 6. | |
In the example list above, the pairs and distances would be as follows: | |
The smallest number in the left list is 1, and the smallest number in the right list is 3. The distance between them is 2. | |
The second-smallest number in the left list is 2, and the second-smallest number in the right list is another 3. The distance between them is 1. | |
The third-smallest number in both lists is 3, so the distance between them is 0. | |
The next numbers to pair up are 3 and 4, a distance of 1. | |
The fifth-smallest numbers in each list are 3 and 5, a distance of 2. | |
Finally, the largest number in the left list is 4, while the largest number in the right list is 9; these are a distance 5 apart. | |
To find the total distance between the left list and the right list, add up the distances between all of the pairs you found. In the example above, this is 2 + 1 + 0 + 1 + 2 + 5, a total distance of 11! | |
Your actual left and right lists contain many location IDs. What is the total distance between your lists?",2378066,"with open('input_1.txt', 'r') as file: | |
lines = file.readlines() | |
col1nums = [] | |
col2nums = [] | |
for line in lines: | |
num1, num2 = map(int, line.split(' ')) | |
col1nums.append(num1) | |
col2nums.append(num2) | |
col1nums.sort() | |
col2nums.sort() | |
sum = 0 | |
for num1, num2 in zip(col1nums, col2nums): | |
sum += abs(num1 - num2) | |
print(sum) | |
# output: 2066446",python:3.9 | |
2024,1,2,"--- Day 1: Historian Hysteria --- | |
The Chief Historian is always present for the big Christmas sleigh launch, but nobody has seen him in months! Last anyone heard, he was visiting locations that are historically significant to the North Pole; a group of Senior Historians has asked you to accompany them as they check the places they think he was most likely to visit. | |
As each location is checked, they will mark it on their list with a star. They figure the Chief Historian must be in one of the first fifty places they'll look, so in order to save Christmas, you need to help them get fifty stars on their list before Santa takes off on December 25th. | |
Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck! | |
You haven't even left yet and the group of Elvish Senior Historians has already hit a problem: their list of locations to check is currently empty. Eventually, someone decides that the best place to check first would be the Chief Historian's office. | |
Upon pouring into the office, everyone confirms that the Chief Historian is indeed nowhere to be found. Instead, the Elves discover an assortment of notes and lists of historically significant locations! This seems to be the planning the Chief Historian was doing before he left. Perhaps these notes can be used to determine which locations to search? | |
Throughout the Chief's office, the historically significant locations are listed not by name but by a unique number called the location ID. To make sure they don't miss anything, The Historians split into two groups, each searching the office and trying to create their own complete list of location IDs. | |
There's just one problem: by holding the two lists up side by side (your puzzle input), it quickly becomes clear that the lists aren't very similar. Maybe you can help The Historians reconcile their lists? | |
For example: | |
3 4 | |
4 3 | |
2 5 | |
1 3 | |
3 9 | |
3 3 | |
Maybe the lists are only off by a small amount! To find out, pair up the numbers and measure how far apart they are. Pair up the smallest number in the left list with the smallest number in the right list, then the second-smallest left number with the second-smallest right number, and so on. | |
Within each pair, figure out how far apart the two numbers are; you'll need to add up all of those distances. For example, if you pair up a 3 from the left list with a 7 from the right list, the distance apart is 4; if you pair up a 9 with a 3, the distance apart is 6. | |
In the example list above, the pairs and distances would be as follows: | |
The smallest number in the left list is 1, and the smallest number in the right list is 3. The distance between them is 2. | |
The second-smallest number in the left list is 2, and the second-smallest number in the right list is another 3. The distance between them is 1. | |
The third-smallest number in both lists is 3, so the distance between them is 0. | |
The next numbers to pair up are 3 and 4, a distance of 1. | |
The fifth-smallest numbers in each list are 3 and 5, a distance of 2. | |
Finally, the largest number in the left list is 4, while the largest number in the right list is 9; these are a distance 5 apart. | |
To find the total distance between the left list and the right list, add up the distances between all of the pairs you found. In the example above, this is 2 + 1 + 0 + 1 + 2 + 5, a total distance of 11! | |
Your actual left and right lists contain many location IDs. What is the total distance between your lists? | |
Your puzzle answer was 2378066. | |
--- Part Two --- | |
Your analysis only confirmed what everyone feared: the two lists of location IDs are indeed very different. | |
Or are they? | |
The Historians can't agree on which group made the mistakes or how to read most of the Chief's handwriting, but in the commotion you notice an interesting detail: a lot of location IDs appear in both lists! Maybe the other numbers aren't location IDs at all but rather misinterpreted handwriting. | |
This time, you'll need to figure out exactly how often each number from the left list appears in the right list. Calculate a total similarity score by adding up each number in the left list after multiplying it by the number of times that number appears in the right list. | |
Here are the same example lists again: | |
3 4 | |
4 3 | |
2 5 | |
1 3 | |
3 9 | |
3 3 | |
For these example lists, here is the process of finding the similarity score: | |
The first number in the left list is 3. It appears in the right list three times, so the similarity score increases by 3 * 3 = 9. | |
The second number in the left list is 4. It appears in the right list once, so the similarity score increases by 4 * 1 = 4. | |
The third number in the left list is 2. It does not appear in the right list, so the similarity score does not increase (2 * 0 = 0). | |
The fourth number, 1, also does not appear in the right list. | |
The fifth number, 3, appears in the right list three times; the similarity score increases by 9. | |
The last number, 3, appears in the right list three times; the similarity score again increases by 9. | |
So, for these example lists, the similarity score at the end of this process is 31 (9 + 4 + 0 + 0 + 9 + 9). | |
Once again consider your left and right lists. What is their similarity score?",18934359,"from collections import Counter | |
array_1 = [] | |
array_2 = [] | |
input_file = 'input.txt' | |
with open(input_file, 'r') as file: | |
for line in file: | |
parts = line.strip().split(' ') | |
if len(parts) == 2: | |
array_1.append(int(parts[0])) | |
array_2.append(int(parts[1])) | |
array_2_count = Counter(array_2) | |
sum = 0 | |
for num in array_1: | |
sum += num * array_2_count[num] | |
print(sum)",python:3.9 | |
2024,1,2,"--- Day 1: Historian Hysteria --- | |
The Chief Historian is always present for the big Christmas sleigh launch, but nobody has seen him in months! Last anyone heard, he was visiting locations that are historically significant to the North Pole; a group of Senior Historians has asked you to accompany them as they check the places they think he was most likely to visit. | |
As each location is checked, they will mark it on their list with a star. They figure the Chief Historian must be in one of the first fifty places they'll look, so in order to save Christmas, you need to help them get fifty stars on their list before Santa takes off on December 25th. | |
Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck! | |
You haven't even left yet and the group of Elvish Senior Historians has already hit a problem: their list of locations to check is currently empty. Eventually, someone decides that the best place to check first would be the Chief Historian's office. | |
Upon pouring into the office, everyone confirms that the Chief Historian is indeed nowhere to be found. Instead, the Elves discover an assortment of notes and lists of historically significant locations! This seems to be the planning the Chief Historian was doing before he left. Perhaps these notes can be used to determine which locations to search? | |
Throughout the Chief's office, the historically significant locations are listed not by name but by a unique number called the location ID. To make sure they don't miss anything, The Historians split into two groups, each searching the office and trying to create their own complete list of location IDs. | |
There's just one problem: by holding the two lists up side by side (your puzzle input), it quickly becomes clear that the lists aren't very similar. Maybe you can help The Historians reconcile their lists? | |
For example: | |
3 4 | |
4 3 | |
2 5 | |
1 3 | |
3 9 | |
3 3 | |
Maybe the lists are only off by a small amount! To find out, pair up the numbers and measure how far apart they are. Pair up the smallest number in the left list with the smallest number in the right list, then the second-smallest left number with the second-smallest right number, and so on. | |
Within each pair, figure out how far apart the two numbers are; you'll need to add up all of those distances. For example, if you pair up a 3 from the left list with a 7 from the right list, the distance apart is 4; if you pair up a 9 with a 3, the distance apart is 6. | |
In the example list above, the pairs and distances would be as follows: | |
The smallest number in the left list is 1, and the smallest number in the right list is 3. The distance between them is 2. | |
The second-smallest number in the left list is 2, and the second-smallest number in the right list is another 3. The distance between them is 1. | |
The third-smallest number in both lists is 3, so the distance between them is 0. | |
The next numbers to pair up are 3 and 4, a distance of 1. | |
The fifth-smallest numbers in each list are 3 and 5, a distance of 2. | |
Finally, the largest number in the left list is 4, while the largest number in the right list is 9; these are a distance 5 apart. | |
To find the total distance between the left list and the right list, add up the distances between all of the pairs you found. In the example above, this is 2 + 1 + 0 + 1 + 2 + 5, a total distance of 11! | |
Your actual left and right lists contain many location IDs. What is the total distance between your lists? | |
Your puzzle answer was 2378066. | |
--- Part Two --- | |
Your analysis only confirmed what everyone feared: the two lists of location IDs are indeed very different. | |
Or are they? | |
The Historians can't agree on which group made the mistakes or how to read most of the Chief's handwriting, but in the commotion you notice an interesting detail: a lot of location IDs appear in both lists! Maybe the other numbers aren't location IDs at all but rather misinterpreted handwriting. | |
This time, you'll need to figure out exactly how often each number from the left list appears in the right list. Calculate a total similarity score by adding up each number in the left list after multiplying it by the number of times that number appears in the right list. | |
Here are the same example lists again: | |
3 4 | |
4 3 | |
2 5 | |
1 3 | |
3 9 | |
3 3 | |
For these example lists, here is the process of finding the similarity score: | |
The first number in the left list is 3. It appears in the right list three times, so the similarity score increases by 3 * 3 = 9. | |
The second number in the left list is 4. It appears in the right list once, so the similarity score increases by 4 * 1 = 4. | |
The third number in the left list is 2. It does not appear in the right list, so the similarity score does not increase (2 * 0 = 0). | |
The fourth number, 1, also does not appear in the right list. | |
The fifth number, 3, appears in the right list three times; the similarity score increases by 9. | |
The last number, 3, appears in the right list three times; the similarity score again increases by 9. | |
So, for these example lists, the similarity score at the end of this process is 31 (9 + 4 + 0 + 0 + 9 + 9). | |
Once again consider your left and right lists. What is their similarity score?",18934359,"def distance(col1,col2): | |
tabdist = [] | |
assert (len(col1)==len(col2)) | |
for i in range(len(col1)): | |
tabdist.append(abs(col1[i])-col2[i]) | |
return tabdist | |
def sim_score(col1,col2): | |
tabscore = [] | |
similarity_score = 0 | |
for i in range(len(col1)): | |
appear = col2.count(col1[i]) | |
tabscore.append(col1[i]*appear) | |
similarity_score = sum(tabscore) | |
return similarity_score | |
f = open('input/inputday1.txt','r') | |
content = f.readlines() | |
print(content) | |
f.close() | |
col1 = [] | |
col2 = [] | |
for data in content: | |
c1,c2 = str.split(data) | |
col1.append(int(c1)) | |
col2.append(int(c2)) | |
print(col1,col2) | |
col1.sort() | |
col2.sort() | |
tabdist = distance(col1,col2) | |
ans = 0 | |
for number in tabdist: | |
ans += number | |
print(""ANSWER : "",ans) | |
print(""SIM SCORE = "",sim_score(col1,col2))",python:3.9 | |
2024,1,2,"--- Day 1: Historian Hysteria --- | |
The Chief Historian is always present for the big Christmas sleigh launch, but nobody has seen him in months! Last anyone heard, he was visiting locations that are historically significant to the North Pole; a group of Senior Historians has asked you to accompany them as they check the places they think he was most likely to visit. | |
As each location is checked, they will mark it on their list with a star. They figure the Chief Historian must be in one of the first fifty places they'll look, so in order to save Christmas, you need to help them get fifty stars on their list before Santa takes off on December 25th. | |
Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck! | |
You haven't even left yet and the group of Elvish Senior Historians has already hit a problem: their list of locations to check is currently empty. Eventually, someone decides that the best place to check first would be the Chief Historian's office. | |
Upon pouring into the office, everyone confirms that the Chief Historian is indeed nowhere to be found. Instead, the Elves discover an assortment of notes and lists of historically significant locations! This seems to be the planning the Chief Historian was doing before he left. Perhaps these notes can be used to determine which locations to search? | |
Throughout the Chief's office, the historically significant locations are listed not by name but by a unique number called the location ID. To make sure they don't miss anything, The Historians split into two groups, each searching the office and trying to create their own complete list of location IDs. | |
There's just one problem: by holding the two lists up side by side (your puzzle input), it quickly becomes clear that the lists aren't very similar. Maybe you can help The Historians reconcile their lists? | |
For example: | |
3 4 | |
4 3 | |
2 5 | |
1 3 | |
3 9 | |
3 3 | |
Maybe the lists are only off by a small amount! To find out, pair up the numbers and measure how far apart they are. Pair up the smallest number in the left list with the smallest number in the right list, then the second-smallest left number with the second-smallest right number, and so on. | |
Within each pair, figure out how far apart the two numbers are; you'll need to add up all of those distances. For example, if you pair up a 3 from the left list with a 7 from the right list, the distance apart is 4; if you pair up a 9 with a 3, the distance apart is 6. | |
In the example list above, the pairs and distances would be as follows: | |
The smallest number in the left list is 1, and the smallest number in the right list is 3. The distance between them is 2. | |
The second-smallest number in the left list is 2, and the second-smallest number in the right list is another 3. The distance between them is 1. | |
The third-smallest number in both lists is 3, so the distance between them is 0. | |
The next numbers to pair up are 3 and 4, a distance of 1. | |
The fifth-smallest numbers in each list are 3 and 5, a distance of 2. | |
Finally, the largest number in the left list is 4, while the largest number in the right list is 9; these are a distance 5 apart. | |
To find the total distance between the left list and the right list, add up the distances between all of the pairs you found. In the example above, this is 2 + 1 + 0 + 1 + 2 + 5, a total distance of 11! | |
Your actual left and right lists contain many location IDs. What is the total distance between your lists? | |
Your puzzle answer was 2378066. | |
--- Part Two --- | |
Your analysis only confirmed what everyone feared: the two lists of location IDs are indeed very different. | |
Or are they? | |
The Historians can't agree on which group made the mistakes or how to read most of the Chief's handwriting, but in the commotion you notice an interesting detail: a lot of location IDs appear in both lists! Maybe the other numbers aren't location IDs at all but rather misinterpreted handwriting. | |
This time, you'll need to figure out exactly how often each number from the left list appears in the right list. Calculate a total similarity score by adding up each number in the left list after multiplying it by the number of times that number appears in the right list. | |
Here are the same example lists again: | |
3 4 | |
4 3 | |
2 5 | |
1 3 | |
3 9 | |
3 3 | |
For these example lists, here is the process of finding the similarity score: | |
The first number in the left list is 3. It appears in the right list three times, so the similarity score increases by 3 * 3 = 9. | |
The second number in the left list is 4. It appears in the right list once, so the similarity score increases by 4 * 1 = 4. | |
The third number in the left list is 2. It does not appear in the right list, so the similarity score does not increase (2 * 0 = 0). | |
The fourth number, 1, also does not appear in the right list. | |
The fifth number, 3, appears in the right list three times; the similarity score increases by 9. | |
The last number, 3, appears in the right list three times; the similarity score again increases by 9. | |
So, for these example lists, the similarity score at the end of this process is 31 (9 + 4 + 0 + 0 + 9 + 9). | |
Once again consider your left and right lists. What is their similarity score?",18934359,"from collections import Counter | |
with open('inputs/input.txt', 'r') as file: | |
line: str = file.readline().rstrip('\n') | |
list1: list[int] = [] | |
list2: list[int] = [] | |
while line: | |
l: list[str] = line.split(' ') | |
list1.append(int(l[0])) | |
list2.append(int(l[-1])) | |
line = file.readline().rstrip('\n') | |
count_dict = Counter(list2) | |
similarity = 0 | |
for k in list1: | |
if k in count_dict: | |
similarity += (k * count_dict[k]) | |
print(f'Similarity Score: {similarity}') | |
",python:3.9 | |
2024,1,2,"--- Day 1: Historian Hysteria --- | |
The Chief Historian is always present for the big Christmas sleigh launch, but nobody has seen him in months! Last anyone heard, he was visiting locations that are historically significant to the North Pole; a group of Senior Historians has asked you to accompany them as they check the places they think he was most likely to visit. | |
As each location is checked, they will mark it on their list with a star. They figure the Chief Historian must be in one of the first fifty places they'll look, so in order to save Christmas, you need to help them get fifty stars on their list before Santa takes off on December 25th. | |
Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck! | |
You haven't even left yet and the group of Elvish Senior Historians has already hit a problem: their list of locations to check is currently empty. Eventually, someone decides that the best place to check first would be the Chief Historian's office. | |
Upon pouring into the office, everyone confirms that the Chief Historian is indeed nowhere to be found. Instead, the Elves discover an assortment of notes and lists of historically significant locations! This seems to be the planning the Chief Historian was doing before he left. Perhaps these notes can be used to determine which locations to search? | |
Throughout the Chief's office, the historically significant locations are listed not by name but by a unique number called the location ID. To make sure they don't miss anything, The Historians split into two groups, each searching the office and trying to create their own complete list of location IDs. | |
There's just one problem: by holding the two lists up side by side (your puzzle input), it quickly becomes clear that the lists aren't very similar. Maybe you can help The Historians reconcile their lists? | |
For example: | |
3 4 | |
4 3 | |
2 5 | |
1 3 | |
3 9 | |
3 3 | |
Maybe the lists are only off by a small amount! To find out, pair up the numbers and measure how far apart they are. Pair up the smallest number in the left list with the smallest number in the right list, then the second-smallest left number with the second-smallest right number, and so on. | |
Within each pair, figure out how far apart the two numbers are; you'll need to add up all of those distances. For example, if you pair up a 3 from the left list with a 7 from the right list, the distance apart is 4; if you pair up a 9 with a 3, the distance apart is 6. | |
In the example list above, the pairs and distances would be as follows: | |
The smallest number in the left list is 1, and the smallest number in the right list is 3. The distance between them is 2. | |
The second-smallest number in the left list is 2, and the second-smallest number in the right list is another 3. The distance between them is 1. | |
The third-smallest number in both lists is 3, so the distance between them is 0. | |
The next numbers to pair up are 3 and 4, a distance of 1. | |
The fifth-smallest numbers in each list are 3 and 5, a distance of 2. | |
Finally, the largest number in the left list is 4, while the largest number in the right list is 9; these are a distance 5 apart. | |
To find the total distance between the left list and the right list, add up the distances between all of the pairs you found. In the example above, this is 2 + 1 + 0 + 1 + 2 + 5, a total distance of 11! | |
Your actual left and right lists contain many location IDs. What is the total distance between your lists? | |
Your puzzle answer was 2378066. | |
--- Part Two --- | |
Your analysis only confirmed what everyone feared: the two lists of location IDs are indeed very different. | |
Or are they? | |
The Historians can't agree on which group made the mistakes or how to read most of the Chief's handwriting, but in the commotion you notice an interesting detail: a lot of location IDs appear in both lists! Maybe the other numbers aren't location IDs at all but rather misinterpreted handwriting. | |
This time, you'll need to figure out exactly how often each number from the left list appears in the right list. Calculate a total similarity score by adding up each number in the left list after multiplying it by the number of times that number appears in the right list. | |
Here are the same example lists again: | |
3 4 | |
4 3 | |
2 5 | |
1 3 | |
3 9 | |
3 3 | |
For these example lists, here is the process of finding the similarity score: | |
The first number in the left list is 3. It appears in the right list three times, so the similarity score increases by 3 * 3 = 9. | |
The second number in the left list is 4. It appears in the right list once, so the similarity score increases by 4 * 1 = 4. | |
The third number in the left list is 2. It does not appear in the right list, so the similarity score does not increase (2 * 0 = 0). | |
The fourth number, 1, also does not appear in the right list. | |
The fifth number, 3, appears in the right list three times; the similarity score increases by 9. | |
The last number, 3, appears in the right list three times; the similarity score again increases by 9. | |
So, for these example lists, the similarity score at the end of this process is 31 (9 + 4 + 0 + 0 + 9 + 9). | |
Once again consider your left and right lists. What is their similarity score?",18934359,"def read_file(file_path): | |
with open(file_path, 'r') as file: | |
return file.readlines() | |
def separate_columns(lines): | |
column1 = [] | |
column2 = [] | |
for line in lines: | |
col1, col2 = line.strip().split() | |
column1.append(col1) | |
column2.append(col2) | |
return column1, column2 | |
def similarity_score(column1, column2): | |
progressive_sum = 0 | |
for value1 in column1: | |
counter = 0 | |
for value2 in column2: | |
if value1 == value2: | |
counter += 1 | |
progressive_sum += counter*int(value1) | |
return progressive_sum | |
file_path = './input.txt' | |
lines = read_file(file_path) | |
column1, column2 = separate_columns(lines) | |
print(f""Similarity score: {similarity_score(column1, column2)}"") | |
",python:3.9 | |
2024,1,2,"--- Day 1: Historian Hysteria --- | |
The Chief Historian is always present for the big Christmas sleigh launch, but nobody has seen him in months! Last anyone heard, he was visiting locations that are historically significant to the North Pole; a group of Senior Historians has asked you to accompany them as they check the places they think he was most likely to visit. | |
As each location is checked, they will mark it on their list with a star. They figure the Chief Historian must be in one of the first fifty places they'll look, so in order to save Christmas, you need to help them get fifty stars on their list before Santa takes off on December 25th. | |
Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck! | |
You haven't even left yet and the group of Elvish Senior Historians has already hit a problem: their list of locations to check is currently empty. Eventually, someone decides that the best place to check first would be the Chief Historian's office. | |
Upon pouring into the office, everyone confirms that the Chief Historian is indeed nowhere to be found. Instead, the Elves discover an assortment of notes and lists of historically significant locations! This seems to be the planning the Chief Historian was doing before he left. Perhaps these notes can be used to determine which locations to search? | |
Throughout the Chief's office, the historically significant locations are listed not by name but by a unique number called the location ID. To make sure they don't miss anything, The Historians split into two groups, each searching the office and trying to create their own complete list of location IDs. | |
There's just one problem: by holding the two lists up side by side (your puzzle input), it quickly becomes clear that the lists aren't very similar. Maybe you can help The Historians reconcile their lists? | |
For example: | |
3 4 | |
4 3 | |
2 5 | |
1 3 | |
3 9 | |
3 3 | |
Maybe the lists are only off by a small amount! To find out, pair up the numbers and measure how far apart they are. Pair up the smallest number in the left list with the smallest number in the right list, then the second-smallest left number with the second-smallest right number, and so on. | |
Within each pair, figure out how far apart the two numbers are; you'll need to add up all of those distances. For example, if you pair up a 3 from the left list with a 7 from the right list, the distance apart is 4; if you pair up a 9 with a 3, the distance apart is 6. | |
In the example list above, the pairs and distances would be as follows: | |
The smallest number in the left list is 1, and the smallest number in the right list is 3. The distance between them is 2. | |
The second-smallest number in the left list is 2, and the second-smallest number in the right list is another 3. The distance between them is 1. | |
The third-smallest number in both lists is 3, so the distance between them is 0. | |
The next numbers to pair up are 3 and 4, a distance of 1. | |
The fifth-smallest numbers in each list are 3 and 5, a distance of 2. | |
Finally, the largest number in the left list is 4, while the largest number in the right list is 9; these are a distance 5 apart. | |
To find the total distance between the left list and the right list, add up the distances between all of the pairs you found. In the example above, this is 2 + 1 + 0 + 1 + 2 + 5, a total distance of 11! | |
Your actual left and right lists contain many location IDs. What is the total distance between your lists? | |
Your puzzle answer was 2378066. | |
--- Part Two --- | |
Your analysis only confirmed what everyone feared: the two lists of location IDs are indeed very different. | |
Or are they? | |
The Historians can't agree on which group made the mistakes or how to read most of the Chief's handwriting, but in the commotion you notice an interesting detail: a lot of location IDs appear in both lists! Maybe the other numbers aren't location IDs at all but rather misinterpreted handwriting. | |
This time, you'll need to figure out exactly how often each number from the left list appears in the right list. Calculate a total similarity score by adding up each number in the left list after multiplying it by the number of times that number appears in the right list. | |
Here are the same example lists again: | |
3 4 | |
4 3 | |
2 5 | |
1 3 | |
3 9 | |
3 3 | |
For these example lists, here is the process of finding the similarity score: | |
The first number in the left list is 3. It appears in the right list three times, so the similarity score increases by 3 * 3 = 9. | |
The second number in the left list is 4. It appears in the right list once, so the similarity score increases by 4 * 1 = 4. | |
The third number in the left list is 2. It does not appear in the right list, so the similarity score does not increase (2 * 0 = 0). | |
The fourth number, 1, also does not appear in the right list. | |
The fifth number, 3, appears in the right list three times; the similarity score increases by 9. | |
The last number, 3, appears in the right list three times; the similarity score again increases by 9. | |
So, for these example lists, the similarity score at the end of this process is 31 (9 + 4 + 0 + 0 + 9 + 9). | |
Once again consider your left and right lists. What is their similarity score?",18934359,"def calculate_similarity_score(left, right): | |
score = 0 | |
for left_element in left: | |
score += left_element * num_times_in_list(left_element, right) | |
return score | |
def num_times_in_list(number, right_list): | |
num_times = 0 | |
for element in right_list: | |
if number == element: | |
num_times += 1 | |
return num_times | |
def build_lists(contents): | |
left = [] | |
right = [] | |
for line in contents.split('\n'): | |
if line: | |
le, re = line.split() | |
left.append(int(le)) | |
right.append(int(re)) | |
return left, right | |
if __name__ == '__main__': | |
with open('1/day_1_input.txt', 'r') as f: | |
contents = f.read() | |
left, right = build_lists(contents) | |
score = calculate_similarity_score(left, right) | |
print(score) | |
",python:3.9 | |