-
-
Notifications
You must be signed in to change notification settings - Fork 5
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
Day 4: Camp Cleanup #5
Comments
I think a numeric comparison will work well here. Look for cases where either the first range encloses the second or vice-versa. |
So here's a mystery that stumped me for a while. I wrote this code, based on my previous answers: use std::fs;
use std::io;
use std::io::BufRead;
fn main() {
// Read file line by line
let file = fs::File::open("input.txt")?;
let reader = io::BufReader::new(file);
for line in reader.lines() {
println!("{}", line.unwrap());
}
} That looks fine to me - but the compiler says:
So I pasted that into ChatGPT and it said:
It took me a bit to understand why changing the function signature (adding Then I got it: the |
Here's the version of the code that works: use std::fs;
use std::io;
use std::io::BufRead;
fn main() -> Result<(), std::io::Error> {
// Read file line by line
let file = fs::File::open("input.txt")?;
let reader = io::BufReader::new(file);
for line in reader.lines() {
let line = line?;
println!("{}", line);
}
Ok(())
} |
Tried this: advent-of-code-2022-in-rust/day_04/src/main.rs Lines 1 to 30 in fc234ef
Mostly written by Copilot, prompted by my commits. Apparently wrong:
|
Question was: "In how many assignment pairs does one range fully contain the other?" Maybe exact matches shouldn't count? No, tried that - still got an answer that was too high. I used: if (a <= c && c <= b) || (a <= d && d <= b) && (a != c && b != d) { |
I cheated and ran the solution from https://fasterthanli.me/series/advent-of-code-2022/part-4 - it gets 490, so I should change my code until I get that number too. That example uses trait InclusiveRangeExt {
fn contains_range(&self, other: &Self) -> bool;
// 👋 new! we can have trait methods with default implementations
fn contains_or_is_contained(&self, other: &Self) -> bool {
self.contains_range(other) || other.contains_range(self)
}
}
impl<T> InclusiveRangeExt for RangeInclusive<T>
where
T: PartialOrd,
{
fn contains_range(&self, other: &Self) -> bool {
self.contains(other.start()) && self.contains(other.end())
}
} Which itself uses |
I tried to figure out how to remove that I won't paste in the full transcript because it got tedious. I eventually decided to edit the |
advent-of-code-2022-in-rust/day_04/src/main.rs Lines 1 to 36 in 7b04308
|
Part 2:
|
Copilot completed this task for me based just on my new function name: fn range_overlaps_range(range: (u32, u32), other: (u32, u32)) -> bool {
range.0 <= other.0 && other.0 <= range.1
|| range.0 <= other.1 && other.1 <= range.1
|| other.0 <= range.0 && range.0 <= other.1
|| other.0 <= range.1 && range.1 <= other.1
} |
advent-of-code-2022-in-rust/day_04_part_2/src/main.rs Lines 1 to 39 in 01110b4
|
https://adventofcode.com/2022/day/4
Example:
Diagram:
In how many pairs does one group entirely overlap the other?
The text was updated successfully, but these errors were encountered: