Advent of Code 25

Day 1

code = 0
pointing = 50

with open("day-1/input.txt", "r") as file:
    content = file.read()

    for line in content.split("\n"):
        chars = list(line)

        if len(chars) > 0:
            direction = chars.pop(0)
            value = int("".join(chars))

            print("\n%s, %s%s" % (pointing, direction, value)) # just for debugging

            if direction == "L":
                pointing += value
                pointing %= 100
            else:
                pointing -= value
                pointing %= 100
            
            print("%s\n" % pointing)

            if pointing == 0:
                code += 1

print(code)
                            
code = 0
pointing = 50

with open("day-1/input.txt", "r") as file:
    content = file.read()

    for line in content.split("\n"):
        chars = list(line)

        if len(chars) > 0:
            direction = chars.pop(0)
            value = int("".join(chars))

            print("\n%s, %s%s" % (pointing, direction, value)) # just for debugging

            if direction == "L":
                for i in range(value):
                    pointing += 1
                    pointing %= 100

                    if pointing == 0:
                        code += 1
            else:
                for i in range(value):
                    pointing -= 1
                    pointing %= 100

                    if pointing == 0:
                        code += 1
            
            print("%s\n" % pointing)

print(code)
                            

Day 2

total_of_invalid_ids = 0

with open("day-2/input.txt", "r") as file:
    content = file.read()

    for id_range in content.split(","):
        start = id_range.split("-")[0]
        end = id_range.split("-")[1]

        for i in range(int(start), int(end)):
            s = str(i)
            sl = len(s) // 2

            if s[:sl] == s[sl:]:
                total_of_invalid_ids += i

print(total_of_invalid_ids)
                            
total_of_invalid_ids = 0

def is_repeated_sequence(s: str) -> bool:
    if len(s) < 2:
        return False
    
    return (s + s).find(s, 1) < len(s)

with open("day-2/input.txt", "r") as file:
    content = file.read()

    for id_range in content.split(","):
        start_s, end_s = [p for p in id_range.split("-")]
        start, end = int(start_s), int(end_s)

        for i in range(start, end):
            if is_repeated_sequence(str(i)):
                total_of_invalid_ids += i

print(total_of_invalid_ids)
                            

Day 3

voltage = 0

with open("day-3/input.txt", "r") as file:
    content = file.read()

    for line in content.split("\n"):
        chars = list(line)

        biggest_combo = 0
        for i, c in enumerate(chars):
            temp = chars[i + 1:]

            biggest_num = 0
            for c2 in temp:
                if int(c2) > biggest_num:
                    biggest_num = int(c2)
                        
            if int(c + str(biggest_num)) > biggest_combo and biggest_num != 0:
                biggest_combo = int(c + str(biggest_num))
        
        voltage += biggest_combo

print(voltage)
                            
todo
                            

Day 4

map = []
amount_can_take = 0

with open("day-4/input.txt", "r") as file:
    content = file.read()

    for line in content.split("\n"):
        section = []

        for char in list(line):
            section.append(char)
        
        map.append(section)

for iy, y in enumerate(map):
    for ix, x in enumerate(y):
        if x == "@":
            adjacent = 0

            if iy > 0:
                if map[iy - 1][ix] == "@":
                    adjacent += 1
                
                if ix > 0:
                    if map[iy - 1][ix - 1] == "@":
                        adjacent += 1
                
                if ix < len(map[iy - 1]) - 1:
                    if map[iy - 1][ix + 1] == "@":
                        adjacent += 1
            
            if ix > 0:
                if map[iy][ix - 1] == "@":
                    adjacent += 1
            
            if ix < len(map[iy]) - 1:
                if map[iy][ix + 1] == "@":
                    adjacent += 1
            
            if iy < len(map) - 1:
                if map[iy + 1][ix] == "@":
                    adjacent += 1
                
                if ix > 0:
                    if map[iy + 1][ix - 1] == "@":
                        adjacent += 1
                
                if ix < len(map[iy + 1]) - 1:
                    if map[iy + 1][ix + 1] == "@":
                        adjacent += 1
            
            if adjacent < 4:
                amount_can_take += 1

print(amount_can_take)
                            
map = []
amount_can_take = 0

