Summing numbers with Iterators in Rust | Advent of Code 2022 Day 1

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

I loved this! I’m doing AoC in rust this year to learn/get better with rust. So being able to watch your process for solving a problem I just solved myself, in detail like this is a huge help! I was able to pick up on a bunch of rust idioms just by you talking through your approach to the problem and considering your options, so I really hope you keep doing these, and keep the detailed talking through the problem in!

aranasaurus
Автор

The reason you can't sort an iterator is because it wouldn't have anywhere to store it. An iterator only has access to the current value, not past or future ones.
Thus sorting requires collecting all of the elements into some sort of new container, which can then be sorted in place. This is what itertools does.

squelchedotter
Автор

I ended up solving this one almost the same way you did, and then experimented with other ideas just for the sake of learning. A few observations:

There are also sort_unstable, sort_unstable_by, etc. They can be more efficient if it is OK to rearrange values that compare equal. On a problem of this size, the performance difference would be tiny; but it's good to know about for those cases when you have much larger inputs.

For parsing the numbers, you can do:
.map(str::parse::<u32>)
(with no closure). I find that just a little easier to read.

To sort in reverse, you can do:
.sort_by(|a, b| a.cmp(b).reverse())
I think that more obviously conveys that you want it sorted in reverse. It's easy to overlook that `a` and `b` were swapped in the closure. There is also sort_by_key() combined with std::cmp::Reverse, which I find a little harder to read (and remember).

I really hope you will post more Advent of Code videos. I enjoy seeing how other people approach problems, including going back and refining a solution.

markday
Автор

As someone trying to use AOC to learn Rust this is great! Learning so much! Thank You!

michaelalls
Автор

Thoroughly explained, and thoroughly enjoying learning rust.

devbites
Автор

this is awesome thank you. I just tackled problem 1 in Python and am starting to learn Rust, so will be following along with this series to try and learn :)

drewmiddleton
Автор

Thx for sharing your solution.
I'm currently learning rust and use advent of code to play around with the language, and it's very helpful to have you explain certain aspects.

Tomorrow, I will have to change my repository to your setup. That looks way more reasonable than mine 😅

tobiasgraf
Автор

Everything here is helpful, but gosh thanks for the project setup notes! Exactly what will be helpful when I get around to day 3 tonight.

ballingt
Автор

Looking forward to this! I am also planning to do this myself. Interested in your solutions😁 Keep it up!

gergelypaless
Автор

I've been meaning to start learning Rust. I mainly work with C#, so this will be very useful!

inzyster
Автор

I'm mindblown by the FP usage here

opposite
Автор

Great video! Looking forward to learning rust with this version of AoC

laupetre
Автор

If anyone is doing this on windows, you will encounter an error while you are splitting because in Windows, the line seperators are \r\n instead of \n

adityasanthosh
Автор

Like you said, parse returns a result but max returns an Option. While you want to unwrap them both in this case, they are quite different.

jasperdevries
Автор

Also trying to do this year‘s AoC with Rust again! My goal is to reach the top 100 once. But it’s not looking very realistic after day 1. 😂

benibachmann
Автор

Funny challenge, though the questions are released in the middle of the night so I'm not even paying attention to the indicated delay to solve them. The delay should count from the instant the question is shown. +1 point for the TDD approach!
I used itertools and coalesce so that I don't have to load the whole file in memory. I know it's not that big, it's just for the sake of trying.

phenanrithe
Автор

I’m writing every puzzle in a different language this year. I did the first day in shell script

jomy-games
Автор

Nice, love comparing solutions and not just blatantly copying yours because I'm stuck!
I was thinking if it was possible to extend the Iterator trait such that you could pick the "top_3()" items or something. Digging through the Iterator file, it was a little too intimidating to start anything

erifetim
Автор

hey i don't get why sum need a type, won't rust know the type from parse?

mustafazakiassagaf
Автор

I solved this challenge in almost the exact same way about an hour ago. Love to see people thinking in a similar way to myself

petarvujovic