Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

This year I really want to try to use this to learn Rust. Are there any good practices or crates to handle file input like most of the problems give?

As I recall, most of the time the input is delineated by spaces or linebreaks and it helps if you can carve it up easily right off the bat.



I tried AoC in Rust two years ago, and you really don't need anything beyond the excellent standard library. Rust has lots of nice features that make working with the type of input AoC gives trivial.

By limiting yourself to the standard library at first, I find that you can get a good feel for the language itself.

You could look up some of the published solutions from last year to get a feel for what is possible.

Example: https://github.com/bertptrs/adventofcode/tree/master/2018/sr...

This person kept a minimal common library for recurring functionality and a small wrapper application that launches the code for each puzzle. It looks like a really clean approach.


I would recommend against a common framework if this is your first venture into rust: I tried exactly this last year, but since I was learning a lot about the language along the way I kept having to go back and update previous days to reflect framework updates.


I tried using it to learn Haskell last year. Warning: it gets hard, and you will want to concentrate on the problem solving, not on compiler errors. It definitely helped my Haskell for the first week or two, but then I switched to Python. I like Rust and recently learned it, but honestly doing AoC in it while learning sounds really painful.


You can do that with the standard library, just do File::read_to_string and String::lines or String::split_whitespace.


Last year I attempted it during a comically short time (only 3 days), but was able to settle on something like this: https://github.com/j1elo/advent-of-code-2018-rust/blob/maste...

i.e. pass the input from standard input, and just build an on-the-go Vector of some data type that is appropriate for the problem at hand. Day 3 was different from 1 and 2 in that for the first time the parsing was not immediate; using Serde and Recap modules did a fantastically succinct job of parsing the text input. Of course it depends on how much you want to rely on external crates.

Let's see if this year I can do more than 3 days worth of AoC :-)


I did part of last year in rust and found that indeed iterating over lines and splits worked quite well.

Also had a good learning experience using the nom parser combinator package [0], but this takes a bit of code (that you can often reuse between days).

If you are aiming for fast solutions, the !scan macro of serde-scan [1] is made for exactly this, but then you are far outside the core language...

[0]: https://crates.io/crates/nom [1]: https://docs.rs/serde_scan/0.3.2/serde_scan/macro.scan.html


> This year I really want to try to use this to learn Rust. Are there any good practices or crates to handle file input like most of the problems give?

Last year I asked the same, and I found this answer on SO:

https://stackoverflow.com/questions/31046763/does-rust-have-...

See the answer which mentions the "text_io" crete.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: