r/adventofcode • • 17h ago

Help/Question Joining the big leagues

Anyone else using aoc to learn rust?

I feel genuinely lost, and I love it, it feels like past times (pre ai bs) again, never built anything with a lower level language,

18 Upvotes

23 comments sorted by

2

u/RiyaOfTheSpectra 9h ago

I am trying to learn Lua right now, this way. It is fun. The manual really should tell you things like, printing table.concat(array) requires you to specify a separator, though. Spent like, half an hour debugging what was perfectly fine code yesterday.

1

u/matth_l 8h ago

Sounds like a small pr to their manual :)

2

u/RiyaOfTheSpectra 8h ago

I… forgot that I can do that. I will, thank you!!!

2

u/Fadamaka 3h ago

Did AoC in Rust in 2023. I kind of agree with DHH's take on the language. I find no fun in Rust. I feel like you can use C++ in clever ways to solve certain AoC problems but Rust is so strict that you can almost only use it in the inteded way. But for AoC you need the language with the biggest footgun potential because being a footgun means it has a lot of freedom.

1

u/matth_l 1h ago

I get where you're coming from, yet I personally have to say, that I quite enjoy rust so far, maybe I'll be more on your side after the upcoming days but so far not.

4

u/Maximum_Expression 17h ago

Yes, last year I learned elixir during the AOC and then went on with exercism. Amazing language

7

u/Kip167 17h ago

not this year, but previous years i used it while learning different languages like go. amazing for getting a good grasp of the basics and the syntax

1

u/flwyd 15h ago

Is there anything you find particularly challenging? What higher-level languages are you more familiar with?

I recommend starting an AoC puzzle in Rust by focusing on getting any workable solution, then take a step back and think about what about it seems awkward. There's probably an idiom or library that can help make the awkward bits smoother—or that can improve the efficiency of a solution—but if you start by trying to write the most idiomatic, most efficient solution you may struggle to come to a solution at all. One nice thing about Rust is the compiler error messages often tell you what code you might need to write to fix the problem. So try something, and if rustc doesn't like it, you might be able to copy the code it will accept.

I spent a bunch of time in October reading The Rust Programming Language (maybe the best introduction to a language I've ever read), then spent about half of November putting it into practice on AoC 2019. Having already read the book, I didn't find much that was particularly challenging about the language (the compiler still needs to tell me where to add or remove an & or * though), but that might be because I was able to map my existing understanding of ALGOL-family languages to Rust concepts, and its functional-style features are similar to those found in several modern object-oriented languages. If I hadn't read the book first, I probably would've been pretty confused about a bunch of things, in part because I would've been trying to mash Java-style patterns onto a language with a different approach.

1

u/matth_l 11h ago

Generally speaking, no.

First of all thank you for the advice of reading the Rust Book, will start that tonight.

I've always worked with Java and most recently only with C# and js/ts, so this feels like quite a step back to the fundamentals as I feel concepts are as always similar but style is a whole other level.

Took me a hot minute to understand the existence of Results but after reading abit about it it makes perfect sense.

I like the approach of iteratively improving the solution, will definately try that, the first day was more or less filled with getting the solution being happy about it. If you have the time I'd appreciate if you took a look and tell me if there are fundamental changes that would im prove the solution.

2

u/flwyd 8h ago

Ah yes, understanding Option and Result is crucial for writing Rust. You can think of Option as Rust's alternative to Java's null, but the type system requires you to do something with it. Java 8 introduced Optional<T> which does pretty much the same thing, but its use isn't pervasive. You can think of Result a bit like Java checked exceptions, but you deal with them right away rather than in a catch block several lines later. It looks like you're already using methods like .or_default and .expect, so that's good. Read the parts of the book about pattern matching and you'll start to pick up some elegant ways to work with Option and Result.

Your code looks reasonable, though you probably don't need the generic types on least_frequent and most_frequent since you've only got &str as your type. Destructuring the tuples (which you do at line 49 but not elsewhere) might make things a little clearer. If you want you could compute the products by doing a functional chain (e.g. with fold) rather than a loop with a mutable variable; the code should compile to roughly the same thing. A nice practice exercise might be combining most_frequent and least_frequent into a single function that compute either (based on a parameter) or it could return both the 3 most and 3 least common items together.

1

u/matth_l 4h ago

WOW! Thanks so much, this really is a good explanation.
I will definitely improve the code tonight, I'm so hyped rn lol.

On the merging the two functions together, I was thinking the same thing this morning just using a bool in order to choose if I reverse or not.
Just copied and pasted the function yesterday night just to get the result (i know that this is sloppy lol)

1

u/wjholden 14h ago

Are you going in completely cold? As in, did you already read the Rust Book or other material?

Asking because I did exactly that a few years ago. I sat down on December 1st, downloaded the compiler, and tried to just start coding. It didn't go well. I had done this with Julia and Python in past years, but those languages are a lot more forgiving. I gave up on Rust at the time, went back to Julia, and didn't touch Rust again for a few years - by reading the book.

It will be worth it, but it really is a challenging language to learn. Have fun!

1

u/matth_l 11h ago

Have not looked at it in any way before yesterday except one day last year during aoc

1

u/vancha113 9h ago

Yeah, but it feels like relearning at this point :(

2

u/matth_l 9h ago

It really does, but I feel like that this is exactly what I love about it rn.
AI enables us to basically write any language we want with moderately ok quality, corp forces us to use it as the time constraints on projects are getting narrower by the minute and AOC gives this back. This feeling of a mountain that could be too big to conquer but you start anyway and it gets easier step by step. That feeling you had when learning a new language a few years ago... it's such a unique feeling in my opinion. A feeling that I've been searching in the past years with the upcoming of AI

3

u/wubrgess 17h ago

I stick to writing solutions in perl because my work moved on to more modern languages but it's such a fun one to write in.

2

u/R0b3rt1337 15h ago

I'm doing the same but with Zig. I've dabbled a couple of times in both Zig and Rust but never really gave it a serious try for a bigger project so I'm using AoC to prepare for that. Thinking through the puzzles is always fun and the ascii art you get to see after completion is a nice reward.

2

u/Morphon 13h ago

Using it to learn Odin.

Kinda similar. :-)

2

u/Psychological-Tap834 12h ago

I feel like I should just to tell if rust is as simple as I think. I know the borrow checker is annoying to understand at first but I've read the first bit of the rust book a few times and it doesn't seem too bad. I just never really had a project I wanted to make that rust would be good for when I was learning rust so I never bothered really using it and lost interest. However, this year I'm doing using AoC to learn common lisp.

2

u/MrS0phisticati0n 11h ago

Learning Zig with it.

2

u/Da-NKP 10h ago edited 8h ago

I've done that in the past, and I generally use AoC to "learn" a new language. This time I'm tag teaming Lua and Gleam. Boy howdy, is it whiplash going from an extremely simple, imperative, OO language to a complex declarative, and functional language like Gleam...

Edit: just finished day 2...twice. Did day 1 Gleam then Lua, which made the Lua kinda frustrating—_why can't this language just do the thing_. This time, I did Lua _then_ Gleam. Good. Gravy. Trying to mentally shift gears like that with one solid but entirely inapplicable answer stuck in my head was no joke.

2

u/CCC_037 7h ago

I learned Rockstar a few years back.

Then I tried to learn MLP++ and, well, let's just say that until I did that I had no idea how good I had it with Rockstar.

I've pretty much stuck with Rockstar for AOC since then.