rust
December 27, 2018

AOC: Puzzle 4: struct and impl

DISCLAIMER

Each time i dig into some interesting topic while solving Advent Of Code puzzles. This time it was rust structs and method implementation for structs.

For tracking guard shift (see puzzle details) i introduced ShiftTimeline structure:

struct ShiftTimeline {
    data: Vec<u32>,
}

The idea was to keep minutes between the guard was sleeping in vector.

struct, or structure, is a custom data type that lets you name and package together multiple related values that make up a meaningful group. If you’re familiar with an object-oriented language, a structis like an object’s data attributes. In this chapter, we’ll compare and contrast tuples with structs, demonstrate how to use structs, and discuss how to define methods and associated functions to specify behavior associated with a struct’s data. Structs and enums (discussed in Chapter 6) are the building blocks for creating new types in your program’s domain to take full advantage of Rust’s compile time type checking.

First i was going to add more properties but after all I left only one. Its does not makes much sense to keep structure for single field... But i decided to keep it in order to group methods around it. Reading the rust book about methods:

Methods are similar to functions: they’re declared with the fn keyword and their name, they can have parameters and a return value, and they contain some code that is run when they’re called from somewhere else. However, methods are different from functions in that they’re defined within the context of a struct, and their first parameter is always self, which represents the instance of the struct the method is being called on.

When used ShiftTimeline structure i found duplicated data initialization code:

let timeline = ShiftTimeline { data: Vec::new() };

So implemented simple initializer:

struct ShiftTimeline {
    data: Vec<u32>,
}

impl ShiftTimeline {
    fn new() -> ShiftTimeline {
        ShiftTimeline { data: Vec::new() }
    }
}

Now it is possible to use ShiftTimeline::new() to get property initialized. One more method accepting reference to self for write access to structure fields:

impl ShiftTimeline {

    ...

    fn record_sleep_time(&mut self, asleep_at: NaiveDateTime, awake_at: NaiveDateTime) {
       let start_min = asleep_at_time.minute();
       let end_min = awake_at.minute();
       for idx in start_min..end_min {
           self.data.push(idx);
       }
    }
}

...

shift_timeline.record_sleep_time(...)