Live data from Hacker News

Learning Rust via Advent of Code

forrestthewoods.com

41–50 of 85 posts

Re: Learning Rust via Advent of Code

#41
post #20

" Learning a new language via Advent of Code turned out to be a great idea. I highly recommend it. " I'll second this: Using a set of progressive problems is an excellent way to get used to actually writing code in a language. " My first stumbling block was parsing. Almost every AoC problem starts with parsing lines of text from an input file. " Scheme. Guile. The PEG parsing module. (There's no kill like overkill.)

> I'll second this: Using a set of progressive problems is an excellent way to get used to actually writing code in a language. IME AoC isn't really progressive though, the problems ramp up and down pretty dramatically. > Scheme. Guile. The PEG parsing module. (There's no kill like overkill.) Rust has several pretty good parsing libraries e.g. nom, lalrpop, pest, …

I've been using Kattis problems as a way to work on my Rust skills. The problem there is you're limited to the standard library. Rust's ultra-minimal stdlib makes a number of those kattis problems remarkably hard to implement, where I've managed to whip up something in short order in Python using the stdlib.

I keep thinking that Rust has gone a little too far on the "keep it minimal" front. It also really disturbs me just how often with rust I'm having to rely on third party hosted crates. That's one huge amount of trust going on there.

Re: Learning Rust via Advent of Code

#42
post #28

This is slightly off-topic, but someone mentioned that it would be a good idea to check out BurntSushi's solutions after trying it yourself which seems like a fantastic idea. It would be cool if there was some resource that linked well-written, idiomatic solutions for other languages as well. Anyone know if this has been done?

https://github.com/Bogdanp/awesome-advent-of-code

Not all repos are idiomatic, there’s at least one that isn’t (mine).

I found some of the repos useful for bettering my rust skills.

Re: Learning Rust via Advent of Code

#43
I also thought regex seemed overkill and didn't feel like adding an extra crate to my Cargo.toml. Luckily for the days I attempted I was able to get by on just the split method for str's[0] which was nice and concise. So for your regex example you could also do:

    // #1 @ 916,616: 21x29
    let parts: Vec = l.split(['@', ',', ':', 'x'].as_ref()).collect();
    let x = parts[1].trim().parse::().expect("x as i32");
    let y = parts[2].trim().parse::().expect("y as i32");
    let w = parts[3].trim().parse::().expect("width as i32");
    let h = parts[4].trim().parse::().expect("height as i32");
[0] https://doc.rust-lang.org/std/primitive.str.html#method.spli...

Re: Learning Rust via Advent of Code

#44
post #18

Hey, I did the same thing this year! At least I tried to do AoC in Rust until I reached the problem which required using doubly-linked lists... Turns out it's not quite as easy as a Rust newbie would think. Regarding input parsing, I pretty quickly converged to using regexes and never looked back. I guess it's an acquired taste, but rubular.com is my best friend when it comes to it.

In my sibling post on performance I go into details on the doubly-linked list problem. https://www.forrestthewoods.com/blog/solving-advent-of-code-... You're right that Rust doesn't make doubly-linked lists easy. For that problem I somewhat skirted the issue. For a later problem I built an Octree. I used an Rc >. That feels pretty gross, but I'm still not sure what the idiomatic pattern is. :(

The community is slowly standardising on using arena-like patterns for this kind of stuff

Re: Learning Rust via Advent of Code

#45

For sorting sequential fields you can chain comparisons with .then(): struct Date { year: u32, month: u32, day: u32, } let mut vec: Vec = Vec::new(); vec.sort_by(|a,b| { a.year.cmp(&b.year) .then(a.month.cmp(&b.month)) .then(a.day.cmp(&b.day)) }); Alternatively you can derive PartialEq, PartialOrd, Eq and Ord for your struct, which will produce a lexicographic ordering based on the top-to-bottom declaration order of…

Or you can use sort_by_key and extract the relevant sorting key as a tuple (or any other Ord structure) e.g. vec.sort_by_key(|d| (d.year, d.month, d.day)) sort_by is more flexible as it works fine with borrows, but when sorting on a series of integer values or references sort_by_key is great. > Alternatively you can derive PartialCmp for your struct, which will produce a lexicographic ordering based on the top-to-bot…

I quite like this tuple approach.

And yeah, I meant Ord (and the required other Traits), I edited my post. Thanks for pointing this out.

Re: Learning Rust via Advent of Code

#46
post #41

Earlier quoted context omitted.

> I'll second this: Using a set of progressive problems is an excellent way to get used to actually writing code in a language. IME AoC isn't really progressive though, the problems ramp up and down pretty dramatically. > Scheme. Guile. The PEG parsing module. (There's no kill like overkill.) Rust has several pretty good parsing libraries e.g. nom, lalrpop, pest, …

I've been using Kattis problems as a way to work on my Rust skills. The problem there is you're limited to the standard library. Rust's ultra-minimal stdlib makes a number of those kattis problems remarkably hard to implement, where I've managed to whip up something in short order in Python using the stdlib. I keep thinking that Rust has gone a little too far on the "keep it minimal" front. It also really disturbs me…

Part of what we do to balance this out is to have the team make some of those third party packages; the regex crate is a good example of that. It’s still maintained by the team even if it’s not in the standard library itself.

Re: Learning Rust via Advent of Code

#47
> I feel like there should be a helper for simple parse operations.

> parse!("#{} @ {},{}: {}x{}", id, x, y, w, h);

> This would be a clean inverse of println!.

The text_io crate does this exactly.

https://crates.io/crates/text_io

    #[macro_use]
    extern crate text_io;

    fn main() {
        let id: u32;
        let x: u32;
        let y: u32;
        let w: u32;
        let h: u32;
        scan!("#{} @ {},{}: {}x{}", id, x, y, w, h);
    }

Re: Learning Rust via Advent of Code

#48
post #33

Earlier quoted context omitted.

I learned Go first and then went to Rust, which was convenient because it appears sushi went the same route.

Fellow Gopher here interested in learning Rust. Any recommendations?

The Rust book is really good.

https://doc.rust-lang.org/book/

Re: Learning Rust via Advent of Code

#49
I did this year's AoC in Ada. (Well, part of it, I didn't do the whole challenge.) I'd never written any serious Ada code before, and this was a great way to get a better feel for it. Overall a positive experience! Ada can be a bit verbose at times, but it wasn't nearly as bad as I had expected it to be... Once you learn not to fight against the grain of the language, it's just a matter of decomposing the problems in an Ada-ish way.

Suprisingly I found it very constructive to work on "two puzzles at once" -- how to use the new language, and solving the AoC itself.

Re: Learning Rust via Advent of Code

#50

I also thought regex seemed overkill and didn't feel like adding an extra crate to my Cargo.toml. Luckily for the days I attempted I was able to get by on just the split method for str's[0] which was nice and concise. So for your regex example you could also do: // #1 @ 916,616: 21x29 let parts: Vec = l.split(['@', ',', ':', 'x'].as_ref()).collect(); let x = parts[1].trim().parse:: ().expect("x as i32"); let y = part…

This is awesome. How did you figure this out? The documentation on `split` says:

"The pattern can be a &str, char, or a closure that determines the split."

But in your case it is an array of chars and it splits for each of them. I don't see this documented at all.

Post reply on HN