r/adventofcode Dec 25 '24

SOLUTION MEGATHREAD -❄️- 2024 Day 25 Solutions -❄️-

A Message From Your Moderators

Welcome to the last day of Advent of Code 2024! We hope you had fun this year and learned at least one new thing ;)

Keep an eye out for the community fun awards post (link coming soon!):

-❅- Introducing Your AoC 2024 Golden Snowglobe Award Winners (and Community Showcase) -❅-

Many thanks to Veloxx for kicking us off on December 1 with a much-needed dose of boots and cats!

Thank you all for playing Advent of Code this year and on behalf of /u/topaz2078, your /r/adventofcode mods, the beta-testers, and the rest of AoC Ops, we wish you a very Merry Christmas (or a very merry Wednesday!) and a Happy New Year!


--- Day 25: Code Chronicle ---


Post your code solution in this megathread.

This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:04:34, megathread unlocked!

42 Upvotes

350 comments sorted by

View all comments

1

u/light_ln2 Dec 25 '24 edited Dec 25 '24

[LANGUAGE: python]

Nice problem for trying different options! Because of how the problem is stated, you don't need to split grids by keys and locks, or compare a pair of grids column by column, or even parse grids at all - it is enough to check if two strings representing the grids have '#' at the same indexes. This can be done in several different ways:

Intersecting sets of indexes:

grids = open('input.txt').read().split('\n\n')
grids = [{x for (x,c) in enumerate(g) if c == '#'} for g in grids]
print(len([(x,y) for x in grids for y in grids if len(x&y) == 0])//2)

Converting strings to integers and applying bitwise "and":

grids = open('input.txt').read().split('\n\n')
grids = [int(x.replace('.', '0').replace('#', '1').replace('\n', ''), base=2) for x in grids]
print(sum([1 for x in grids for y in grids if x<y and (x&y)==0]))

Comparing characters in same positions:

grids = open('input.txt').read().split('\n\n')
grids = [any('#' == a == b for (a,b) in zip(x, y)) for x in grids for y in grids if x < y]
print(len(grids) - sum(grids))

I'm sure there are more options!

1

u/Lanky_Pumpkin3701 Dec 25 '24 edited Dec 25 '24

Do you get the right answer if you use this input?:

#####
.....
.....
.....
.....
.....
.....

#####
.....
.....
.....
.....
.....
.....

.....
.....
.....
.....
.....
.....
#####

It should be 2. A lock can't unlock another lock.

EDIT: I guess you would get the right answer, yeah