-
-
Notifications
You must be signed in to change notification settings - Fork 5
Day 3: Rucksack Reorganization #4
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
Comments
Had a long conversation with ChatGPT about this. I asked:
The first code example it gave me was; use std::collections::HashSet;
fn main() {
let string1 = "hello";
let string2 = "world";
// Convert the strings to HashSets
let set1: HashSet<char> = string1.chars().collect();
let set2: HashSet<char> = string2.chars().collect();
// Use the intersection method to find the common characters
let common_chars: HashSet<char> = set1.intersection(&set2).collect();
// Print the common characters
for c in common_chars {
println!("{}", c);
}
} I said:
fn main() {
let string1 = "hello";
let string2 = "world";
// Create Vecs of characters for each string
let chars1: Vec<char> = string1.chars().collect();
let chars2: Vec<char> = string2.chars().collect();
// Iterate over the elements of one Vec and check if they appear in the other
for c1 in chars1 {
if chars2.contains(&c1) {
println!("{}", c1);
}
}
} Then:
fn main() {
let string1 = "hello";
let string2 = "world";
// Iterate over the elements of one string and check if they appear in the other
for c1 in string1.chars() {
if string2.contains(c1) {
println!("{}", c1);
}
}
} It added:
|
Finally I said:
fn find_common_chars(string1: &str, string2: &str) {
// Iterate over the elements of one string and check if they appear in the other
for c1 in string1.chars() {
if string2.contains(c1) {
println!("{}", c1);
}
}
}
fn main() {
let string1 = "hello";
let string2 = "world";
find_common_chars(string1, string2);
} |
But when I ran that code I got a warning:
|
Fed my code and that error to ChatGPT:
It replied:
|
I added a print debug line and saw this:
|
I still don't understand that overflow error. My |
Hunch: maybe the problem is the type of the variable that came back from this block of code? let priority = match duplicate {
'a'...'z' => duplicate as u8 - 'a' as u8 + 1,
'A'...'Z' => duplicate as u8 - 'A' as u8 + 27,
_ => 0,
}; Tip from https://fasterthanli.me/series/advent-of-code-2022/part-1 - I'm going to install https://rust-analyzer.github.io/ |
That advice helped! I closed my VS Code window and opened a new one just on a folder with a single Cargo.toml file and now the extension works. |
Got the right answer for part 1! advent-of-code-2022-in-rust/03/src/main.rs Lines 1 to 43 in d3acbd4
|
I'm switching from |
New naming convention, because of this:
So I'm calling things |
Look at sets of three lines:
First find the badges - those are the characters present in all three. Then calculate "What is the sum of the priorities of those item types?". |
Now trying this: use std::fs;
use std::io;
use std::io::BufRead;
fn find_duplicate_character(s1: &str, s2: &str) -> Option<char> {
for c1 in s1.chars() {
for c2 in s2.chars() {
if c1 == c2 {
return Some(c1);
}
}
}
None
}
fn main() -> io::Result<()> {
let file = fs::File::open("input.txt")?;
let reader = io::BufReader::new(file);
let mut score: i32 = 0;
// Read the file three lines at a time
let mut lines = reader.lines();
let mut c1;
while let Some(Ok(line1)) = lines.next() {
if let Some(Ok(line2)) = lines.next() {
if let Some(Ok(line3)) = lines.next() {
// What character is present in all three lines?
// Do not use find_duplicate_character() here
// because it is too slow.
let mut found = false;
for c1 in line1.chars() {
for c2 in line2.chars() {
for c3 in line3.chars() {
if c1 == c2 && c2 == c3 {
found = true;
break;
}
}
if found {
break;
}
}
if found {
break;
}
}
if found {
let priority = match c1 {
'a'..='z' => c1 as u8 - 'a' as u8 + 1,
'A'..='Z' => c1 as u8 - 'A' as u8 + 27,
_ => 0,
} as i32;
score += priority;
} else {
// Quit with error
panic!("No character found that was in all 3 lines");
}
}
}
}
println!("{}", score);
Ok(())
} Getting this error:
|
Ran
|
I had to muck around with variable scope a little bit but I got there: advent-of-code-2022-in-rust/day_03_part_2/src/main.rs Lines 6 to 52 in f37b6b8
|
Tried that and got a warning:
Pasting that back into ChatGPT. |
New error:
Pasted that into ChatGPT, got back a bit of a tangle. Decided to try solving this myself. |
Tried this: while let line1 = lines.next().unwrap()? {
let line2 = lines.next().unwrap()?;
let line3 = lines.next().unwrap()?;
// What character is present in all three lines?
let mut found = false;
for c1 in line1.chars() {
for c2 in line2.chars() {
for c3 in line3.chars() { Warning:
|
Wow https://fasterthanli.me/series/advent-of-code-2022/part-3 has a VERY different solution to this. fn main() -> color_eyre::Result<()> {
let rucksacks = include_str!("input.txt").lines().map(|line| {
line.bytes()
.map(Item::try_from)
.collect::<Result<HashSet<_>, _>>()
});
let sum = itertools::process_results(rucksacks, |rs| {
rs.tuples()
.map(|(a, b, c)| {
a.iter()
.copied()
.find(|i| b.contains(i) && c.contains(i))
.map(|i| dbg!(i.score()))
.unwrap_or_default()
})
.sum::<usize>()
})?;
dbg!(sum);
Ok(())
} I used Copilot to help me create this list of questions, then pasted the whole lot into ChatGPT:
|
Uh oh!
There was an error while loading. Please reload this page.
https://adventofcode.com/2022/day/3
Need to look for common types - items in both the front and back half.
The text was updated successfully, but these errors were encountered: