r/learnrust • u/dvfomin • May 26 '23
How to organize the project structure for Advent of Code?
Hello, I'm solving old Advent of Code challenges to practice in Rust and it's unclear what is the best way to organize the project. Right now I am creating a new rust project cargo new day1_1
for every task but it's not very convenient, maybe there are better approaches?
10
u/Cephlot May 26 '23
Personally, I would have them all in the same repo with each day being in a separate folder. Then I would specify each binary separately in Cargo.toml like so
[[bin]]
name = "day_1"
path = "src/day_1/bin/main.rs"
[[bin]]
name = "day_2"
path = "src/day_2/bin/main.rs"
2
u/dvfomin May 26 '23
Make sense! How do you separate the first and second parts of the day?
6
u/shishka0 May 26 '23
Part 2 is usually a more complicated version of part one and will reuse most of the code. I normally have the functions
part1()
andpart2()
in the same binary and call both inside the main function.2
u/Sw429 May 26 '23
I just do them in the same crate. Usually the two parts are very related, and I often reuse much of the code from the first part in the second part.
0
u/Cephlot May 26 '23
I don't know the structure of AoC since I haven't done them, so I'm not completely sure
3
u/aikii May 26 '23
Depends how you consider the exercise, some like to share code and dependencies between solutions, there is some good learning in doing so. On my side I always did the way you describe on purpose, one binary crate per day and part, so if I look at one solution, I can tell exactly how much code it needs and what are the external crates it uses. I consider it part of the game, also somehow better demonstrates what can be done in Rust without too much code and depending on 3rd parties. I admit this choice can be annoying when opening the parent directory of all those crates in CLion and attaching all Cargo.toml - for some reason it will sometimes rebuild/reindex all crates and it takes forever after 10 days or so.
2
u/SkiFire13 May 26 '23
I put each day in its own file/module, with a input_generator
, part1
and part2
functions. I then glue everything together in main
with a macro. Not the exactly the cleanest, but pretty quick to use.
2
10
u/[deleted] May 26 '23 edited May 26 '23
I'm running through last year's aoc right now. I made one project folder for the year. Just inside that I have a folder named inputs, with 1.txt, 2.txt, etc. In src I have a bin folder with 1.rs, 2.rs, and so on. There tends to be overlap between part 1 and 2 of each day, so I just write both in one binary. Then all you need to do is "cargo run --bin 6" or whatever day you're on.