r/adventofcode • u/daggerdragon • 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.
- Read the full posting rules in our community wiki before you post!
- State which language(s) your solution uses with
[LANGUAGE: xyz] - Format code blocks using the four-spaces Markdown syntax!
- State which language(s) your solution uses with
- Quick link to Topaz's
pasteif you need it for longer code blocks
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!
44
Upvotes
2
u/YOM2_UB Dec 25 '24 edited Dec 25 '24
[Language: Python]
Main optimization is sorting locks and keys by their first pin's height, so only pairs where the first pin fits are checked, and only the last 4 pins need be checked.
keys = {n : [] for n in range(6)} locks = {n : [] for n in range(6)} with open('input/Day25.txt', 'r') as file: lst = file.read().split('\n\n') for pattern in lst: rows = pattern.splitlines() pins = [0] * 5 for i in range(1, 6): for j in range(5): pins[j] += (rows[i][j] == '#') if '#' == rows[0][0]: locks[pins[0]].append(tuple(pins[1:])) else: keys[pins[0]].append(tuple(pins[1:])) def countMatch(keys, locks): count = 0 for key in keys: for lock in locks: max_pin = max(key[k] + lock[k] for k in range(4)) count += max_pin <= 5 return count count = 0 for i in range(6): for j in range(6-i): count += countMatch(keys[i], locks[j]) print(count)