with open("day-4/input.txt", "r") as file:
    content = file.read()

    for line in content.split("\n"):
        section = []

        for char in list(line):
            section.append(char)
        
        map.append(section)

last_result = 0

while True:
    for iy, y in enumerate(map):
        for ix, x in enumerate(y):
            if x == "@":
                adjacent = 0

                if iy > 0:
                    if map[iy - 1][ix] == "@":
                        adjacent += 1
                    
                    if ix > 0:
                        if map[iy - 1][ix - 1] == "@":
                            adjacent += 1
                    
                    if ix < len(map[iy - 1]) - 1:
                        if map[iy - 1][ix + 1] == "@":
                            adjacent += 1
                
                if ix > 0:
                    if map[iy][ix - 1] == "@":
                        adjacent += 1
                
                if ix < len(map[iy]) - 1:
                    if map[iy][ix + 1] == "@":
                        adjacent += 1
                
                if iy < len(map) - 1:
                    if map[iy + 1][ix] == "@":
                        adjacent += 1
                    
                    if ix > 0:
                        if map[iy + 1][ix - 1] == "@":
                            adjacent += 1
                    
                    if ix < len(map[iy + 1]) - 1:
                        if map[iy + 1][ix + 1] == "@":
                            adjacent += 1
                
                if adjacent < 4:
                    amount_can_take += 1
                    map[iy][ix] = "."
    
    if amount_can_take == last_result:
        break
    else:
        last_result = amount_can_take

print(amount_can_take)
                            

Day 5

fresh = 0

id_ranges = []
ids = []

with open("day-5/input.txt", "r") as file:
    content = file.read()

    for line in content.split("\n"):
        if "-" in line:
            id_ranges.append(
                (
                    int(line.split("-")[0]), 
                    int(line.split("-")[1])
                )
            )
        elif len(line) > 0:
            ids.append(int(line))

for id in ids:
    for range in id_ranges:
        if id >= range[0] and id <= range[1]:
            fresh += 1
            break

print(fresh)
                            
todo
                            

Day 6

total = 0

with open("day-6/input.txt", "r") as file:
    content = file.read()
    lines = content.split("\n")

    rows = []

    for i in range(4):
        rows.append([int(x) for x in lines[i].split()])
    
    ops = lines[4].split()

    for i in range(len(rows[0])):
        result = 0

        if ops[i] == "+":
            result = rows[0][i] + rows[1][i] + rows[2][i] + rows[3][i]
        else:
            result = rows[0][i] * rows[1][i] * rows[2][i] * rows[3][i]
        
        total += result

print(total)
                            
todo
                            

Day 7

area = []

with open("day-7/input.txt", "r") as file:
    content = file.read()
    lines = content.split("\n")

    for line in lines:
        area.append([ char for char in list(line) ])

rows = len(area)
cols = len(area[0])

start_row = None
start_col = None

for y in range(rows):
    for x in range(len(area[y])):
        if area[y][x] == "S":
            start_row, start_col = y, x
            break
    
    if start_row is not None:
        break

splits = 0
active = {start_col}

for y in range(start_row + 1, rows):
    new_active = set()

    for x in active:
        if x < 0 or x >= len(area[y]):
            continue

        cell = area[y][x]

        if cell == '^':
            splits += 1

            if x - 1 >= 0:
                new_active.add(x - 1)
            
            if x + 1 < len(area[y]):
                new_active.add(x + 1)
        else:
            new_active.add(x)
    active = new_active

print(splits)
                            
todo
                            

Day 8

todo
                            
todo
                            

Day 9

red_squares = []

with open("day-9/input.txt", "r") as file:
    content = file.read()
    lines = content.split("\n")

    for line in lines:
        red_squares.append((
            int(line.split(",")[0]), int(line.split(",")[1])
        ))

largest_square = 0

for s1 in red_squares:
    for s2 in red_squares:
        dist_x = abs(s1[0] - s2[0]) + 1
        dist_y = abs(s1[1] - s2[1]) + 1

        if dist_x * dist_y > largest_square:
            largest_square = dist_x * dist_y

print(largest_square)
                            
todo
                            

Day 10

todo
                            
todo
                            

Day 11

todo
                            
todo
                            

Day 12

todo
                            
todo