Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using self in Solution implementation #5

Open
drmason13 opened this issue May 8, 2021 · 1 comment
Open

Allow using self in Solution implementation #5

drmason13 opened this issue May 8, 2021 · 1 comment
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@drmason13
Copy link
Owner

All the methods on the traits so far are associated functions.

So essentially the implementing struct is just a marker or container for organising the solutions. It has no use for any data or fields.

I'd like people to experiment with this idea, I have in mind fun implementations of Solution like this:

// from Advent of Code 2019
pub struct IntcodeComputer {
    program: Vec<Instruction>,
    stack: Vec<i32>,
    register: HashMap<Register, i32>,
}

impl Solution<Day2> for IntcodeComputer {
    fn part1(&self) -> u32 {
        // use the intcode computer and access its fields, methods!
        self.program.iter() // ...
    }
}

We could even have mutability.

impl Solution<Day2> for IntcodeComputer {
    fn part1(&mut self) -> u32 {
        // use the intcode computer and mutate it while solving the solution
       self.stack.push(3)
    }
}
@drmason13
Copy link
Owner Author

run might want to ensure each part gets a fresh IntcodeComputer by default.

These signatures are wildly different and incompatible with the current ones, and it won't always make sense to use one or the other.

There might be a need for separate traits, maybe Solution and Solve. I'd like for them to have nice readable names and avoid:

impl SolutionMutSelf<Day2> for IntcodeComputer {

I also wonder if they could blanket impl each other in some way...
If something can solve with part1(&self) maybe part1() should just make a Self and then run part1(&self)?

@drmason13 drmason13 added enhancement New feature or request help wanted Extra attention is needed labels May 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant