Skip to content

Advent of Code 2022

Day 01
max_calories = 0
calories = 0

with open("day01_input.txt") as f:
    for line in f:
        if not line.strip():
            if calories > max_calories:
                max_calories = calories
            calories = 0
            continue
        calories += int(line)

print(max_calories)
all_calories = []
calories = 0

with open("day01_input.txt") as f:
    for line in f:
        if not line.strip():
            all_calories.append(calories)
            calories = 0
            continue
        calories += int(line)

print(sum(sorted(all_calories)[-3:]))
Day 02
score = 0

op_order = ["A", "B", "C"]
me_order = ["X", "Y", "Z"]
with open("day02_input.txt") as f:
    for line in f:
        op, me = line.strip().split()
        ind_op = op_order.index(op)
        ind_me = me_order.index(me)
        if ind_me == ind_op:
            score += ind_me + 1 + 3
        elif any(
            [
                ind_me == 1 and ind_op == 0,
                ind_me == 2 and ind_op == 1,
                ind_me == 0 and ind_op == 2,
            ]
        ):
            score += ind_me + 1 + 6
        else:
            score += ind_me + 1 + 0

print(score)
score = 0

op_order = ["A", "B", "C"]
me_order = ["X", "Y", "Z"]
with open("day02_input.txt") as f:
    for line in f:
        op, me = line.strip().split()
        ind_op = op_order.index(op)
        ind_me = me_order.index(me)
        if ind_me == 1:
            score += ind_op + 1 + 3
        elif ind_me == 2:
            shape_map = {
                0: 1 + 1,
                1: 2 + 1,
                2: 0 + 1,
            }
            score += shape_map[ind_op] + 6
        else:
            shape_map = {
                0: 2 + 1,
                1: 0 + 1,
                2: 1 + 1,
            }
            score += shape_map[ind_op] + 0

print(score)
Day 03
priorities = 0

with open("day03_input.txt") as f:
    for line in f:
        line = line.strip()
        if not line:
            continue
        first, second = line[: len(line) // 2], line[len(line) // 2 :]
        overlap = set(first).intersection(set(second)).pop()
        if overlap.isupper():
            priorities += ord(overlap) - ord("A") + 27
        else:
            priorities += ord(overlap) - ord("a") + 1

print(priorities)
priorities = 0

with open("day03_input.txt") as f:
    groups = f.readlines()
    groups = [groups[3 * i : 3 * i + 3] for i in range(0, len(groups) // 3)]

    for g in groups:
        items = None
        for line in g:
            line = line.strip()
            if not items:
                items = set(line)
            else:
                items = items.intersection(set(line))
        overlap = items.pop()

        if overlap.isupper():
            priorities += ord(overlap) - ord("A") + 27
        else:
            priorities += ord(overlap) - ord("a") + 1

print(priorities)
Day 04
count = 0

with open("day04_input.txt") as f:
    sections = f.readlines()
    for section in sections:
        section = section.strip()
        r1, r2 = section.split(",")

        r1_start, r1_stop = int(r1.split("-")[0]), int(r1.split("-")[1]) + 1
        r1 = set(range(r1_start, r1_stop))

        r2_start, r2_stop = int(r2.split("-")[0]), int(r2.split("-")[1]) + 1
        r2 = set(range(r2_start, r2_stop))

        if r1.issuperset(r2) or r2.issuperset(r1):
            count += 1

print(count)
count = 0

with open("day04_input.txt") as f:
    sections = f.readlines()
    for section in sections:
        section = section.strip()
        r1, r2 = section.split(",")

        r1_start, r1_stop = int(r1.split("-")[0]), int(r1.split("-")[1]) + 1
        r1 = set(range(r1_start, r1_stop))

        r2_start, r2_stop = int(r2.split("-")[0]), int(r2.split("-")[1]) + 1
        r2 = set(range(r2_start, r2_stop))

        if r1.intersection(r2):
            count += 1

print(count)
Day 06
1
2
3
4
5
6
7
8
with open("day06_input.txt") as f:
    buffer = f.read()

    for i in range(0, len(buffer) - 4):
        if len(set(buffer[i : i + 4])) == 4:
            break

    print(i + 4)
1
2
3
4
5
6
7
8
9
with open("day06_input.txt") as f:
    buffer = f.read()

    offset = 14
    for i in range(0, len(buffer) - offset):
        if len(set(buffer[i : i + offset])) == offset:
            break

    print(i + offset)