\$\begingroup\$

I solved the Advent of Code problems last year in Python, and I wanted to learn a new language this year, so I decided to write up Part 1 of on of the hardest problems from last year - problem 15 - in Rust.

While the problem in all its particulars is quite difficult to understand fully, the basic idea is quite simple: each Unit (with two teams: G , the Goblins , and E , the Elves ) is present on a CombatGrid . Every single turn, they check if there's an enemy unit adjacent to them (where diagonal doesn't count as adjacent). If so, they attack that unit, reducing their HP. If not, they move using a weird and kind of fussy algorithm that boils down to BFS, and then proceed to attack if they're now in range of an enemy unit after moving. If not, they end their turn. This goes on until every unit of a particular team is dead.

As far as I can tell, this program is 100% correct in all its particulars - it produced the correct answer for my input and some others I tested. It uses BFS to calculate the correct moves for each unit, which I think is an appropriate enough algorithm, and it's definitely fast enough for the problem's needs.

What I'm really looking for review on is making this code more idiomatic, since I basically just read the Rust book and winged my way through writing this code. I'm not sure about the fact that I have to derive Copy and/or Clone for many of these types, it feels a bit "cheaty", and the structure of the code is also a little more awkward than it feels like it needs to be, but fighting with the borrow checker means some difficulty in correcting that problem.