Advent of Code 2024 Day 1 - Done Entirely in LINQ!

preview_player
Показать описание

The Day 1 problem from Advent of Code 2024 brings an age-old question of finding the common elements in two sets of numbers. Sorting and walking in lockstep is an equally old strategy that may produce results very time- and space-efficiently. Remember the merge sort algorithm? There you go!

Still, new technologies bring new ideas. Can you find the intersecting elements of two lists without sorting them first? This video will show you how to accomplish that.

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
👨 About Me 👨
Hi, I’m Zoran, I have more than 20 years of experience as a software developer, architect, team lead, and more. I have been programming in C# since its inception in the early 2000s. Since 2017 I have started publishing professional video courses at Pluralsight and Udemy and by this point, there are over 100 hours of the highest-quality videos you can watch on those platforms. On my YouTube channel, you can find shorter video forms focused on clarifying practical issues in coding, design, and architecture of .NET applications.❤️
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
⚡️COPYRIGHT NOTICE:
The Copyright Laws of the United States recognize a “fair use” of copyrighted content. Section 107 of the U.S. Copyright Act states: “Notwithstanding the provisions of sections 106 and 106A, the fair use of a copyrighted work, including such use by reproduction in copies or phono records or by any other means specified by that section, for purposes such as criticism, comment, news reporting, teaching (including multiple copies for classroom use), scholarship, or research, is not an infringement of copyright." This video and our youtube channel, in general, may contain certain copyrighted works that were not specifically authorized to be used by the copyright holder(s), but which we believe in good faith are protected by federal law and the Fair use doctrine for one or more of the reasons noted above.
Рекомендации по теме
Комментарии
Автор

Excellent solution and thought process. That's an instant subscribe!

Wish I could be half that good with LINQ someday.

FelipeCotti
Автор

Please, please, please continue the Advent of Code challenges
Watching your videos is a salve for sour minds 💐

ahmadkelany
Автор

please keep this series going, its nice to see it be done in your typical style.

harisimer
Автор

3:40 Why is the HashSet only created once??

superpcstation
Автор

I found this channel several months ago and it's quickly become one of my favorite programming channels.

I do wonder about this:
> Can you find the intersecting elements of two lists without sorting them first?

The first thing in the solution is a call to Order on both lists. That's sorting both lists first. Syntactically it may not look similar to the original deleted code, but I'd argue that it's essentially the same and doesn't answer the question.

HarleyPebley
Автор

Oh man im too old for this, @Zoran Horvat, how you finding willing and motivation to attend code challenges ? Leverage Linq is brilliant as usual i love it.

eugene
Автор

You solved it just like I did! The first part, anyway. I knew using a HashSet for the second part would scale better, but the input was small enough that it hardly mattered. Later days won't be so kind.

nickcorrado
Автор

This is why I became a Patreon. Thank you for taking the time to show us how you solve these tasks. I already did these tasks in a similar way, but still I learned from this.

dehaaneric
Автор

Would love to see you doing all days of the event. I'm currently on Day 5 finished and love seeing other's approaches.

drewfyre
Автор

How come HashSet is created once? Would it be different it was in a anonymous function (x=> …)

SyKot
Автор

Amazingly succinct, though one more improvement would be to add a concise code comment before each expression summarizing the requirement that it attempts to satisfy. That way future developers and debuggers know exactly what they are working with.

Though always good practice, I say this especially in reference to the requirement for similarityScore, which is calculated by "adding up each number in the left list after multiplying it by the number of times that number appears in the right list". Your first rendition of the expression made that multiplication explicit, but I simply do not find it in your final version after 4:11.

Where is that required multiplication in your final version?

kenbrady
Автор

Nice serie Zoran! Keep them coming :). Wouldn´t it be enough to calculate total distance: int totalDistance = Math.Abs(leftList.Sum() - rightList.Sum())? Do we really need to Sort() anything here?

camlcase
Автор

Great example of using FP to solve what feels like an inherently imperative problem.

In general, iteration is expensive, so if you must iterate do it to set up the data structures that avoid future iterations. In practice this is a `Set` `Map` `Tree` or `Trie`.

Java is lacking a `zip` function, which makes this far less elegant when you have to implement that as well (rolling your own `Gatherer` is such a java answer). Maybe in java 26 or something 😢

adambickford
Автор

what about joins for the second part ? is it a good option?

belongstomyself
visit shbcf.ru