I spent some time to solve programming puzzles from AdventOfCode-2015 as it checks ones scripting/programming skills by providing numerous programming challenges level wise, it has total 25 days of challenges this paritcular blog has first 5 challengs that i managed to solve.
All Puzzle’s scripts can be found here
Day 1: Not Quite Lisp
Description
- Initialize a variable to keep track of the current floor, starting at floor 0.
- Iterate through each character in the instructions.
- If the character is an opening parenthesis ‘(‘, increment the current floor by 1.
- If the character is a closing parenthesis ‘)’, decrement the current floor by 1.
- After processing all characters, the current floor will be the result.
Solve
1 | class PuzzleSolver: |
Day 2: I Was Told There Would Be No Math
Description
Approach
- Read the dimensions (length, width, and height) of each present from the input.
- For each present, calculate the surface area of the box using the formula: 2lw+2wh+2hl2lw+2wh+2hl.
- Determine the area of the smallest side of the box.
- Add the surface area and the area of the smallest side to get the total required wrapping paper for each present.
- Sum up the total required wrapping paper for all presents to find the total square feet of wrapping paper needed.
Solve
1 | class PuzzleSolver: |
OneLiner
1 | print(sum(2 * (l*w + w*h + h*l) + min(l*w, w*h, h*l) for l, w, h in (map(int, line.strip().split('x')) for line in open("./input.txt")))) |
Day 3: Perfectly Spherical Houses in a Vacuum
Description
In the puzzle, Santa is moving around on a two-dimensional grid. The grid represents the map of houses in which Santa is delivering presents. Each house is identified by its coordinates on the grid. For simplicity, we can consider the grid as a Cartesian coordinate system, where each house is located at a unique pair of x and y coordinates.
When we initialize Santa’s starting position at (0, 0)
, we’re placing him at the center of the grid. This point serves as the starting point from which Santa begins his delivery route. The (0, 0)
point represents the origin of the grid, where the x and y coordinates are both zero.
1 | Y' (North) |
As Santa moves around the grid according to the given directions (north, south, east, west), his position on the grid changes. Each movement in a particular direction (e.g., one step to the north) results in a change of Santa’s coordinates on the grid.
- Moving one step to the north increases Santa’s y-coordinate by 1.
- Moving one step to the south decreases Santa’s y-coordinate by 1.
- Moving one step to the east increases Santa’s x-coordinate by 1.
- Moving one step to the west decreases Santa’s x-coordinate by 1.
So if:
- If Santa moves north from
(0, 0)
, he reaches(0, 1)
. - If Santa moves east from
(0, 0)
, he reaches(1, 0)
. - If Santa moves south from
(1, 0)
, he returns to(0, 0)
.
For Example:
for string
^>v<
:Santa starts at (0,0) and moves north, delivering a present to (0,1).
Then he moves east, delivering a present to (1,1).
Next, he moves south, delivering a present to (1,0).
Finally, he moves west, delivering a present to (0,0) again (his starting location).
In this case, Santa ends up visiting four houses:
- (0,0) - visited twice
- (0,1)
- (1,0)
- (1,1)
Even though Santa revisits his starting location, it still counts as a unique house visited. Therefore, in total, four houses receive at least one present.
To solve this puzzle, you’ll need to simulate Santa’s movements and keep track of the houses he visits. Here’s a high-level guide on how you can proceed:
Initialize Santa’s starting position: Santa starts at the origin (0, 0) on the grid.
Read the input directions: Read the input directions provided in the puzzle.
Iterate over each direction: For each direction (north, south, east, or west), update Santa’s position accordingly.
Keep track of visited houses: Maintain a set or dictionary to keep track of the houses Santa has visited. Add each visited house to the set or dictionary.
Count unique visited houses: After iterating through all directions, count the number of unique houses visited by Santa. This count represents the number of houses that receive at least one present.
Solve
1 | class PuzzleSolver: |
Day 4: The Ideal Stocking Stuffer
Description
This one is pretty simple we just have to brute force the puzzle input with combining sereies of positive number seuentially and check if its hash converted strings is starting with 00000
Approach
- Choose libraries or built-in functions for calculating MD5 hashes.
- Define a function to calculate the MD5 hash of a given string.
- Create a loop to iterate over increasing numbers starting from 1 (or any other starting value you prefer).
- Append each number to the secret key.
- Calculate the MD5 hash of the combined string.
- Check if the hash starts with at least five zeroes.
Solve
1 | import hashlib |
Day 5: Doesn’t He Have Intern-Elves For This?
Description
Approach
- Read the strings from the text file.
- Define a function to check if a string is “nice” according to the given criteria.
- Iterate through each string and use the function to determine if it’s “nice” or “naughty”.
- Keep count of the number of “nice” strings found.
- Print the total count of “nice” strings.
Solve
1 | import string |
Summary
The challenges were a good warm-up, with some tricky parts to keep things interesting specially puzzle 3 that took me more time to solve, i would like to try other puzzle as well and post writeup.
One key takeaway is the importance of efficiency in problem-solving. By implementing wrapper functions, we can save time and avoid redundant code, ultimately enhancing our overall productivity. This approach streamlined the solving process and prepared me to tackle more complex challenges efficiently.