Advent of Code is an annual online event that challenges participants with daily programming problems from December 1st to 25th. These problems range in difficulty and are designed to test and enhance your coding skills. This post demonstrates how to solve Advent of Code 2024 problems using Wolfram, providing step-by-step solutions. You can find recordings of solutions to these problems in the Live Coding sessions here: https://www.youtube.com/@wolframinstitute/videos
Initialization
Initialization
Click on the problem name above, or evaluate prompt[1] to view Problem Day 1
In[]:=
prompt[1]
Out[]=
--- 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 44 32 51 33 93 3Maybe 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?
In[]:=
summarizeProblem[prompt[1],1]
In[]:=
summarizeProblem[prompt[1],2]
Out[]=
In Part Two of the Advent of Code challenge, you need to calculate a "similarity score" between two lists of location IDs (the left list and the right list). Here's a concise summary of the task:1. **Input**: You have two lists of integers: - **Left List**: Contains location IDs. - **Right List**: Also contains location IDs.2. **Goal**: For each number in the left list, determine how many times it appears in the right list and calculate a similarity score.3. **Calculation**: - For each number in the left list: - Count how many times that number appears in the right list. - Multiply the number from the left list by its count from the right list. - Add this product to the total similarity score.4. **Output**: The final similarity score, which is the sum of all the products calculated for each number in the left list.### Example:Given the left list: `3, 4, 2, 1, 3, 3` and the right list: `3, 4, 3, 2, 5, 1, 3, 9`:- For `3`: appears 4 times in the right list → score += 3 * 4 = 12- For `4`: appears 1 time in the right list → score += 4 * 1 = 4- For `2`: appears 1 time in the right list → score += 2 * 1 = 2- For `1`: appears 1 time in the right list → score += 1 * 1 = 1- For `3` (again): appears 4 times in the right list → score += 3 * 4 = 12- For `3` (again): appears 4 times in the right list → score += 3 * 4 = 12Total similarity score = 12 + 4 + 2 + 1 + 12 + 12 = 43.### Implementation Steps:1. Read the two lists of integers.2. For each integer in the left list, count its occurrences in the right list.3. Calculate the similarity score based on the counts.4. Output the final similarity score.This summary should help you focus on the coding task for Part Two of the challenge.
Solution
Solution
In[]:=
{left,right}=Thread@ImportString[input[1],"Table"];
In[]:=
answer[1,1]=Total[Abs[Sort[left]-Sort[right]]]
Out[]=
2904518
In[]:=
postAnswer[1,1]
Out[]=
That's the right answer! You are one gold star closer to finding the Chief Historian. [Continue to Part Two] |
In[]:=
answer[1,2]=Total[left×Lookup[Counts[right],left,0]]
Out[]=
18650129
In[]:=
postAnswer[1,2]
Out[]=
That's the right answer! You are one gold star closer to finding the Chief Historian. You have completed Day 1! You can [Share on Bluesky Twitter Mastodon ] this victory or [Return to Your Advent Calendar] . |
In[]:=
prompt[4]
Out[]=
--- Day 4: Ceres Search ---"Looks like the Chief's not here. Next!" One of The Historians pulls out a device and pushes the only button on it. After a brief flash, you recognize the interior of the Ceres monitoring station !As the search for the Chief continues, a small Elf who lives on the station tugs on your shirt; she'd like to know if you could help her with her word search (your puzzle input). She only has to find one word: XMAS .This word search allows words to be horizontal, vertical, diagonal, written backwards, or even overlapping other words. It's a little unusual, though, as you don't merely need to find one instance of XMAS - you need to find all of them . Here are a few ways XMAS might appear, where irrelevant characters have been replaced with . :..X....SAMX..A..A.XMAS.S.X....The actual word search will be full of letters instead. For example:MMMSXXMASMMSAMXMSMSAAMXSXMAAMMMSAMASMSMXXMASAMXAMMXXAMMXXAMASMSMSASXSSSAXAMASAAAMAMMMXMMMMMXMXAXMASXIn this word search, XMAS occurs a total of 18 times; here's the same word search again, but where letters not involved in any XMAS have been replaced with . :....XXMAS..SAMXMS......S..A.....A.A.MS.XXMASAMX.MMX.....XA.AS.S.S.S.SS.A.A.A.A.A..M.M.M.MM.X.X.XMASXTake a look at the little Elf's word search. How many times does XMAS appear?
Solution
Solution
Visuals
Visuals
Solution
Solution
Solution
Solution
Solution
Solution
Solution
Solution
Solution
Solution
Solution
Solution
Solution
Solution
CITE THIS NOTEBOOK
CITE THIS NOTEBOOK
Advent of code 2024: Wolfram Language solutions by Nikolay Murzin
by Nikolay Murzin
Wolfram Community, STAFF PICKS, January 11, 2025
https://community.wolfram.com/groups/-/m/t/3355474
by Nikolay Murzin
Wolfram Community, STAFF PICKS, January 11, 2025
https://community.wolfram.com/groups/-/m/t/3355474