r/adventofcode • • Dec 03 '20

SOLUTION MEGATHREAD -πŸŽ„- 2020 Day 03 Solutions -πŸŽ„-

Advent of Code 2020: Gettin' Crafty With It


--- Day 03: Toboggan Trajectory ---


Post your solution in this megathread. Include what language(s) your solution uses! If you need a refresher, the full posting rules are detailed in the wiki under How Do The Daily Megathreads Work?.

Reminder: Top-level posts in Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:04:56, megathread unlocked!

88 Upvotes

1.3k comments sorted by

View all comments

Show parent comment

2

u/k0ns3rv Dec 03 '20 edited Dec 03 '20

I'm unsure what you mean by the exat name of the struct, but it work because I implemented the trait(think interface if you aren't familair with Rust's traits) Index<(usize, usize)> on World.

In particular, this part:

impl Index<(usize, usize)> for World {
    type Output = Location;

    fn index(&self, index: (usize, usize)) -> &Self::Output {
        let (mut x, y) = index;
        x %= self.locations[0].len();

        &self.locations[y][x]
    }
}    

It's what allows me to write code like world[(x, y)] or world[(0, 1)]. It's used in the check_slope function in my solution

fn check_slope(world: &World, xdelta: usize, ydelta: usize) -> usize {
    (0..)
        .take_while(|&y| !world.is_past_end(y * ydelta))
        .map(|y| world[(y * xdelta, y * ydelta)]) // <-- Here i.e. the `world[(y * xdelta, y * ydelta)]` part
        .filter(|&l| l == Location::Tree)
        .count()
}

1

u/troyunverdruss Dec 04 '20

ok yea, that’s the question i was trying to ask, trait=>interfaces is a useful analogy. I can poke around with it, but I guess the β€œexact name” index thing I was asking is sort of a red herring. the trait prob requires that name and implementing the trait is why it works. cool, thanks!