this post was submitted on 13 Nov 2025
1 points (54.5% liked)

Advent Of Code

1202 readers
3 users here now

An unofficial home for the advent of code community on programming.dev! Other challenges are also welcome!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

Everybody Codes is another collection of programming puzzles with seasonal events.

EC 2025

AoC 2025

Solution Threads

M T W T F S S
1 2 3 4 5 6 7
8 9 10 11 12

Visualisations Megathread

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 2 years ago
MODERATORS
 

Quest 6: Mentorship Matrix

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

Link to participate: https://everybody.codes/

you are viewing a single comment's thread
view the rest of the comments
[โ€“] Pyro@programming.dev 2 points 1 month ago

Python

Used sliding window for part 3. The off-by-one difference between i and j tripped me up for a while.

def part1(data: str, mentor: str = 'A'):
    mentors = 0
    pairs = 0
    for p in data:
        if p == mentor:
            mentors += 1
        elif p == mentor.lower():
            pairs += mentors
    return pairs

assert part1("ABabACacBCbca") == 5

def part2(data: str):
    all_pairs = 0
    for mentor in 'ABC':
        all_pairs += part1(data, mentor)
    return all_pairs    

assert part2("ABabACacBCbca") == 11

from collections import defaultdict

def part3(data: str, distance_limit: int = 1000, repeat: int = 1000):
    n = len(data)
    N = len(data) * repeat

    pairs = 0
    person_count = defaultdict(int)
    curr = 0

    # initialize the first window excluding the right boundary
    for j in range(distance_limit):
        person_count[data[j % n]] += 1

    for curr in range(N):
        # move left boundary (if applicable)
        if (i := curr - distance_limit - 1) >= 0:
            person_count[data[i % n]] -= 1
        # move right boundary (if applicable)
        if (j := curr + distance_limit) < N:
            person_count[data[j % n]] += 1
        # if mentee, record pairs
        if data[curr % n].islower():
            pairs += person_count[data[curr % n].upper()]

    return pairs
    

assert (t := part3("AABCBABCABCabcabcABCCBAACBCa", 10, 1)) == 34, f"Expected 34 but got {t}"
assert (t := part3("AABCBABCABCabcabcABCCBAACBCa", 10, 2)) == 72, f"Expected 72 but got {t}"
assert (t := part3("AABCBABCABCabcabcABCCBAACBCa", 1000, 1000)) == 3442321, f"Expected 3442321 but got {t}"-