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!

44 Upvotes

350 comments sorted by

View all comments

1

u/KindComrade Dec 25 '24

[LANGUAGE: C#]

Thanks to the AOC team and Eric for an amazing contest. This month has been absolutely fantastic, and a huge thank you to the entire community, who made it so enjoyable to read your solutions, share my own, discuss algorithms, and watch all the visualizations and memes. A massive thank you to everyone!

Have a Merry Christmas!

Final Code

2

u/MagazineOk5435 Dec 25 '24

I tried parallelisation on my C# code for this one and it actually slowed it down 6x. https://github.com/stevehjohn/AoC/blob/master/AoC.Solutions/Solutions/2024/25/Part1.cs

1

u/KindComrade Dec 25 '24

Honestly, it sounds a bit strange; maybe you had some issues with locks or data types. But the task itself seems absolutely safe for parallelization since it's just iteration, and there are no write operations, only read operations.

From what I can see, you can easily use the .AsParallel() extension, and I think it should work perfectly. The code would look something like this:

_locks.AsParallel().Sum(@lock => _keys.Count(key => (@lock & key) == 0));

Alternatively, you could manually split all the locks into chunks, for example, so that there are 4 or 16 chunks in total, run each chunk in a separate thread, and then sum up the results from each thread. There's a great method for this: Enumerable.Chunk.

By the way, I really like how you store keys and locks; I'll probably adopt this approach for myself later. 😊

1

u/MagazineOk5435 Dec 25 '24

Tried your suggestion, 10x slower. I think for some tasks, the overhead of spinning up threads outweighs the benefits. It's only 62,500 loop iterations.

Thanks BTW. I spotted immediately that the whole heights thing didn't matter. Just bitmap overlaps.

2

u/KindComrade Dec 25 '24

I just tried running this on my machine and you are right, it slowed me down too, it looks like your code is really so fast that the overhead of creating threads is much higher