This year, I've got a paper to write in Python, so I thought, why not choose Python for AOC as well? Again I need to go through the syntax and learn how we structure our ideas. So let's begin.
Challenges can be found here.
Day 1
The Day 1: Trebuchet. Before doing that I need to update my local machine with my AOC GitHub repo, which has the solutions of previous years. Even last year I was not able to find time to do all the challenges. This time around, I aim to complete all the challenges. If you happen to come across this blog, please drop a reminder in the comments if I forget the deadline to submit my solutions. Thanks!
Setting the Stage for the Challenge
The plan so far looks simple, it's all about how the elves plan to lift up. They've got a tricky method, making the ride not as simple as it seems. Let's help them calibrate.
Input Processing Strategies
The input looks like this, and all we need to do is split it at the new line.
1abc2
pqr3stu8vwx
zoneight234
7pqrstsixteen
Solving the Part I ⭐
I guess we need to look at the first letters until we find a number, then go backward from the end until we find another number. For now, this is the idea that I can come up with.
def find_first_last_digits(input_string):
digits = [int(character) for character in input_string if character.isdigit()]
if digits:
first_digit = digits[0]
last_digit = digits[-1]
return int(first_digit), int(last_digit)
else:
return None, None
digits
➡️ It goes through each character in the sentence to see if it's a number and saves it.Then we get first and last by index.
Solving the Part II ⭐
Elves use super logical codes, and the part of strings that we need to decode has words like "one," "two," and "three" instead of digits. So our earlier logic won't work here. I guess we could go for search and replace.
This will be slow as we will be searching for each string and then replacing it. Please do share if you have better solutions than these.
for str2 in lines:
str2 = str2.replace("one","o1ne")
str2 = str2.replace("two","t2wo")
str2 = str2.replace("three","th3ree")
str2 = str2.replace("four","fo4ur")
str2 = str2.replace("five","fiv5e")
str2 = str2.replace("six","si6x")
str2 = str2.replace("seven","sev7n")
str2 = str2.replace("eight","e8ight")
str2 = str2.replace("nine","nin9e")
first, last = find_first_last_digits(str2);
sum2 = sum2 + (first * 10) + last
Day 2
Day 2: Cube Conundrum, Challenge can be found here. So we're in the sky now, a place called Snow Island and it has very little snow. The Elf who is about to accompany us offers to play a game with us.
Input Processing Strategies
For example input I tried by using Dictionary. We need to find how to process that input from a file. I find this more challenging at times.
with open('/home/nisnym/t/practice/Advent-of-Code/2023/Day 2/input.txt', 'r') as file:
# Read all lines from the file
lines = file.read().split('\n')
game_datas = {}
for line in lines:
# game_info = line.split("Game")
game_info = line.split(": ")
game_number = game_info[0][5:]
game_data = game_info[1].split("; ")
# print(game_number)
game_results = []
for result in game_data:
colors_counts = result.split(", ")
color_dict = {color: int(count) for count, color in [cc.split() for cc in colors_counts]}
game_results.append(color_dict)
game_datas[game_number] = game_results
Below compare the game_data
Dictionary with the given values for possible games.
Solving the Part I ⭐
Let's figure out the games possible with 12 red cubes, 13 green cubes, and 14 blue cubes in the bag. After that, we'll add up the game IDs.
target_counts = {'red': 12, 'green': 13, 'blue': 14}
possible_games = []
for game_no, game_round in game_datas.items():
valid_games = True
for ins in game_round:
for color, count in ins.items():
if color not in target_counts or count > target_counts[color]:
valid_games = False
break
if valid_games:
possible_games.append(int(game_no))
Solving the Part II ⭐
The elf is creating things for himself now. The above conditions don't matter here now. All we need to do is to find the minimum number of balls needed for Game N to happen. This means that we need to find the max of every set and then finding the total sum of it.
answer = 0
for game_id, rounds in game_datas.items():
max_colurs = {'red': 0, 'green': 0, 'blue': 0}
for dict in rounds:
for color, count in dict.items():
max_colurs[color] = max(count,max_colurs[color])
answer += max_colurs['blue'] * max_colurs['green'] * max_colurs['red']
print(f"the power is {answer}")
Day 3
Day 3: Gear Ratios, Challenge can be found here. After reaching the Gondola Lift, it doesn't work. Looks like everything is broken and now we need to help the Engineer Elf to fix the machine.