GithubHelp home page GithubHelp logo

advent-of-code-2023's Introduction

These are my solutions to the 2023 Advent Of Code challenge. I'm super-new to Rust, so this is probably full of terrible unidiomatic code! Comments or advice welcome!

Questions

Confusions I had while implementing these that I'd like to follow-up on later to understand better

How to make a vector-containing-a-single-string

My implementation for solve_three originally included:

let mut bracketed_lines: Vec<&str> = Vec::new();
let mut bracketing_line_vec = vec!(std::iter::repeat('.').take(length_of_line).collect::<String>().as_str());
bracketed_lines.append(&mut bracketing_line_vec);
bracketed_lines.append(&mut lines_as_vec);
bracketed_lines.append(&mut bracketing_line_vec);

For which the second line had an error:

temporary value dropped while borrowed
creates a temporary value which is freed while still in use

The solution involved extracting std::iter::...<String>() to a separate variable - however, I don't know why this was different. Why would that value be dropped within vec!?

How to reuse a vector?

Vec::append leaves the other empty, hence the need to clone() in process_three() when "wrapping" the lines in symbol-free lines. How should one append the same vector to another vector twice?

How to find the size of the intersection of two sets?

See implementation of four.rs - .try_len() gives inaccurate answer.

How to reference a Vec field of a struct in impl function?

See five.rs. I originally had (paraphrased):

struct AlmanacMap {
    ranges: Vec<AlmanacMapRange>
}

impl AlmanacMap {
    pub fn map(&self, source: i32) -> i32 {
        return self.ranges.into_iter()
            ...
    }
}

but got cannot move out of 'self.ranges' which is behind a shared reference. I think I understand part of the problem (the self.ranges in the implementation would move that field, and because it's a field the borrow-checker can't guarantee there aren't other moves happening), but am not sure how to resolve it. This seems like a situation where I'd want to borrow, but some quick Googling around suggested that that's impossible.

Why does lines behave differently when passed into a function?

The following is fine:

let mut lines = fs::read_to_string("myfilename.txt").unwrap().lines();
lines.next()

But this fails:

let mut lines = fs::read_to_string("myfilename.txt").unwrap().lines();
do_something_with_lines(lines);

fn do_something_with_lines(lines: Lines<&str>) {
    lines.next();
}

with the following error:

the method `next` exists for struct `std::io::Lines<&str>`, but its trait bounds were not satisfied
the following trait bounds were not satisfied:
`&str: BufRead`
which is required by `std::io::Lines<&str>: Iterator`

I tried changing the type signature to do_something_with_lines(lines: Lines<dyn BufRead>) and got:

the size for values of type `(dyn BufRead + 'static)` cannot be known at compilation time
the trait `Sized` is not implemented for `(dyn BufRead + 'static)`

Should a function ever consume a str?

My intuition is that a function should accept a borrow if the object being passed should outlive the lifetime of the function, and accept an unborrowed object if the object should be consumed (i.e. should not be accessible after the function completes). In challenge seven, I am creating a Hand from a string - and I know that there's no need to refer to that string again after the Hand is created. By the rules above, this suggests to me that I should have Hand::new accept a str (not a &str) - is that correct? And, if so - how would I do that, since lines() etc. all seem to return &strs?

advent-of-code-2023's People

Contributors

scubbo avatar jjackson2-legalzoom avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.