/r/Python's 2022 Advent of Code

It's December, which means it's time for Advent of Code!

Advent of Code is a yearly advent calendar where there's a new programming puzzle each day. They vary in difficulty and don't require a computer science background to solve. You can solve the problems using any programming language you like, so while we love python here, you can take this as an opportunity to explore other languages. If you have trouble with one of the puzzles, don't worry, another day's puzzle might be easier for you!

This year we're joining our sister community, /r/LearnPython, for a shared leaderboard. The /r/Python and /r/LearnPython leaderboard code is: 1179846-33292986. Our sister community, the Python Discord is also hosting a leaderboard, head to #advent-of-code to join and discuss the puzzles!

If you want to talk about your solutions, please state which day the solution is for, and then put the solution under the spoiler tag (>!spoilers!< will become spoilers). Please be sure to format your code for reddit or host it on a code hosting site such as GitHub or GitLab. Additional discussion of the daily puzzles can be found on the Advent of Code Subreddit: /r/adventofcode

link to the leaderboard: https://adventofcode.com/2022/leaderboard/private enter the code 1179846-33292986 and click Join!

We hope you take this time as a chance to have some fun solving these puzzles! Good luck!

12 comments · 98% upvoted · other discussions

comments

jdnewmil

There is a general trend toward more difficult puzzles as the month progresses... Day 2 could very well be easier for you than Day 1, but Day 10 will definitely be harder than Day 2. But you tend to get into the rhythm of it as it goes. Don't get discouraged if you can't keep up late in the month... reading other people's solutions is very educational. And don't get hung up on making it to the leaderboards... they fill up one or two minutes after the puzzle is released.

Durinthal

Not at all optimized code golfing day 1 part 2, assuming the input is s:

sum(sorted([sum(t) for t in [[int(i) for i in l.split()] for l in s.split("\n\n")]])[-3:])

JMVs_Rules

Can I do some days later? E.g. today, I wanna sleep but want to do these

[deleted]

yes

IamNotIntelligent69

Im pretty sure you can, I can still access questions from AoC 2021.

JMVs_Rules

Yeah I found out, thank you! Got all 6 open questions done till now

mauriciofuentesf

oh this is a first for me! Most of the year ive been learning python and bc of work had to stop for a few months. Its gonna be a good way to refresh my memory and pick up were i left!

EvInChains

Had a lot of fun with Day 3's puzzles. Ended up using functools.reduce and the grouper recipe from itertools: https://github.com/EvanFNG/Advent-of-Code-2022/blob/main/day3.py

RakingBuckets

I really enjoyed writing unreadable code for Day 3:

from functools import reduce
from string import ascii_letters

priorities = {char: i for i, char in enumerate(ascii_letters, 1)}

with open("day03") as data:
    lines = data.read().split("\n")

print("part one:",
    sum(
        sum(
            map(priorities.get, 
                set(line[:(split := len(line) // 2)]) & set(line[split:]))
        ) 
        for line in lines
    )
)

print("part two:",
    sum(
        map(
            lambda s: sum(map(priorities.get, reduce(set.intersection, s))), 
            zip(*list(map(set, lines[i::3]) for i in range(3)))
        )
    )
 )
ProbablyDoesntLikeU

It's time!!!

yashk1

Just found out about this. Right in time when i am up skilling my python skills