Alper Ahmetoglu

Advent of Code 2024: solutions with explanations

#programming

My aoc24 solutions. I will not try to write the most efficient code, but I'll also try to be algorithmically efficient. E.g., I don't really care if I could have done some shenanigans while reading the file, but I will try to care about not doing unnecessary loops.

Day 1

Part 1

We are asked to pair the nth smallest element in the left list with the nth smallest element in the right list. To know the nth smallest element in a list, the list should be at least partially ordered. So, we sort them and count the difference.

import sys

f = open(sys.argv[1], "r")
arr1 = []
arr2 = []
for line in f.readlines():
    x, y = line.split()
    arr1.append(int(x))
    arr2.append(int(y))
arr1 = sorted(arr1)
arr2 = sorted(arr2)
total_distance = 0
for x, y in zip(arr1, arr2):
    total_distance += abs(x-y)
print(total_distance)

Part 2

In this part, the right list can be thought of as the weights of the numbers that appear on the left. We don’t need to sort the lists but we have to iterate over each of them to (1) find the weight of each number and (2) count them with respect to those weights.

import sys

f = open(sys.argv[1], "r")
arr1 = []
arr2 = []
for line in f.readlines():
    x, y = line.split()
    arr1.append(int(x))
    arr2.append(int(y))

weights = {}
similarity = 0
for y in arr2:
    if y not in weights:
        weights[y] = 0
    weights[y] += 1
for x in arr1:
    if x not in weights:
        continue
    similarity += x * weights[x]
print(similarity)

Day 2

Part 1

We are asked to check if a sequence of numbers is decreasing or increasing by some specific thresholds. As we need to report how many of them checks this criteria, we have to iterate over them. While checking the sequence, we can stop at any point when we realize the sequence doesn't match the criteria.

import sys

file = sys.argv[1]
safe_count = 0
with open(file) as f:
    for line in f.readlines():
        line = line + " "
        sign = None
        prev_num = None
        curr_num = ""
        safe = True
        for char in line:
            if char == " ":
                curr_num = int(curr_num)
                if prev_num is None:
                    prev_num = curr_num
                    curr_num = ""
                    continue

                if sign is None:
                    diff = curr_num - prev_num
                    if not (1 <= abs(diff) <= 3):
                        safe = False
                        break
                    else:
                        sign = 1 if diff > 0 else -1
                        prev_num = curr_num
                        curr_num = ""
                        continue

                if 1 <= ((curr_num - prev_num) * sign) <= 3:
                    prev_num = curr_num
                    curr_num = ""
                else:
                    safe = False
                    break
            else:
                curr_num += char

        if safe:
            safe_count += 1
print(safe_count)